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.
53 * @return string the note text or NULL if note not found
57 public 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
65 * Id of the note to retrieve.
67 * @return string the note subject or NULL if note not found
71 public 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
79 * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself.
81 * @return boolean TRUE if the note should be displayed, otherwise FALSE
85 public 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
135 * (reference) an assoc array of name/value pairs.
137 * (deprecated) associated array with note id - preferably set $params['id'].
139 * @return object $note CRM_Core_BAO_Note object
142 public static function &add(&$params, $ids = array()) {
143 $dataExists = self
::dataExists($params);
145 return CRM_Core_DAO
::$_nullObject;
148 $note = new CRM_Core_BAO_Note();
150 if (!isset($params['modified_date'])) {
151 $params['modified_date'] = date("Ymd");
154 if (!isset($params['privacy'])) {
155 $params['privacy'] = 0;
158 $note->copyValues($params);
159 if (empty($params['contact_id'])) {
160 if ($params['entity_table'] == 'civicrm_contact') {
161 $note->contact_id
= $params['entity_id'];
164 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('id', $ids));
171 // check and attach and files as needed
172 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_note', $note->id
);
174 if ($note->entity_table
== 'civicrm_contact') {
175 CRM_Core_BAO_Log
::register($note->entity_id
,
179 $displayName = CRM_Contact_BAO_Contact
::displayName($note->entity_id
);
181 $noteActions = FALSE;
182 $session = CRM_Core_Session
::singleton();
183 if ($session->get('userID')) {
184 if ($session->get('userID') == $note->entity_id
) {
187 elseif (CRM_Contact_BAO_Contact_Permission
::allow($note->entity_id
, CRM_Core_Permission
::EDIT
)) {
192 $recentOther = array();
194 $recentOther = array(
195 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
196 "reset=1&action=update&cid={$note->entity_id}&id={$note->id}&context=home"
198 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/note',
199 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
204 // add the recently created Note
205 CRM_Utils_Recent
::add($displayName . ' - ' . $note->subject
,
206 CRM_Utils_System
::url('civicrm/contact/view/note',
207 "reset=1&action=view&cid={$note->entity_id}&id={$note->id}&context=home"
221 * Check if there is data to create the object
223 * @param array $params
224 * (reference ) an assoc array of name/value pairs.
229 public static function dataExists(&$params) {
230 // return if no data present
231 if (!strlen($params['note'])) {
238 * Given the list of params in the params array, fetch the object
239 * and store the values in the values array
241 * @param array $params
242 * Input parameters to find object.
243 * @param array $values
244 * Output values of the object.
245 * @param int $numNotes
246 * The maximum number of notes to return (0 if all).
248 * @return object $notes Object of CRM_Core_BAO_Note
251 public static function &getValues(&$params, &$values, $numNotes = self
::MAX_NOTES
) {
252 if (empty($params)) {
255 $note = new CRM_Core_BAO_Note();
256 $note->entity_id
= $params['contact_id'];
257 $note->entity_table
= 'civicrm_contact';
259 // get the total count of notes
260 $values['noteTotalCount'] = $note->count();
262 // get only 3 recent notes
263 $note->orderBy('modified_date desc');
264 $note->limit($numNotes);
269 while ($note->fetch()) {
270 $values['note'][$note->id
] = array();
271 CRM_Core_DAO
::storeValues($note, $values['note'][$note->id
]);
275 // if we have collected the number of notes, exit loop
276 if ($numNotes > 0 && $count >= $numNotes) {
289 * @param bool $showStatus
290 * Do we need to set status or not.
292 * @return mixed|NULL $return no of deleted notes on success, FALSE otherwise@access public
295 public static function del($id, $showStatus = TRUE) {
297 $recent = array($id);
298 $note = new CRM_Core_DAO_Note();
302 if ($note->entity_table
== 'civicrm_note') {
303 $status = ts('Selected Comment has been deleted successfully.');
306 $status = ts('Selected Note has been deleted successfully.');
309 // Delete all descendents of this Note
310 foreach (self
::getDescendentIds($id) as $childId) {
311 $childNote = new CRM_Core_DAO_Note();
312 $childNote->id
= $childId;
313 $childNote->delete();
315 $recent[] = $childId;
318 $return = $note->delete();
321 CRM_Core_Session
::setStatus($status, ts('Deleted'), 'success');
324 // delete the recently created Note
325 foreach ($recent as $recentId) {
330 CRM_Utils_Recent
::del($noteRecent);
336 * Delete all records for this contact id
339 * ID of the contact for which note needs to be deleted.
345 public static function deleteContact($id) {
346 // need to delete for both entity_id
347 $dao = new CRM_Core_DAO_Note();
348 $dao->entity_table
= 'civicrm_contact';
349 $dao->entity_id
= $id;
352 // and the creator contact id
353 $dao = new CRM_Core_DAO_Note();
354 $dao->contact_id
= $id;
359 * Retrieve all records for this entity-id
362 * ID of the relationship for which records needs to be retrieved.
364 * @param string $entityTable
366 * @return array $viewNote array of note properties
370 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
377 WHERE entity_table=\"{$entityTable}\"
380 ORDER BY modified_date desc";
381 $params = array(1 => array($id, 'Integer'));
383 $dao = CRM_Core_DAO
::executeQuery($query, $params);
385 while ($dao->fetch()) {
386 $viewNote[$dao->id
] = $dao->note
;
393 * Get log record count for a Contact
395 * @param int $contactID
397 * @return int $count count of log records
401 public static function getContactNoteCount($contactID) {
402 $note = new CRM_Core_DAO_Note();
403 $note->entity_id
= $contactID;
404 $note->entity_table
= 'civicrm_contact';
407 while ($note->fetch()) {
408 if (!self
::getNotePrivacyHidden($note)) {
416 * Get all descendent notes of the note with given ID
418 * @param int $parentId
419 * ID of the note to start from.
420 * @param int $maxDepth
421 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
422 * @param bool $snippet
423 * If TRUE, returned values will be pre-formatted for display in a table of notes.
425 * @return array Nested associative array beginning with direct children of given note.
429 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
430 return self
::buildNoteTree($parentId, $maxDepth, $snippet);
434 * Get total count of direct children visible to the current user
439 * @return int $count Number of notes having the give note as parent
443 public static function getChildCount($id) {
444 $note = new CRM_Core_DAO_Note();
445 $note->entity_table
= 'civicrm_note';
446 $note->entity_id
= $id;
449 while ($note->fetch()) {
450 if (!self
::getNotePrivacyHidden($note)) {
458 * Recursive function to get all descendent notes of the note with given ID
460 * @param int $parentId
461 * ID of the note to start from.
462 * @param int $maxDepth
463 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
464 * @param bool $snippet
465 * If TRUE, returned values will be pre-formatted for display in a table of notes.
467 * (Reference) Variable to store all found descendents.
469 * Depth of current iteration within the descendent tree (used for comparison against maxDepth).
471 * @return array Nested associative array beginning with direct children of given note.
474 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) {
475 if ($maxDepth && $depth > $maxDepth) {
479 // get direct children of given parentId note
480 $note = new CRM_Core_DAO_Note();
481 $note->entity_table
= 'civicrm_note';
482 $note->entity_id
= $parentId;
483 $note->orderBy('modified_date asc');
485 while ($note->fetch()) {
486 // foreach child, call this function, unless the child is private/hidden
487 if (!self
::getNotePrivacyHidden($note)) {
488 CRM_Core_DAO
::storeValues($note, $tree[$note->id
]);
490 // get name of user that created this note
491 $contact = new CRM_Contact_DAO_Contact();
492 $createdById = $note->contact_id
;
493 $contact->id
= $createdById;
496 $tree[$note->id
]['createdBy'] = $contact->display_name
;
497 $tree[$note->id
]['createdById'] = $createdById;
498 $tree[$note->id
]['modified_date'] = CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
500 // paper icon view for attachments part
501 $paperIconAttachmentInfo = CRM_Core_BAO_File
::paperIconAttachment('civicrm_note', $note->id
);
502 $tree[$note->id
]['attachment'] = $paperIconAttachmentInfo ?
implode('', $paperIconAttachmentInfo) : '';
505 $tree[$note->id
]['note'] = nl2br($tree[$note->id
]['note']);
506 $tree[$note->id
]['note'] = smarty_modifier_mb_truncate(
507 $tree[$note->id
]['note'],
512 CRM_Utils_Date
::customFormat($tree[$note->id
]['modified_date']);
518 $tree[$note->id
]['child'],
528 * Given a note id, get a list of the ids of all notes that are descendents of that note
530 * @param int $parentId
531 * Id of the given note.
533 * (reference) one-dimensional array to store found descendent ids.
535 * @return array $ids One-dimensional array containing ids of all desendent notes
537 public static function getDescendentIds($parentId, &$ids = array()) {
538 // get direct children of given parentId note
539 $note = new CRM_Core_DAO_Note();
540 $note->entity_table
= 'civicrm_note';
541 $note->entity_id
= $parentId;
543 while ($note->fetch()) {
544 // foreach child, add to ids list, and recurse
546 self
::getDescendentIds($note->id
, $ids);
552 * Delete all note related to contact when contact is deleted
554 * @param int $contactID
555 * Contact id whose notes to be deleted.
560 public static function cleanContactNotes($contactID) {
561 $params = array(1 => array($contactID, 'Integer'));
563 // delete all notes related to contribution
564 $contributeQuery = "DELETE note.*
565 FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
566 WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
568 CRM_Core_DAO
::executeQuery($contributeQuery, $params);
570 // delete all notes related to participant
571 $participantQuery = "DELETE note.*
572 FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
573 WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
575 CRM_Core_DAO
::executeQuery($participantQuery, $params);
577 // delete all contact notes
578 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
580 $contactNoteId = CRM_Core_DAO
::executeQuery($contactQuery, $params);
581 while ($contactNoteId->fetch()) {
582 self
::del($contactNoteId->id
, FALSE);