3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * BAO object for crm_note table
39 class CRM_Core_BAO_Note
extends CRM_Core_DAO_Note
{
42 * Const the max number of notes we display at any given time
48 * Given a note id, retrieve the note text
51 * Id of the note to retrieve.
54 * the note text or NULL if note not found
58 public static function getNoteText($id) {
59 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
63 * Given a note id, retrieve the note subject
66 * Id of the note to retrieve.
69 * the note subject or NULL if note not found
73 public static function getNoteSubject($id) {
74 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'subject');
78 * Given a note id, decide if the note should be displayed based on privacy setting
81 * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself.
84 * TRUE if the note should be displayed, otherwise FALSE
88 public static function getNotePrivacyHidden($note) {
89 if (CRM_Core_Permission
::check('view all notes')) {
93 $noteValues = array();
94 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
95 CRM_Core_DAO
::storeValues($note, $noteValues);
98 $noteDAO = new CRM_Core_DAO_Note();
101 if ($noteDAO->fetch()) {
102 CRM_Core_DAO
::storeValues($noteDAO, $noteValues);
106 CRM_Utils_Hook
::notePrivacy($noteValues);
108 if (!$noteValues['privacy']) {
111 elseif (isset($noteValues['notePrivacy_hidden'])) {
112 // If the hook has set visibility, use that setting.
113 return $noteValues['notePrivacy_hidden'];
116 // Default behavior (if hook has not set visibility)
117 // is to hide privacy notes unless the note creator is the current user.
119 if ($noteValues['privacy']) {
120 $session = CRM_Core_Session
::singleton();
121 $userID = $session->get('userID');
122 return ($noteValues['contact_id'] != $userID);
131 * Takes an associative array and creates a note object
133 * the function extract all the params it needs to initialize the create a
134 * note object. the params array could contain additional unused name/value
137 * @param array $params
138 * (reference) an assoc array of name/value pairs.
140 * (deprecated) associated array with note id - preferably set $params['id'].
143 * $note CRM_Core_BAO_Note object
146 public static function &add(&$params, $ids = array()) {
147 $dataExists = self
::dataExists($params);
149 return CRM_Core_DAO
::$_nullObject;
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 ($params['entity_table'] == '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;
186 $session = CRM_Core_Session
::singleton();
187 if ($session->get('userID')) {
188 if ($session->get('userID') == $note->entity_id
) {
191 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
196 $recentOther = array();
198 $recentOther = array(
199 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
200 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
202 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
203 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
208 // add the recently created Note
209 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
210 CRM_Utils_System
::url('civicrm/contact/view/note',
211 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
225 * Check if there is data to create the object
227 * @param array $params
228 * (reference ) an assoc array of name/value pairs.
233 public static function dataExists(&$params) {
234 // return if no data present
235 if (!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
256 public static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
257 if (empty($params)) {
260 $note = new CRM_Core_BAO_Note();
261 $note->entity_id
= $params['contact_id'];
262 $note->entity_table
= 'civicrm_contact';
264 // get the total count of notes
265 $values['noteTotalCount'] = $note->count();
267 // get only 3 recent notes
268 $note->orderBy('modified_date desc');
269 $note->limit($numNotes);
274 while ($note->fetch()) {
275 $values['note'][$note->id
] = array();
276 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
280 // if we have collected the number of notes, exit loop
281 if ($numNotes > 0 && $count >= $numNotes) {
294 * @param bool $showStatus
295 * Do we need to set status or not.
297 * @return mixed|NULL $return no of deleted notes on success, FALSE otherwise
301 public static function del($id, $showStatus = TRUE) {
303 $recent = array($id);
304 $note = new CRM_Core_DAO_Note();
308 if ($note->entity_table
== 'civicrm_note') {
309 $status = ts('Selected Comment has been deleted successfully.');
312 $status = ts('Selected Note has been deleted successfully.');
315 // Delete all descendents of this Note
316 foreach (self
::getDescendentIds($id) as $childId) {
317 $childNote = new CRM_Core_DAO_Note();
318 $childNote->id
= $childId;
319 $childNote->delete();
321 $recent[] = $childId;
324 $return = $note->delete();
327 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
330 // delete the recently created Note
331 foreach ($recent as $recentId) {
336 CRM_Utils_Recent
::del($noteRecent);
342 * Delete all records for this contact id
345 * ID of the contact for which note needs to be deleted.
351 public static function deleteContact($id) {
352 // need to delete for both entity_id
353 $dao = new CRM_Core_DAO_Note();
354 $dao->entity_table
= 'civicrm_contact';
355 $dao->entity_id
= $id;
358 // and the creator contact id
359 $dao = new CRM_Core_DAO_Note();
360 $dao->contact_id
= $id;
365 * Retrieve all records for this entity-id
368 * ID of the relationship for which records needs to be retrieved.
370 * @param string $entityTable
373 * array of note properties
377 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
384 WHERE entity_table=\"{$entityTable}\"
387 ORDER BY modified_date desc";
388 $params = array(1 => array($id, 'Integer'));
390 $dao = CRM_Core_DAO
::executeQuery($query, $params);
392 while ($dao->fetch()) {
393 $viewNote[$dao->id
] = $dao->note
;
400 * Get log record count for a Contact
402 * @param int $contactID
405 * $count count of log records
409 public static function getContactNoteCount($contactID) {
410 $note = new CRM_Core_DAO_Note();
411 $note->entity_id
= $contactID;
412 $note->entity_table
= 'civicrm_contact';
415 while ($note->fetch()) {
416 if (!self
::getNotePrivacyHidden($note)) {
424 * Get all descendent notes of the note with given ID
426 * @param int $parentId
427 * ID of the note to start from.
428 * @param int $maxDepth
429 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
430 * @param bool $snippet
431 * If TRUE, returned values will be pre-formatted for display in a table of notes.
434 * Nested associative array beginning with direct children of given note.
438 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
439 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
443 * Get total count of direct children visible to the current user
449 * $count Number of notes having the give note as parent
453 public static function getChildCount($id) {
454 $note = new CRM_Core_DAO_Note();
455 $note->entity_table
= 'civicrm_note';
456 $note->entity_id
= $id;
459 while ($note->fetch()) {
460 if (!self
::getNotePrivacyHidden($note)) {
468 * Recursive function to get all descendent notes of the note with given ID
470 * @param int $parentId
471 * ID of the note to start from.
472 * @param int $maxDepth
473 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
474 * @param bool $snippet
475 * If TRUE, returned values will be pre-formatted for display in a table of notes.
477 * (Reference) Variable to store all found descendents.
479 * Depth of current iteration within the descendent tree (used for comparison against maxDepth).
482 * Nested associative array beginning with direct children of given note.
485 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) {
486 if ($maxDepth && $depth > $maxDepth) {
490 // get direct children of given parentId note
491 $note = new CRM_Core_DAO_Note();
492 $note->entity_table
= 'civicrm_note';
493 $note->entity_id
= $parentId;
494 $note->orderBy('modified_date asc');
496 while ($note->fetch()) {
497 // foreach child, call this function, unless the child is private/hidden
498 if (!self
::getNotePrivacyHidden($note)) {
499 CRM_Core_DAO
::storeValues($note, $tree[$note->id
]);
501 // get name of user that created this note
502 $contact = new CRM_Contact_DAO_Contact();
503 $createdById = $note->contact_id
;
504 $contact->id
= $createdById;
507 $tree[$note->id
]['createdBy'] = $contact->display_name
;
508 $tree[$note->id
]['createdById'] = $createdById;
509 $tree[$note->id
]['modified_date'] = CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
511 // paper icon view for attachments part
512 $paperIconAttachmentInfo = CRM_Core_BAO_File
::paperIconAttachment('civicrm_note', $note->id
);
513 $tree[$note->id
]['attachment'] = $paperIconAttachmentInfo ?
implode('', $paperIconAttachmentInfo) : '';
516 $tree[$note->id
]['note'] = nl2br($tree[$note->id
]['note']);
517 $tree[$note->id
]['note'] = smarty_modifier_mb_truncate(
518 $tree[$note->id
]['note'],
523 CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
529 $tree[$note->id
]['child'],
539 * Given a note id, get a list of the ids of all notes that are descendents of that note
541 * @param int $parentId
542 * Id of the given note.
544 * (reference) one-dimensional array to store found descendent ids.
547 * One-dimensional array containing ids of all desendent notes
549 public static function getDescendentIds($parentId, &$ids = array()) {
550 // get direct children of given parentId note
551 $note = new CRM_Core_DAO_Note();
552 $note->entity_table
= 'civicrm_note';
553 $note->entity_id
= $parentId;
555 while ($note->fetch()) {
556 // foreach child, add to ids list, and recurse
558 self
::getDescendentIds($note->id
, $ids);
564 * Delete all note related to contact when contact is deleted
566 * @param int $contactID
567 * Contact id whose notes to be deleted.
572 public static function cleanContactNotes($contactID) {
573 $params = array(1 => array($contactID, 'Integer'));
575 // delete all notes related to contribution
576 $contributeQuery = "DELETE note.*
577 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
578 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
580 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
582 // delete all notes related to participant
583 $participantQuery = "DELETE note.*
584 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
585 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
587 CRM_Core_DAO
::executeQuery($participantQuery, $params);
589 // delete all contact notes
590 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
592 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
593 while ($contactNoteId->fetch()) {
594 self
::del($contactNoteId->id
, FALSE);