3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * BAO object for crm_note table.
21 class CRM_Core_BAO_Note
extends CRM_Core_DAO_Note
{
24 * Const the max number of notes we display at any given time.
30 * Given a note id, retrieve the note text.
33 * Id of the note to retrieve.
36 * the note text or NULL if note not found
39 public static function getNoteText($id) {
40 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
44 * Given a note id, retrieve the note subject
47 * Id of the note to retrieve.
50 * the note subject or NULL if note not found
53 public static function getNoteSubject($id) {
54 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'subject');
58 * Given a note id, decide if the note should be displayed based on privacy setting
61 * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself.
64 * TRUE if the note should be displayed, otherwise FALSE
67 public static function getNotePrivacyHidden($note) {
68 if (CRM_Core_Permission
::check('view all notes')) {
72 $noteValues = array();
73 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
74 CRM_Core_DAO
::storeValues($note, $noteValues);
77 $noteDAO = new CRM_Core_DAO_Note();
80 if ($noteDAO->fetch()) {
81 CRM_Core_DAO
::storeValues($noteDAO, $noteValues);
85 CRM_Utils_Hook
::notePrivacy($noteValues);
87 if (empty($noteValues['privacy'])) {
90 elseif (isset($noteValues['notePrivacy_hidden'])) {
91 // If the hook has set visibility, use that setting.
92 return $noteValues['notePrivacy_hidden'];
95 // Default behavior (if hook has not set visibility)
96 // is to hide privacy notes unless the note creator is the current user.
98 if ($noteValues['privacy']) {
99 $session = CRM_Core_Session
::singleton();
100 $userID = $session->get('userID');
101 return ($noteValues['contact_id'] != $userID);
110 * Takes an associative array and creates a note object.
112 * the function extract all the params it needs to initialize the create a
113 * note object. the params array could contain additional unused name/value
116 * @param array $params
117 * (reference) an assoc array of name/value pairs.
119 * (deprecated) associated array with note id - preferably set $params['id'].
120 * @return null|object
121 * $note CRM_Core_BAO_Note object
122 * @throws \CRM_Core_Exception
124 public static function add(&$params, $ids = array()) {
125 $dataExists = self
::dataExists($params);
130 if (!empty($params['entity_table']) && $params['entity_table'] == 'civicrm_contact' && !empty($params['check_permissions'])) {
131 if (!CRM_Contact_BAO_Contact_Permission
::allow($params['entity_id'], CRM_Core_Permission
::EDIT
)) {
132 throw new CRM_Core_Exception('Permission denied to modify contact record');
136 $note = new CRM_Core_BAO_Note();
138 if (!isset($params['privacy'])) {
139 $params['privacy'] = 0;
142 $note->copyValues($params);
143 if (empty($params['contact_id'])) {
144 if (CRM_Utils_Array
::value('entity_table', $params) == 'civicrm_contact') {
145 $note->contact_id
= $params['entity_id'];
148 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
155 // check and attach and files as needed
156 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_note', $note->id
);
158 if ($note->entity_table
== 'civicrm_contact') {
159 CRM_Core_BAO_Log
::register($note->entity_id
,
163 $displayName = CRM_Contact_BAO_Contact
::displayName($note->entity_id
);
165 $noteActions = FALSE;
167 $loggedInContactID = CRM_Core_Session
::singleton()->getLoggedInContactID();
168 if ($loggedInContactID) {
169 if ($loggedInContactID == $note->entity_id
) {
172 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
177 $recentOther = array();
179 $recentOther = array(
180 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
181 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
183 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
184 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
189 // add the recently created Note
190 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
191 CRM_Utils_System
::url('civicrm/contact/view/note',
192 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
206 * Check if there is data to create the object.
208 * @param array $params
209 * (reference ) an assoc array of name/value pairs.
213 public static function dataExists(&$params) {
214 // return if no data present
215 if (empty($params['id']) && !strlen($params['note'])) {
222 * Given the list of params in the params array, fetch the object
223 * and store the values in the values array
225 * @param array $params
226 * Input parameters to find object.
227 * @param array $values
228 * Output values of the object.
229 * @param int $numNotes
230 * The maximum number of notes to return (0 if all).
233 * $notes Object of CRM_Core_BAO_Note
235 public static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
236 if (empty($params)) {
239 $note = new CRM_Core_BAO_Note();
240 $note->entity_id
= $params['contact_id'];
241 $note->entity_table
= 'civicrm_contact';
243 // get the total count of notes
244 $values['noteTotalCount'] = $note->count();
246 // get only 3 recent notes
247 $note->orderBy('modified_date desc');
248 $note->limit($numNotes);
253 while ($note->fetch()) {
254 $values['note'][$note->id
] = array();
255 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
259 // if we have collected the number of notes, exit loop
260 if ($numNotes > 0 && $count >= $numNotes) {
273 * @param bool $showStatus
274 * Do we need to set status or not.
277 * no of deleted notes on success, null otherwise
279 public static function del($id, $showStatus = TRUE) {
281 $recent = array($id);
282 $note = new CRM_Core_DAO_Note();
286 if ($note->entity_table
== 'civicrm_note') {
287 $status = ts('Selected Comment has been deleted successfully.');
290 $status = ts('Selected Note has been deleted successfully.');
293 // Delete all descendents of this Note
294 foreach (self
::getDescendentIds($id) as $childId) {
295 $childNote = new CRM_Core_DAO_Note();
296 $childNote->id
= $childId;
297 $childNote->delete();
298 $recent[] = $childId;
301 $return = $note->delete();
303 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
306 // delete the recently created Note
307 foreach ($recent as $recentId) {
312 CRM_Utils_Recent
::del($noteRecent);
318 * Delete all records for this contact id.
321 * ID of the contact for which note needs to be deleted.
323 public static function deleteContact($id) {
324 // need to delete for both entity_id
325 $dao = new CRM_Core_DAO_Note();
326 $dao->entity_table
= 'civicrm_contact';
327 $dao->entity_id
= $id;
330 // and the creator contact id
331 $dao = new CRM_Core_DAO_Note();
332 $dao->contact_id
= $id;
337 * Retrieve all records for this entity-id
340 * ID of the relationship for which records needs to be retrieved.
342 * @param string $entityTable
345 * array of note properties
348 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
355 WHERE entity_table=\"{$entityTable}\"
358 ORDER BY modified_date desc";
359 $params = array(1 => array($id, 'Integer'));
361 $dao = CRM_Core_DAO
::executeQuery($query, $params);
363 while ($dao->fetch()) {
364 $viewNote[$dao->id
] = $dao->note
;
371 * Get log record count for a Contact.
373 * @param int $contactID
376 * $count count of log records
379 public static function getContactNoteCount($contactID) {
380 $note = new CRM_Core_DAO_Note();
381 $note->entity_id
= $contactID;
382 $note->entity_table
= 'civicrm_contact';
385 while ($note->fetch()) {
386 if (!self
::getNotePrivacyHidden($note)) {
394 * Get all descendent notes of the note with given ID.
396 * @param int $parentId
397 * ID of the note to start from.
398 * @param int $maxDepth
399 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
400 * @param bool $snippet
401 * If TRUE, returned values will be pre-formatted for display in a table of notes.
404 * Nested associative array beginning with direct children of given note.
407 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
408 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
412 * Get total count of direct children visible to the current user.
418 * $count Number of notes having the give note as parent
421 public static function getChildCount($id) {
422 $note = new CRM_Core_DAO_Note();
423 $note->entity_table
= 'civicrm_note';
424 $note->entity_id
= $id;
427 while ($note->fetch()) {
428 if (!self
::getNotePrivacyHidden($note)) {
436 * Recursive function to get all descendent notes of the note with given ID.
438 * @param int $parentId
439 * ID of the note to start from.
440 * @param int $maxDepth
441 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
442 * @param bool $snippet
443 * If TRUE, returned values will be pre-formatted for display in a table of notes.
445 * (Reference) Variable to store all found descendents.
447 * Depth of current iteration within the descendent tree (used for comparison against maxDepth).
450 * Nested associative array beginning with direct children of given note.
452 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) {
453 if ($maxDepth && $depth > $maxDepth) {
457 // get direct children of given parentId note
458 $note = new CRM_Core_DAO_Note();
459 $note->entity_table
= 'civicrm_note';
460 $note->entity_id
= $parentId;
461 $note->orderBy('modified_date asc');
463 while ($note->fetch()) {
464 // foreach child, call this function, unless the child is private/hidden
465 if (!self
::getNotePrivacyHidden($note)) {
466 CRM_Core_DAO
::storeValues($note, $tree[$note->id
]);
468 // get name of user that created this note
469 $contact = new CRM_Contact_DAO_Contact();
470 $createdById = $note->contact_id
;
471 $contact->id
= $createdById;
474 $tree[$note->id
]['createdBy'] = $contact->display_name
;
475 $tree[$note->id
]['createdById'] = $createdById;
476 $tree[$note->id
]['modified_date'] = CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
478 // paper icon view for attachments part
479 $paperIconAttachmentInfo = CRM_Core_BAO_File
::paperIconAttachment('civicrm_note', $note->id
);
480 $tree[$note->id
]['attachment'] = $paperIconAttachmentInfo ?
implode('', $paperIconAttachmentInfo) : '';
483 $tree[$note->id
]['note'] = nl2br($tree[$note->id
]['note']);
484 $tree[$note->id
]['note'] = smarty_modifier_mb_truncate(
485 $tree[$note->id
]['note'],
490 CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
496 $tree[$note->id
]['child'],
506 * Given a note id, get a list of the ids of all notes that are descendents of that note
508 * @param int $parentId
509 * Id of the given note.
511 * (reference) one-dimensional array to store found descendent ids.
514 * One-dimensional array containing ids of all desendent notes
516 public static function getDescendentIds($parentId, &$ids = array()) {
517 // get direct children of given parentId note
518 $note = new CRM_Core_DAO_Note();
519 $note->entity_table
= 'civicrm_note';
520 $note->entity_id
= $parentId;
522 while ($note->fetch()) {
523 // foreach child, add to ids list, and recurse
525 self
::getDescendentIds($note->id
, $ids);
531 * Delete all note related to contact when contact is deleted.
533 * @param int $contactID
534 * Contact id whose notes to be deleted.
536 public static function cleanContactNotes($contactID) {
537 $params = array(1 => array($contactID, 'Integer'));
539 // delete all notes related to contribution
540 $contributeQuery = "DELETE note.*
541 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
542 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
544 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
546 // delete all notes related to participant
547 $participantQuery = "DELETE note.*
548 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
549 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
551 CRM_Core_DAO
::executeQuery($participantQuery, $params);
553 // delete all contact notes
554 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
556 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
557 while ($contactNoteId->fetch()) {
558 self
::del($contactNoteId->id
, FALSE);
563 * Whitelist of possible values for the entity_table field
566 public static function entityTables() {
568 'civicrm_relationship' => 'Relationship',
569 'civicrm_contact' => 'Contact',
570 'civicrm_participant' => 'Participant',
571 'civicrm_contribution' => 'Contribution',