3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
57 static function getNoteText($id) {
58 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
62 * given a note id, retrieve the note subject
64 * @param int $id id of the note to retrieve
66 * @return string the note subject or null if note not found
71 static function getNoteSubject($id) {
72 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $id, 'subject');
76 * given a note id, decide if the note should be displayed based on privacy setting
78 * @param object $note Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself
80 * @return boolean TRUE if the note should be displayed, otherwise FALSE
85 static function getNotePrivacyHidden($note) {
86 if (CRM_Core_Permission
::check('view all notes')) {
90 $noteValues = array();
91 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
92 CRM_Core_DAO
::storeValues($note, $noteValues);
95 $noteDAO = new CRM_Core_DAO_Note();
98 if ($noteDAO->fetch()) {
99 CRM_Core_DAO
::storeValues($noteDAO, $noteValues);
103 CRM_Utils_Hook
::notePrivacy($noteValues);
105 if (!$noteValues['privacy']) {
108 elseif (isset($noteValues['notePrivacy_hidden'])) {
109 // If the hook has set visibility, use that setting.
110 return $noteValues['notePrivacy_hidden'];
113 // Default behavior (if hook has not set visibility)
114 // is to hide privacy notes unless the note creator is the current user.
116 if ($noteValues['privacy']) {
117 $session = CRM_Core_Session
::singleton();
118 $userID = $session->get('userID');
119 return ($noteValues['contact_id'] != $userID);
128 * takes an associative array and creates a note object
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
134 * @param array $params (reference) an assoc array of name/value pairs
135 * @param array $ids (deprecated) associated array with note id - preferably set $params['id']
137 * @return object $note CRM_Core_BAO_Note object
141 static function &add(&$params, $ids = array()) {
142 $dataExists = self
::dataExists($params);
144 return CRM_Core_DAO
::$_nullObject;
147 $note = new CRM_Core_BAO_Note();
149 if (!isset($params['modified_date'])) {
150 $params['modified_date'] = date("Ymd");
153 if (!isset($params['privacy'])) {
154 $params['privacy'] = 0;
157 $note->copyValues($params);
158 if (!CRM_Utils_Array
::value('contact_id', $params)) {
159 if ($params['entity_table'] == 'civicrm_contact') {
160 $note->contact_id
= $params['entity_id'];
163 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
170 // check and attach and files as needed
171 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_note', $note->id
);
173 if ($note->entity_table
== 'civicrm_contact') {
174 CRM_Core_BAO_Log
::register($note->entity_id
,
178 $displayName = CRM_Contact_BAO_Contact
::displayName($note->entity_id
);
180 $noteActions = FALSE;
181 $session = CRM_Core_Session
::singleton();
182 if ($session->get('userID')) {
183 if ($session->get('userID') == $note->entity_id
) {
186 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
191 $recentOther = array();
193 $recentOther = array(
194 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
195 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
197 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
198 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
203 // add the recently created Note
204 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
205 CRM_Utils_System
::url('civicrm/contact/view/note',
206 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
220 * Check if there is data to create the object
222 * @param array $params (reference ) an assoc array of name/value pairs
228 static function dataExists(&$params) {
229 // return if no data present
230 if (!strlen($params['note'])) {
237 * Given the list of params in the params array, fetch the object
238 * and store the values in the values array
240 * @param array $params input parameters to find object
241 * @param array $values output values of the object
242 * @param array $ids the array that holds all the db ids
243 * @param int $numNotes the maximum number of notes to return (0 if all)
245 * @return object $notes Object of CRM_Core_BAO_Note
249 static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
250 if (empty($params)) {
253 $note = new CRM_Core_BAO_Note();
254 $note->entity_id
= $params['contact_id'];
255 $note->entity_table
= 'civicrm_contact';
257 // get the total count of notes
258 $values['noteTotalCount'] = $note->count();
260 // get only 3 recent notes
261 $note->orderBy('modified_date desc');
262 $note->limit($numNotes);
267 while ($note->fetch()) {
268 $values['note'][$note->id
] = array();
269 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
273 // if we have collected the number of notes, exit loop
274 if ($numNotes > 0 && $count >= $numNotes) {
283 * Function to delete the notes
285 * @param int $id note id
286 * @param boolean $showStatus do we need to set status or not
288 * @return $return no of deleted notes on success, false otherwise
293 static function del($id, $showStatus = TRUE) {
295 $recent = array($id);
296 $note = new CRM_Core_DAO_Note();
300 if ($note->entity_table
== 'civicrm_note') {
301 $status = ts('Selected Comment has been deleted successfully.');
304 $status = ts('Selected Note has been deleted successfully.');
307 // Delete all descendents of this Note
308 foreach (self
::getDescendentIds($id) as $childId) {
309 $childNote = new CRM_Core_DAO_Note();
310 $childNote->id
= $childId;
311 $childNote->delete();
313 $recent[] = $childId;
316 $return = $note->delete();
319 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
322 // delete the recently created Note
323 foreach ($recent as $recentId) {
328 CRM_Utils_Recent
::del($noteRecent);
334 * delete all records for this contact id
336 * @param int $id 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
359 * @param int $id ID of the relationship for which records needs to be retrieved.
361 * @return array $viewNote array of note properties
366 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
373 WHERE entity_table=\"{$entityTable}\"
376 ORDER BY modified_date desc";
377 $params = array(1 => array($id, 'Integer'));
379 $dao = CRM_Core_DAO
::executeQuery($query, $params);
381 while ($dao->fetch()) {
382 $viewNote[$dao->id
] = $dao->note
;
389 * Function to get log record count for a Contact
391 * @param int $contactId Contact ID
393 * @return int $count count of log records
398 static function getContactNoteCount($contactID) {
399 $note = new CRM_Core_DAO_Note();
400 $note->entity_id
= $contactID;
401 $note->entity_table
= 'civicrm_contact';
404 while ($note->fetch()) {
405 if (!self
::getNotePrivacyHidden($note)) {
413 * Function to get all descendent notes of the note with given ID
415 * @param int $parentId ID of the note to start from
416 * @param int $maxDepth Maximum number of levels to descend into the tree; if not given, will include all descendents.
417 * @param bool $snippet If TRUE, returned values will be pre-formatted for display in a table of notes.
419 * @return array Nested associative array beginning with direct children of given note.
424 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
425 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
429 * Get total count of direct children visible to the current user
431 * @param int $id Note ID
433 * @return int $count Number of notes having the give note as parent
438 public static function getChildCount($id) {
439 $note = new CRM_Core_DAO_Note();
440 $note->entity_table
= 'civicrm_note';
441 $note->entity_id
= $id;
444 while ($note->fetch()) {
445 if (!self
::getNotePrivacyHidden($note)) {
453 * Recursive function to get all descendent notes of the note with given ID
455 * @param int $parentId ID of the note to start from
456 * @param int $maxDepth Maximum number of levels to descend into the tree; if not given, will include all descendents.
457 * @param bool $snippet If TRUE, returned values will be pre-formatted for display in a table of notes.
458 * @param array $tree (Reference) Variable to store all found descendents
459 * @param int $depth Depth of current iteration within the descendent tree (used for comparison against maxDepth)
461 * @return array Nested associative array beginning with direct children of given note.
464 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(
466 if ($maxDepth && $depth > $maxDepth) {
470 // get direct children of given parentId note
471 $note = new CRM_Core_DAO_Note();
472 $note->entity_table
= 'civicrm_note';
473 $note->entity_id
= $parentId;
474 $note->orderBy('modified_date asc');
476 while ($note->fetch()) {
477 // foreach child, call this function, unless the child is private/hidden
478 if (!self
::getNotePrivacyHidden($note)) {
479 CRM_Core_DAO
::storeValues($note, $tree[$note->id
]);
481 // get name of user that created this note
482 $contact = new CRM_Contact_DAO_Contact();
483 $createdById = $note->contact_id
;
484 $contact->id
= $createdById;
487 $tree[$note->id
]['createdBy'] = $contact->display_name
;
488 $tree[$note->id
]['createdById'] = $createdById;
489 $tree[$note->id
]['modified_date'] = CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
492 $tree[$note->id
]['note'] = nl2br($tree[$note->id
]['note']);
493 $tree[$note->id
]['note'] = smarty_modifier_mb_truncate(
494 $tree[$note->id
]['note'],
499 CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
505 $tree[$note->id
]['child'],
515 * given a note id, get a list of the ids of all notes that are descendents of that note
517 * @param int $parentId Id of the given note
518 * @param array $ids (reference) one-dimensional array to store found descendent ids
520 * @return array $ids One-dimensional array containing ids of all desendent notes
522 public static function getDescendentIds($parentId, &$ids = array(
524 // get direct children of given parentId note
525 $note = new CRM_Core_DAO_Note();
526 $note->entity_table
= 'civicrm_note';
527 $note->entity_id
= $parentId;
529 while ($note->fetch()) {
530 // foreach child, add to ids list, and recurse
532 self
::getDescendentIds($note->id
, $ids);
538 * function to delete all note related to contact when contact is deleted
540 * @param int $contactID contact id whose notes to be deleted
541 * @param array $deleteNoteID to store all deleted note ids
546 static function cleanContactNotes($contactID) {
547 $params = array(1 => array($contactID, 'Integer'));
549 // delete all notes related to contribution
550 $contributeQuery = "DELETE note.*
551 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
552 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
554 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
556 // delete all notes related to participant
557 $participantQuery = "DELETE note.*
558 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
559 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
561 CRM_Core_DAO
::executeQuery($participantQuery, $params);
563 // delete all contact notes
564 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
566 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
567 while ($contactNoteId->fetch()) {
568 self
::del($contactNoteId->id
, FALSE);