3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * BAO object for crm_note table.
37 class CRM_Core_BAO_Note
extends CRM_Core_DAO_Note
{
40 * Const the max number of notes we display at any given time.
46 * Given a note id, retrieve the note text.
49 * Id of the note to retrieve.
52 * the note text or NULL if note not found
55 public static function getNoteText($id) {
56 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
60 * Given a note id, retrieve the note subject
63 * Id of the note to retrieve.
66 * the note subject or NULL if note not found
69 public static function getNoteSubject($id) {
70 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'subject');
74 * Given a note id, decide if the note should be displayed based on privacy setting
77 * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself.
80 * TRUE if the note should be displayed, otherwise FALSE
83 public static function getNotePrivacyHidden($note) {
84 if (CRM_Core_Permission
::check('view all notes')) {
88 $noteValues = array();
89 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
90 CRM_Core_DAO
::storeValues($note, $noteValues);
93 $noteDAO = new CRM_Core_DAO_Note();
96 if ($noteDAO->fetch()) {
97 CRM_Core_DAO
::storeValues($noteDAO, $noteValues);
101 CRM_Utils_Hook
::notePrivacy($noteValues);
103 if (!$noteValues['privacy']) {
106 elseif (isset($noteValues['notePrivacy_hidden'])) {
107 // If the hook has set visibility, use that setting.
108 return $noteValues['notePrivacy_hidden'];
111 // Default behavior (if hook has not set visibility)
112 // is to hide privacy notes unless the note creator is the current user.
114 if ($noteValues['privacy']) {
115 $session = CRM_Core_Session
::singleton();
116 $userID = $session->get('userID');
117 return ($noteValues['contact_id'] != $userID);
126 * Takes an associative array and creates a note object.
128 * the function extract all the params it needs to initialize the create a
129 * note object. the params array could contain additional unused name/value
132 * @param array $params
133 * (reference) an assoc array of name/value pairs.
135 * (deprecated) associated array with note id - preferably set $params['id'].
136 * @return null|object
137 * $note CRM_Core_BAO_Note object
138 * @throws \CRM_Exception
140 public static function add(&$params, $ids = array()) {
141 $dataExists = self
::dataExists($params);
146 if (!empty($params['entity_table']) && $params['entity_table'] == 'civicrm_contact' && !empty($params['check_permissions'])) {
147 if (!CRM_Contact_BAO_Contact_Permission
::allow($params['entity_id'], CRM_Core_Permission
::EDIT
)) {
148 throw new CRM_Exception('Permission denied to modify contact record');
152 $note = new CRM_Core_BAO_Note();
154 if (!isset($params['modified_date'])) {
155 $params['modified_date'] = date("Ymd");
158 if (!isset($params['privacy'])) {
159 $params['privacy'] = 0;
162 $note->copyValues($params);
163 if (empty($params['contact_id'])) {
164 if (CRM_Utils_Array
::value('entity_table', $params) == 'civicrm_contact') {
165 $note->contact_id
= $params['entity_id'];
168 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
175 // check and attach and files as needed
176 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_note', $note->id
);
178 if ($note->entity_table
== 'civicrm_contact') {
179 CRM_Core_BAO_Log
::register($note->entity_id
,
183 $displayName = CRM_Contact_BAO_Contact
::displayName($note->entity_id
);
185 $noteActions = FALSE;
187 $loggedInContactID = CRM_Core_Session
::singleton()->getLoggedInContactID();
188 if ($loggedInContactID) {
189 if ($loggedInContactID == $note->entity_id
) {
192 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
197 $recentOther = array();
199 $recentOther = array(
200 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
201 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
203 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
204 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
209 // add the recently created Note
210 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
211 CRM_Utils_System
::url('civicrm/contact/view/note',
212 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
226 * Check if there is data to create the object.
228 * @param array $params
229 * (reference ) an assoc array of name/value pairs.
233 public static function dataExists(&$params) {
234 // return if no data present
235 if (empty($params['id']) && !strlen($params['note'])) {
242 * Given the list of params in the params array, fetch the object
243 * and store the values in the values array
245 * @param array $params
246 * Input parameters to find object.
247 * @param array $values
248 * Output values of the object.
249 * @param int $numNotes
250 * The maximum number of notes to return (0 if all).
253 * $notes Object of CRM_Core_BAO_Note
255 public static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
256 if (empty($params)) {
259 $note = new CRM_Core_BAO_Note();
260 $note->entity_id
= $params['contact_id'];
261 $note->entity_table
= 'civicrm_contact';
263 // get the total count of notes
264 $values['noteTotalCount'] = $note->count();
266 // get only 3 recent notes
267 $note->orderBy('modified_date desc');
268 $note->limit($numNotes);
273 while ($note->fetch()) {
274 $values['note'][$note->id
] = array();
275 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
279 // if we have collected the number of notes, exit loop
280 if ($numNotes > 0 && $count >= $numNotes) {
293 * @param bool $showStatus
294 * Do we need to set status or not.
297 * no of deleted notes on success, null otherwise
299 public static function del($id, $showStatus = TRUE) {
301 $recent = array($id);
302 $note = new CRM_Core_DAO_Note();
306 if ($note->entity_table
== 'civicrm_note') {
307 $status = ts('Selected Comment has been deleted successfully.');
310 $status = ts('Selected Note has been deleted successfully.');
313 // Delete all descendents of this Note
314 foreach (self
::getDescendentIds($id) as $childId) {
315 $childNote = new CRM_Core_DAO_Note();
316 $childNote->id
= $childId;
317 $childNote->delete();
318 $recent[] = $childId;
321 $return = $note->delete();
323 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
326 // delete the recently created Note
327 foreach ($recent as $recentId) {
332 CRM_Utils_Recent
::del($noteRecent);
338 * Delete all records for this contact id.
341 * ID of the contact for which note needs to be deleted.
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;
350 // and the creator contact id
351 $dao = new CRM_Core_DAO_Note();
352 $dao->contact_id
= $id;
357 * Retrieve all records for this entity-id
360 * ID of the relationship for which records needs to be retrieved.
362 * @param string $entityTable
365 * array of note properties
368 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
375 WHERE entity_table=\"{$entityTable}\"
378 ORDER BY modified_date desc";
379 $params = array(1 => array($id, 'Integer'));
381 $dao = CRM_Core_DAO
::executeQuery($query, $params);
383 while ($dao->fetch()) {
384 $viewNote[$dao->id
] = $dao->note
;
391 * Get log record count for a Contact.
393 * @param int $contactID
396 * $count count of log records
399 public static function getContactNoteCount($contactID) {
400 $note = new CRM_Core_DAO_Note();
401 $note->entity_id
= $contactID;
402 $note->entity_table
= 'civicrm_contact';
405 while ($note->fetch()) {
406 if (!self
::getNotePrivacyHidden($note)) {
414 * Get all descendent notes of the note with given ID.
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.
424 * Nested associative array beginning with direct children of given note.
427 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
428 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
432 * Get total count of direct children visible to the current user.
438 * $count Number of notes having the give note as parent
441 public static function getChildCount($id) {
442 $note = new CRM_Core_DAO_Note();
443 $note->entity_table
= 'civicrm_note';
444 $note->entity_id
= $id;
447 while ($note->fetch()) {
448 if (!self
::getNotePrivacyHidden($note)) {
456 * Recursive function to get all descendent notes of the note with given ID.
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.
465 * (Reference) Variable to store all found descendents.
467 * Depth of current iteration within the descendent tree (used for comparison against maxDepth).
470 * Nested associative array beginning with direct children of given note.
472 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) {
473 if ($maxDepth && $depth > $maxDepth) {
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');
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
]);
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;
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']);
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) : '';
503 $tree[$note->id
]['note'] = nl2br($tree[$note->id
]['note']);
504 $tree[$note->id
]['note'] = smarty_modifier_mb_truncate(
505 $tree[$note->id
]['note'],
510 CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
516 $tree[$note->id
]['child'],
526 * Given a note id, get a list of the ids of all notes that are descendents of that note
528 * @param int $parentId
529 * Id of the given note.
531 * (reference) one-dimensional array to store found descendent ids.
534 * One-dimensional array containing ids of all desendent notes
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;
542 while ($note->fetch()) {
543 // foreach child, add to ids list, and recurse
545 self
::getDescendentIds($note->id
, $ids);
551 * Delete all note related to contact when contact is deleted.
553 * @param int $contactID
554 * Contact id whose notes to be deleted.
556 public static function cleanContactNotes($contactID) {
557 $params = array(1 => array($contactID, 'Integer'));
559 // delete all notes related to contribution
560 $contributeQuery = "DELETE note.*
561 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
562 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
564 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
566 // delete all notes related to participant
567 $participantQuery = "DELETE note.*
568 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
569 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
571 CRM_Core_DAO
::executeQuery($participantQuery, $params);
573 // delete all contact notes
574 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
576 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
577 while ($contactNoteId->fetch()) {
578 self
::del($contactNoteId->id
, FALSE);
583 * Whitelist of possible values for the entity_table field
586 public static function entityTables() {
588 'civicrm_relationship' => 'Relationship',
589 'civicrm_contact' => 'Contact',
590 'civicrm_participant' => 'Participant',
591 'civicrm_contribution' => 'Contribution',