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
50 * @param int $id id of the note to retrieve
52 * @return string the note text or null if note not found
56 public static function getNoteText($id) {
57 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
61 * Given a note id, retrieve the note subject
63 * @param int $id id of the note to retrieve
65 * @return string 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
76 * @param object $note Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself
78 * @return boolean TRUE if the note should be displayed, otherwise FALSE
82 public static function getNotePrivacyHidden($note) {
83 if (CRM_Core_Permission
::check('view all notes')) {
87 $noteValues = array();
88 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
89 CRM_Core_DAO
::storeValues($note, $noteValues);
92 $noteDAO = new CRM_Core_DAO_Note();
95 if ($noteDAO->fetch()) {
96 CRM_Core_DAO
::storeValues($noteDAO, $noteValues);
100 CRM_Utils_Hook
::notePrivacy($noteValues);
102 if (!$noteValues['privacy']) {
105 elseif (isset($noteValues['notePrivacy_hidden'])) {
106 // If the hook has set visibility, use that setting.
107 return $noteValues['notePrivacy_hidden'];
110 // Default behavior (if hook has not set visibility)
111 // is to hide privacy notes unless the note creator is the current user.
113 if ($noteValues['privacy']) {
114 $session = CRM_Core_Session
::singleton();
115 $userID = $session->get('userID');
116 return ($noteValues['contact_id'] != $userID);
125 * Takes an associative array and creates a note object
127 * the function extract all the params it needs to initialize the create a
128 * note object. the params array could contain additional unused name/value
131 * @param array $params (reference) an assoc array of name/value pairs
132 * @param array $ids (deprecated) associated array with note id - preferably set $params['id']
134 * @return object $note CRM_Core_BAO_Note object
137 public static function &add(&$params, $ids = array()) {
138 $dataExists = self
::dataExists($params);
140 return CRM_Core_DAO
::$_nullObject;
143 $note = new CRM_Core_BAO_Note();
145 if (!isset($params['modified_date'])) {
146 $params['modified_date'] = date("Ymd");
149 if (!isset($params['privacy'])) {
150 $params['privacy'] = 0;
153 $note->copyValues($params);
154 if (empty($params['contact_id'])) {
155 if ($params['entity_table'] == 'civicrm_contact') {
156 $note->contact_id
= $params['entity_id'];
159 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
166 // check and attach and files as needed
167 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_note', $note->id
);
169 if ($note->entity_table
== 'civicrm_contact') {
170 CRM_Core_BAO_Log
::register($note->entity_id
,
174 $displayName = CRM_Contact_BAO_Contact
::displayName($note->entity_id
);
176 $noteActions = FALSE;
177 $session = CRM_Core_Session
::singleton();
178 if ($session->get('userID')) {
179 if ($session->get('userID') == $note->entity_id
) {
182 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
187 $recentOther = array();
189 $recentOther = array(
190 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
191 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
193 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
194 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
199 // add the recently created Note
200 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
201 CRM_Utils_System
::url('civicrm/contact/view/note',
202 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
216 * Check if there is data to create the object
218 * @param array $params (reference ) an assoc array of name/value pairs
223 public static function dataExists(&$params) {
224 // return if no data present
225 if (!strlen($params['note'])) {
232 * Given the list of params in the params array, fetch the object
233 * and store the values in the values array
235 * @param array $params input parameters to find object
236 * @param array $values output values of the object
237 * @param int $numNotes the maximum number of notes to return (0 if all)
239 * @return object $notes Object of CRM_Core_BAO_Note
242 public static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
243 if (empty($params)) {
246 $note = new CRM_Core_BAO_Note();
247 $note->entity_id
= $params['contact_id'];
248 $note->entity_table
= 'civicrm_contact';
250 // get the total count of notes
251 $values['noteTotalCount'] = $note->count();
253 // get only 3 recent notes
254 $note->orderBy('modified_date desc');
255 $note->limit($numNotes);
260 while ($note->fetch()) {
261 $values['note'][$note->id
] = array();
262 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
266 // if we have collected the number of notes, exit loop
267 if ($numNotes > 0 && $count >= $numNotes) {
278 * @param int $id note id
279 * @param boolean $showStatus do we need to set status or not
281 * @return mixed|null $return no of deleted notes on success, false otherwise@access public
284 public static function del($id, $showStatus = TRUE) {
286 $recent = array($id);
287 $note = new CRM_Core_DAO_Note();
291 if ($note->entity_table
== 'civicrm_note') {
292 $status = ts('Selected Comment has been deleted successfully.');
295 $status = ts('Selected Note has been deleted successfully.');
298 // Delete all descendents of this Note
299 foreach (self
::getDescendentIds($id) as $childId) {
300 $childNote = new CRM_Core_DAO_Note();
301 $childNote->id
= $childId;
302 $childNote->delete();
304 $recent[] = $childId;
307 $return = $note->delete();
310 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
313 // delete the recently created Note
314 foreach ($recent as $recentId) {
319 CRM_Utils_Recent
::del($noteRecent);
325 * Delete all records for this contact id
327 * @param int $id ID of the contact for which note needs to be deleted.
333 public static function deleteContact($id) {
334 // need to delete for both entity_id
335 $dao = new CRM_Core_DAO_Note();
336 $dao->entity_table
= 'civicrm_contact';
337 $dao->entity_id
= $id;
340 // and the creator contact id
341 $dao = new CRM_Core_DAO_Note();
342 $dao->contact_id
= $id;
347 * Retrieve all records for this entity-id
349 * @param int $id ID of the relationship for which records needs to be retrieved.
351 * @param string $entityTable
353 * @return array $viewNote array of note properties
357 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
364 WHERE entity_table=\"{$entityTable}\"
367 ORDER BY modified_date desc";
368 $params = array(1 => array($id, 'Integer'));
370 $dao = CRM_Core_DAO
::executeQuery($query, $params);
372 while ($dao->fetch()) {
373 $viewNote[$dao->id
] = $dao->note
;
380 * Get log record count for a Contact
382 * @param int $contactID
384 * @return int $count count of log records
388 public static function getContactNoteCount($contactID) {
389 $note = new CRM_Core_DAO_Note();
390 $note->entity_id
= $contactID;
391 $note->entity_table
= 'civicrm_contact';
394 while ($note->fetch()) {
395 if (!self
::getNotePrivacyHidden($note)) {
403 * Get all descendent notes of the note with given ID
405 * @param int $parentId ID of the note to start from
406 * @param int $maxDepth Maximum number of levels to descend into the tree; if not given, will include all descendents.
407 * @param bool $snippet If TRUE, returned values will be pre-formatted for display in a table of notes.
409 * @return array Nested associative array beginning with direct children of given note.
413 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
414 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
418 * Get total count of direct children visible to the current user
420 * @param int $id Note ID
422 * @return int $count Number of notes having the give note as parent
426 public static function getChildCount($id) {
427 $note = new CRM_Core_DAO_Note();
428 $note->entity_table
= 'civicrm_note';
429 $note->entity_id
= $id;
432 while ($note->fetch()) {
433 if (!self
::getNotePrivacyHidden($note)) {
441 * Recursive function to get all descendent notes of the note with given ID
443 * @param int $parentId ID of the note to start from
444 * @param int $maxDepth Maximum number of levels to descend into the tree; if not given, will include all descendents.
445 * @param bool $snippet If TRUE, returned values will be pre-formatted for display in a table of notes.
446 * @param array $tree (Reference) Variable to store all found descendents
447 * @param int $depth Depth of current iteration within the descendent tree (used for comparison against maxDepth)
449 * @return array 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 Id of the given note
509 * @param array $ids (reference) one-dimensional array to store found descendent ids
511 * @return array $ids One-dimensional array containing ids of all desendent notes
513 public static function getDescendentIds($parentId, &$ids = array()) {
514 // get direct children of given parentId note
515 $note = new CRM_Core_DAO_Note();
516 $note->entity_table
= 'civicrm_note';
517 $note->entity_id
= $parentId;
519 while ($note->fetch()) {
520 // foreach child, add to ids list, and recurse
522 self
::getDescendentIds($note->id
, $ids);
528 * Delete all note related to contact when contact is deleted
530 * @param int $contactID contact id whose notes to be deleted
535 public static function cleanContactNotes($contactID) {
536 $params = array(1 => array($contactID, 'Integer'));
538 // delete all notes related to contribution
539 $contributeQuery = "DELETE note.*
540 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
541 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
543 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
545 // delete all notes related to participant
546 $participantQuery = "DELETE note.*
547 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
548 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
550 CRM_Core_DAO
::executeQuery($participantQuery, $params);
552 // delete all contact notes
553 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
555 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
556 while ($contactNoteId->fetch()) {
557 self
::del($contactNoteId->id
, FALSE);