| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * BAO object for crm_note table |
| 38 | */ |
| 39 | class CRM_Core_BAO_Note extends CRM_Core_DAO_Note { |
| 40 | |
| 41 | /** |
| 42 | * Const the max number of notes we display at any given time. |
| 43 | * @var int |
| 44 | */ |
| 45 | const MAX_NOTES = 3; |
| 46 | |
| 47 | /** |
| 48 | * Given a note id, retrieve the note text |
| 49 | * |
| 50 | * @param int $id |
| 51 | * Id of the note to retrieve. |
| 52 | * |
| 53 | * @return string |
| 54 | * the note text or NULL if note not found |
| 55 | * |
| 56 | */ |
| 57 | public static function getNoteText($id) { |
| 58 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $id, 'note'); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Given a note id, retrieve the note subject |
| 63 | * |
| 64 | * @param int $id |
| 65 | * Id of the note to retrieve. |
| 66 | * |
| 67 | * @return string |
| 68 | * the note subject or NULL if note not found |
| 69 | * |
| 70 | */ |
| 71 | public static function getNoteSubject($id) { |
| 72 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $id, 'subject'); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Given a note id, decide if the note should be displayed based on privacy setting |
| 77 | * |
| 78 | * @param object $note |
| 79 | * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself. |
| 80 | * |
| 81 | * @return bool |
| 82 | * TRUE if the note should be displayed, otherwise FALSE |
| 83 | * |
| 84 | */ |
| 85 | public static function getNotePrivacyHidden($note) { |
| 86 | if (CRM_Core_Permission::check('view all notes')) { |
| 87 | return FALSE; |
| 88 | } |
| 89 | |
| 90 | $noteValues = array(); |
| 91 | if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') { |
| 92 | CRM_Core_DAO::storeValues($note, $noteValues); |
| 93 | } |
| 94 | else { |
| 95 | $noteDAO = new CRM_Core_DAO_Note(); |
| 96 | $noteDAO->id = $note; |
| 97 | $noteDAO->find(); |
| 98 | if ($noteDAO->fetch()) { |
| 99 | CRM_Core_DAO::storeValues($noteDAO, $noteValues); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | CRM_Utils_Hook::notePrivacy($noteValues); |
| 104 | |
| 105 | if (!$noteValues['privacy']) { |
| 106 | return FALSE; |
| 107 | } |
| 108 | elseif (isset($noteValues['notePrivacy_hidden'])) { |
| 109 | // If the hook has set visibility, use that setting. |
| 110 | return $noteValues['notePrivacy_hidden']; |
| 111 | } |
| 112 | else { |
| 113 | // Default behavior (if hook has not set visibility) |
| 114 | // is to hide privacy notes unless the note creator is the current user. |
| 115 | |
| 116 | if ($noteValues['privacy']) { |
| 117 | $session = CRM_Core_Session::singleton(); |
| 118 | $userID = $session->get('userID'); |
| 119 | return ($noteValues['contact_id'] != $userID); |
| 120 | } |
| 121 | else { |
| 122 | return FALSE; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Takes an associative array and creates a note object. |
| 129 | * |
| 130 | * the function extract all the params it needs to initialize the create a |
| 131 | * note object. the params array could contain additional unused name/value |
| 132 | * pairs |
| 133 | * |
| 134 | * @param array $params |
| 135 | * (reference) an assoc array of name/value pairs. |
| 136 | * @param array $ids |
| 137 | * (deprecated) associated array with note id - preferably set $params['id']. |
| 138 | * |
| 139 | * @return object |
| 140 | * $note CRM_Core_BAO_Note object |
| 141 | */ |
| 142 | public static function &add(&$params, $ids = array()) { |
| 143 | $dataExists = self::dataExists($params); |
| 144 | if (!$dataExists) { |
| 145 | return CRM_Core_DAO::$_nullObject; |
| 146 | } |
| 147 | |
| 148 | $note = new CRM_Core_BAO_Note(); |
| 149 | |
| 150 | if (!isset($params['modified_date'])) { |
| 151 | $params['modified_date'] = date("Ymd"); |
| 152 | } |
| 153 | |
| 154 | if (!isset($params['privacy'])) { |
| 155 | $params['privacy'] = 0; |
| 156 | } |
| 157 | |
| 158 | $note->copyValues($params); |
| 159 | if (empty($params['contact_id'])) { |
| 160 | if ($params['entity_table'] == 'civicrm_contact') { |
| 161 | $note->contact_id = $params['entity_id']; |
| 162 | } |
| 163 | } |
| 164 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids)); |
| 165 | if ($id) { |
| 166 | $note->id = $id; |
| 167 | } |
| 168 | |
| 169 | $note->save(); |
| 170 | |
| 171 | // check and attach and files as needed |
| 172 | CRM_Core_BAO_File::processAttachment($params, 'civicrm_note', $note->id); |
| 173 | |
| 174 | if ($note->entity_table == 'civicrm_contact') { |
| 175 | CRM_Core_BAO_Log::register($note->entity_id, |
| 176 | 'civicrm_note', |
| 177 | $note->id |
| 178 | ); |
| 179 | $displayName = CRM_Contact_BAO_Contact::displayName($note->entity_id); |
| 180 | |
| 181 | $noteActions = FALSE; |
| 182 | $session = CRM_Core_Session::singleton(); |
| 183 | if ($session->get('userID')) { |
| 184 | if ($session->get('userID') == $note->entity_id) { |
| 185 | $noteActions = TRUE; |
| 186 | } |
| 187 | elseif (CRM_Contact_BAO_Contact_Permission::allow($note->entity_id, CRM_Core_Permission::EDIT)) { |
| 188 | $noteActions = TRUE; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | $recentOther = array(); |
| 193 | if ($noteActions) { |
| 194 | $recentOther = array( |
| 195 | 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/note', |
| 196 | "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home" |
| 197 | ), |
| 198 | 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/note', |
| 199 | "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home" |
| 200 | ), |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | // add the recently created Note |
| 205 | CRM_Utils_Recent::add($displayName . ' - ' . $note->subject, |
| 206 | CRM_Utils_System::url('civicrm/contact/view/note', |
| 207 | "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home" |
| 208 | ), |
| 209 | $note->id, |
| 210 | 'Note', |
| 211 | $note->entity_id, |
| 212 | $displayName, |
| 213 | $recentOther |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | return $note; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Check if there is data to create the object. |
| 222 | * |
| 223 | * @param array $params |
| 224 | * (reference ) an assoc array of name/value pairs. |
| 225 | * |
| 226 | * @return bool |
| 227 | */ |
| 228 | public static function dataExists(&$params) { |
| 229 | // return if no data present |
| 230 | if (!strlen($params['note'])) { |
| 231 | return FALSE; |
| 232 | } |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Given the list of params in the params array, fetch the object |
| 238 | * and store the values in the values array |
| 239 | * |
| 240 | * @param array $params |
| 241 | * Input parameters to find object. |
| 242 | * @param array $values |
| 243 | * Output values of the object. |
| 244 | * @param int $numNotes |
| 245 | * The maximum number of notes to return (0 if all). |
| 246 | * |
| 247 | * @return object |
| 248 | * $notes Object of CRM_Core_BAO_Note |
| 249 | */ |
| 250 | public static function &getValues(&$params, &$values, $numNotes = self::MAX_NOTES) { |
| 251 | if (empty($params)) { |
| 252 | return NULL; |
| 253 | } |
| 254 | $note = new CRM_Core_BAO_Note(); |
| 255 | $note->entity_id = $params['contact_id']; |
| 256 | $note->entity_table = 'civicrm_contact'; |
| 257 | |
| 258 | // get the total count of notes |
| 259 | $values['noteTotalCount'] = $note->count(); |
| 260 | |
| 261 | // get only 3 recent notes |
| 262 | $note->orderBy('modified_date desc'); |
| 263 | $note->limit($numNotes); |
| 264 | $note->find(); |
| 265 | |
| 266 | $notes = array(); |
| 267 | $count = 0; |
| 268 | while ($note->fetch()) { |
| 269 | $values['note'][$note->id] = array(); |
| 270 | CRM_Core_DAO::storeValues($note, $values['note'][$note->id]); |
| 271 | $notes[] = $note; |
| 272 | |
| 273 | $count++; |
| 274 | // if we have collected the number of notes, exit loop |
| 275 | if ($numNotes > 0 && $count >= $numNotes) { |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return $notes; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Delete the notes. |
| 285 | * |
| 286 | * @param int $id |
| 287 | * Note id. |
| 288 | * @param bool $showStatus |
| 289 | * Do we need to set status or not. |
| 290 | * |
| 291 | * @return int|NULL |
| 292 | * no of deleted notes on success, null otherwise |
| 293 | */ |
| 294 | public static function del($id, $showStatus = TRUE) { |
| 295 | $return = NULL; |
| 296 | $recent = array($id); |
| 297 | $note = new CRM_Core_DAO_Note(); |
| 298 | $note->id = $id; |
| 299 | $note->find(); |
| 300 | $note->fetch(); |
| 301 | if ($note->entity_table == 'civicrm_note') { |
| 302 | $status = ts('Selected Comment has been deleted successfully.'); |
| 303 | } |
| 304 | else { |
| 305 | $status = ts('Selected Note has been deleted successfully.'); |
| 306 | } |
| 307 | |
| 308 | // Delete all descendents of this Note |
| 309 | foreach (self::getDescendentIds($id) as $childId) { |
| 310 | $childNote = new CRM_Core_DAO_Note(); |
| 311 | $childNote->id = $childId; |
| 312 | $childNote->delete(); |
| 313 | $childNote->free(); |
| 314 | $recent[] = $childId; |
| 315 | } |
| 316 | |
| 317 | $return = $note->delete(); |
| 318 | $note->free(); |
| 319 | if ($showStatus) { |
| 320 | CRM_Core_Session::setStatus($status, ts('Deleted'), 'success'); |
| 321 | } |
| 322 | |
| 323 | // delete the recently created Note |
| 324 | foreach ($recent as $recentId) { |
| 325 | $noteRecent = array( |
| 326 | 'id' => $recentId, |
| 327 | 'type' => 'Note', |
| 328 | ); |
| 329 | CRM_Utils_Recent::del($noteRecent); |
| 330 | } |
| 331 | return $return; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Delete all records for this contact id. |
| 336 | * |
| 337 | * @param int $id |
| 338 | * ID of the contact for which note needs to be deleted. |
| 339 | * |
| 340 | * @return void |
| 341 | * |
| 342 | */ |
| 343 | public static function deleteContact($id) { |
| 344 | // need to delete for both entity_id |
| 345 | $dao = new CRM_Core_DAO_Note(); |
| 346 | $dao->entity_table = 'civicrm_contact'; |
| 347 | $dao->entity_id = $id; |
| 348 | $dao->delete(); |
| 349 | |
| 350 | // and the creator contact id |
| 351 | $dao = new CRM_Core_DAO_Note(); |
| 352 | $dao->contact_id = $id; |
| 353 | $dao->delete(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Retrieve all records for this entity-id |
| 358 | * |
| 359 | * @param int $id |
| 360 | * ID of the relationship for which records needs to be retrieved. |
| 361 | * |
| 362 | * @param string $entityTable |
| 363 | * |
| 364 | * @return array |
| 365 | * array of note properties |
| 366 | * |
| 367 | */ |
| 368 | public static function &getNote($id, $entityTable = 'civicrm_relationship') { |
| 369 | $viewNote = array(); |
| 370 | |
| 371 | $query = " |
| 372 | SELECT id, |
| 373 | note |
| 374 | FROM civicrm_note |
| 375 | WHERE entity_table=\"{$entityTable}\" |
| 376 | AND entity_id = %1 |
| 377 | AND note is not null |
| 378 | ORDER BY modified_date desc"; |
| 379 | $params = array(1 => array($id, 'Integer')); |
| 380 | |
| 381 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 382 | |
| 383 | while ($dao->fetch()) { |
| 384 | $viewNote[$dao->id] = $dao->note; |
| 385 | } |
| 386 | |
| 387 | return $viewNote; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get log record count for a Contact. |
| 392 | * |
| 393 | * @param int $contactID |
| 394 | * |
| 395 | * @return int |
| 396 | * $count count of log records |
| 397 | * |
| 398 | */ |
| 399 | public static function getContactNoteCount($contactID) { |
| 400 | $note = new CRM_Core_DAO_Note(); |
| 401 | $note->entity_id = $contactID; |
| 402 | $note->entity_table = 'civicrm_contact'; |
| 403 | $note->find(); |
| 404 | $count = 0; |
| 405 | while ($note->fetch()) { |
| 406 | if (!self::getNotePrivacyHidden($note)) { |
| 407 | $count++; |
| 408 | } |
| 409 | } |
| 410 | return $count; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Get all descendent notes of the note with given ID. |
| 415 | * |
| 416 | * @param int $parentId |
| 417 | * ID of the note to start from. |
| 418 | * @param int $maxDepth |
| 419 | * Maximum number of levels to descend into the tree; if not given, will include all descendents. |
| 420 | * @param bool $snippet |
| 421 | * If TRUE, returned values will be pre-formatted for display in a table of notes. |
| 422 | * |
| 423 | * @return array |
| 424 | * Nested associative array beginning with direct children of given note. |
| 425 | * |
| 426 | */ |
| 427 | public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) { |
| 428 | return self::buildNoteTree($parentId, $maxDepth, $snippet); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Get total count of direct children visible to the current user. |
| 433 | * |
| 434 | * @param int $id |
| 435 | * Note ID. |
| 436 | * |
| 437 | * @return int |
| 438 | * $count Number of notes having the give note as parent |
| 439 | * |
| 440 | */ |
| 441 | public static function getChildCount($id) { |
| 442 | $note = new CRM_Core_DAO_Note(); |
| 443 | $note->entity_table = 'civicrm_note'; |
| 444 | $note->entity_id = $id; |
| 445 | $note->find(); |
| 446 | $count = 0; |
| 447 | while ($note->fetch()) { |
| 448 | if (!self::getNotePrivacyHidden($note)) { |
| 449 | $count++; |
| 450 | } |
| 451 | } |
| 452 | return $count; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Recursive function to get all descendent notes of the note with given ID. |
| 457 | * |
| 458 | * @param int $parentId |
| 459 | * ID of the note to start from. |
| 460 | * @param int $maxDepth |
| 461 | * Maximum number of levels to descend into the tree; if not given, will include all descendents. |
| 462 | * @param bool $snippet |
| 463 | * If TRUE, returned values will be pre-formatted for display in a table of notes. |
| 464 | * @param array $tree |
| 465 | * (Reference) Variable to store all found descendents. |
| 466 | * @param int $depth |
| 467 | * Depth of current iteration within the descendent tree (used for comparison against maxDepth). |
| 468 | * |
| 469 | * @return array |
| 470 | * Nested associative array beginning with direct children of given note. |
| 471 | */ |
| 472 | private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) { |
| 473 | if ($maxDepth && $depth > $maxDepth) { |
| 474 | return FALSE; |
| 475 | } |
| 476 | |
| 477 | // get direct children of given parentId note |
| 478 | $note = new CRM_Core_DAO_Note(); |
| 479 | $note->entity_table = 'civicrm_note'; |
| 480 | $note->entity_id = $parentId; |
| 481 | $note->orderBy('modified_date asc'); |
| 482 | $note->find(); |
| 483 | while ($note->fetch()) { |
| 484 | // foreach child, call this function, unless the child is private/hidden |
| 485 | if (!self::getNotePrivacyHidden($note)) { |
| 486 | CRM_Core_DAO::storeValues($note, $tree[$note->id]); |
| 487 | |
| 488 | // get name of user that created this note |
| 489 | $contact = new CRM_Contact_DAO_Contact(); |
| 490 | $createdById = $note->contact_id; |
| 491 | $contact->id = $createdById; |
| 492 | $contact->find(); |
| 493 | $contact->fetch(); |
| 494 | $tree[$note->id]['createdBy'] = $contact->display_name; |
| 495 | $tree[$note->id]['createdById'] = $createdById; |
| 496 | $tree[$note->id]['modified_date'] = CRM_Utils_Date::customFormat($tree[$note->id]['modified_date']); |
| 497 | |
| 498 | // paper icon view for attachments part |
| 499 | $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id); |
| 500 | $tree[$note->id]['attachment'] = $paperIconAttachmentInfo ? implode('', $paperIconAttachmentInfo) : ''; |
| 501 | |
| 502 | if ($snippet) { |
| 503 | $tree[$note->id]['note'] = nl2br($tree[$note->id]['note']); |
| 504 | $tree[$note->id]['note'] = smarty_modifier_mb_truncate( |
| 505 | $tree[$note->id]['note'], |
| 506 | 80, |
| 507 | '...', |
| 508 | TRUE |
| 509 | ); |
| 510 | CRM_Utils_Date::customFormat($tree[$note->id]['modified_date']); |
| 511 | } |
| 512 | self::buildNoteTree( |
| 513 | $note->id, |
| 514 | $maxDepth, |
| 515 | $snippet, |
| 516 | $tree[$note->id]['child'], |
| 517 | $depth + 1 |
| 518 | ); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return $tree; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Given a note id, get a list of the ids of all notes that are descendents of that note |
| 527 | * |
| 528 | * @param int $parentId |
| 529 | * Id of the given note. |
| 530 | * @param array $ids |
| 531 | * (reference) one-dimensional array to store found descendent ids. |
| 532 | * |
| 533 | * @return array |
| 534 | * One-dimensional array containing ids of all desendent notes |
| 535 | */ |
| 536 | public static function getDescendentIds($parentId, &$ids = array()) { |
| 537 | // get direct children of given parentId note |
| 538 | $note = new CRM_Core_DAO_Note(); |
| 539 | $note->entity_table = 'civicrm_note'; |
| 540 | $note->entity_id = $parentId; |
| 541 | $note->find(); |
| 542 | while ($note->fetch()) { |
| 543 | // foreach child, add to ids list, and recurse |
| 544 | $ids[] = $note->id; |
| 545 | self::getDescendentIds($note->id, $ids); |
| 546 | } |
| 547 | return $ids; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Delete all note related to contact when contact is deleted. |
| 552 | * |
| 553 | * @param int $contactID |
| 554 | * Contact id whose notes to be deleted. |
| 555 | * |
| 556 | * @return void |
| 557 | */ |
| 558 | public static function cleanContactNotes($contactID) { |
| 559 | $params = array(1 => array($contactID, 'Integer')); |
| 560 | |
| 561 | // delete all notes related to contribution |
| 562 | $contributeQuery = "DELETE note.* |
| 563 | FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id |
| 564 | WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'"; |
| 565 | |
| 566 | CRM_Core_DAO::executeQuery($contributeQuery, $params); |
| 567 | |
| 568 | // delete all notes related to participant |
| 569 | $participantQuery = "DELETE note.* |
| 570 | FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id |
| 571 | WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'"; |
| 572 | |
| 573 | CRM_Core_DAO::executeQuery($participantQuery, $params); |
| 574 | |
| 575 | // delete all contact notes |
| 576 | $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'"; |
| 577 | |
| 578 | $contactNoteId = CRM_Core_DAO::executeQuery($contactQuery, $params); |
| 579 | while ($contactNoteId->fetch()) { |
| 580 | self::del($contactNoteId->id, FALSE); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | } |