INFRA-132 - Comment grammar cleanup
[civicrm-core.git] / CRM / Core / BAO / Note.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * BAO object for crm_note table
38 */
39class CRM_Core_BAO_Note extends CRM_Core_DAO_Note {
40
41 /**
100fef9d 42 * Const the max number of notes we display at any given time
6a488035
TO
43 * @var int
44 */
7da04cde 45 const MAX_NOTES = 3;
6a488035
TO
46
47 /**
100fef9d 48 * Given a note id, retrieve the note text
6a488035 49 *
6a0b768e
TO
50 * @param int $id
51 * Id of the note to retrieve.
6a488035 52 *
e60f24eb 53 * @return string the note text or NULL if note not found
6a488035 54 *
6a488035
TO
55 * @static
56 */
00be9182 57 public static function getNoteText($id) {
6a488035
TO
58 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $id, 'note');
59 }
60
61 /**
100fef9d 62 * Given a note id, retrieve the note subject
6a488035 63 *
6a0b768e
TO
64 * @param int $id
65 * Id of the note to retrieve.
6a488035 66 *
e60f24eb 67 * @return string the note subject or NULL if note not found
6a488035 68 *
6a488035
TO
69 * @static
70 */
00be9182 71 public static function getNoteSubject($id) {
6a488035
TO
72 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $id, 'subject');
73 }
74
75 /**
100fef9d 76 * Given a note id, decide if the note should be displayed based on privacy setting
6a488035 77 *
6a0b768e
TO
78 * @param object $note
79 * Either the id of the note to retrieve, or the CRM_Core_DAO_Note object itself.
6a488035
TO
80 *
81 * @return boolean TRUE if the note should be displayed, otherwise FALSE
82 *
6a488035
TO
83 * @static
84 */
00be9182 85 public static function getNotePrivacyHidden($note) {
6a488035
TO
86 if (CRM_Core_Permission::check('view all notes')) {
87 return FALSE;
88 }
89
90 $noteValues = array();
91 if (is_object($note) && get_class($note) == 'CRM_Core_DAO_Note') {
92 CRM_Core_DAO::storeValues($note, $noteValues);
93 }
94 else {
95 $noteDAO = new CRM_Core_DAO_Note();
96 $noteDAO->id = $note;
97 $noteDAO->find();
98 if ($noteDAO->fetch()) {
99 CRM_Core_DAO::storeValues($noteDAO, $noteValues);
100 }
101 }
102
103 CRM_Utils_Hook::notePrivacy($noteValues);
104
105 if (!$noteValues['privacy']) {
106 return FALSE;
107 }
108 elseif (isset($noteValues['notePrivacy_hidden'])) {
109 // If the hook has set visibility, use that setting.
110 return $noteValues['notePrivacy_hidden'];
111 }
112 else {
113 // Default behavior (if hook has not set visibility)
114 // is to hide privacy notes unless the note creator is the current user.
115
116 if ($noteValues['privacy']) {
117 $session = CRM_Core_Session::singleton();
118 $userID = $session->get('userID');
119 return ($noteValues['contact_id'] != $userID);
120 }
121 else {
122 return FALSE;
123 }
124 }
125 }
126
127 /**
100fef9d 128 * Takes an associative array and creates a note object
6a488035
TO
129 *
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
132 * pairs
133 *
6a0b768e
TO
134 * @param array $params
135 * (reference) an assoc array of name/value pairs.
136 * @param array $ids
137 * (deprecated) associated array with note id - preferably set $params['id'].
6a488035
TO
138 *
139 * @return object $note CRM_Core_BAO_Note object
6a488035
TO
140 * @static
141 */
00be9182 142 public static function &add(&$params, $ids = array()) {
6a488035
TO
143 $dataExists = self::dataExists($params);
144 if (!$dataExists) {
145 return CRM_Core_DAO::$_nullObject;
146 }
147
148 $note = new CRM_Core_BAO_Note();
149
150 if (!isset($params['modified_date'])) {
151 $params['modified_date'] = date("Ymd");
152 }
153
154 if (!isset($params['privacy'])) {
155 $params['privacy'] = 0;
156 }
157
158 $note->copyValues($params);
a7488080 159 if (empty($params['contact_id'])) {
6a488035
TO
160 if ($params['entity_table'] == 'civicrm_contact') {
161 $note->contact_id = $params['entity_id'];
162 }
163 }
5554f6f3 164 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
165 if ($id) {
166 $note->id = $id;
6a488035
TO
167 }
168
169 $note->save();
170
171 // check and attach and files as needed
172 CRM_Core_BAO_File::processAttachment($params, 'civicrm_note', $note->id);
173
174 if ($note->entity_table == 'civicrm_contact') {
175 CRM_Core_BAO_Log::register($note->entity_id,
176 'civicrm_note',
177 $note->id
178 );
179 $displayName = CRM_Contact_BAO_Contact::displayName($note->entity_id);
180
181 $noteActions = FALSE;
182 $session = CRM_Core_Session::singleton();
183 if ($session->get('userID')) {
184 if ($session->get('userID') == $note->entity_id) {
185 $noteActions = TRUE;
186 }
187 elseif (CRM_Contact_BAO_Contact_Permission::allow($note->entity_id, CRM_Core_Permission::EDIT)) {
188 $noteActions = TRUE;
189 }
190 }
191
192 $recentOther = array();
193 if ($noteActions) {
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"
197 ),
198 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/note',
199 "reset=1&action=delete&cid={$note->entity_id}&id={$note->id}&context=home"
200 ),
201 );
202 }
203
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"
208 ),
209 $note->id,
210 'Note',
211 $note->entity_id,
212 $displayName,
213 $recentOther
214 );
215 }
216
217 return $note;
218 }
219
220 /**
221 * Check if there is data to create the object
222 *
6a0b768e
TO
223 * @param array $params
224 * (reference ) an assoc array of name/value pairs.
6a488035
TO
225 *
226 * @return boolean
6a488035
TO
227 * @static
228 */
00be9182 229 public static function dataExists(&$params) {
6a488035
TO
230 // return if no data present
231 if (!strlen($params['note'])) {
232 return FALSE;
233 }
234 return TRUE;
235 }
236
237 /**
238 * Given the list of params in the params array, fetch the object
239 * and store the values in the values array
240 *
6a0b768e
TO
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).
6a488035
TO
247 *
248 * @return object $notes Object of CRM_Core_BAO_Note
6a488035
TO
249 * @static
250 */
00be9182 251 public static function &getValues(&$params, &$values, $numNotes = self::MAX_NOTES) {
6a488035
TO
252 if (empty($params)) {
253 return NULL;
254 }
255 $note = new CRM_Core_BAO_Note();
256 $note->entity_id = $params['contact_id'];
257 $note->entity_table = 'civicrm_contact';
258
259 // get the total count of notes
260 $values['noteTotalCount'] = $note->count();
261
262 // get only 3 recent notes
263 $note->orderBy('modified_date desc');
264 $note->limit($numNotes);
265 $note->find();
266
267 $notes = array();
268 $count = 0;
269 while ($note->fetch()) {
270 $values['note'][$note->id] = array();
271 CRM_Core_DAO::storeValues($note, $values['note'][$note->id]);
272 $notes[] = $note;
273
274 $count++;
275 // if we have collected the number of notes, exit loop
276 if ($numNotes > 0 && $count >= $numNotes) {
277 break;
278 }
279 }
280
281 return $notes;
282 }
283
284 /**
100fef9d 285 * Delete the notes
6a488035 286 *
6a0b768e
TO
287 * @param int $id
288 * Note id.
289 * @param bool $showStatus
290 * Do we need to set status or not.
6a488035 291 *
e0b82b44
CW
292 * @return mixed|NULL $return no of deleted notes on success, FALSE otherwise
293 * @access public
6a488035
TO
294 * @static
295 */
00be9182 296 public static function del($id, $showStatus = TRUE) {
6a488035
TO
297 $return = NULL;
298 $recent = array($id);
299 $note = new CRM_Core_DAO_Note();
300 $note->id = $id;
301 $note->find();
302 $note->fetch();
303 if ($note->entity_table == 'civicrm_note') {
304 $status = ts('Selected Comment has been deleted successfully.');
305 }
306 else {
307 $status = ts('Selected Note has been deleted successfully.');
308 }
309
310 // Delete all descendents of this Note
311 foreach (self::getDescendentIds($id) as $childId) {
312 $childNote = new CRM_Core_DAO_Note();
313 $childNote->id = $childId;
314 $childNote->delete();
82f5e43b 315 $childNote->free();
6a488035
TO
316 $recent[] = $childId;
317 }
318
319 $return = $note->delete();
82f5e43b 320 $note->free();
6a488035
TO
321 if ($showStatus) {
322 CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
323 }
324
325 // delete the recently created Note
326 foreach ($recent as $recentId) {
327 $noteRecent = array(
328 'id' => $recentId,
329 'type' => 'Note',
330 );
331 CRM_Utils_Recent::del($noteRecent);
332 }
333 return $return;
334 }
335
336 /**
100fef9d 337 * Delete all records for this contact id
6a488035 338 *
6a0b768e
TO
339 * @param int $id
340 * ID of the contact for which note needs to be deleted.
6a488035
TO
341 *
342 * @return void
343 *
6a488035
TO
344 * @static
345 */
346 public static function deleteContact($id) {
347 // need to delete for both entity_id
348 $dao = new CRM_Core_DAO_Note();
349 $dao->entity_table = 'civicrm_contact';
350 $dao->entity_id = $id;
351 $dao->delete();
352
353 // and the creator contact id
354 $dao = new CRM_Core_DAO_Note();
355 $dao->contact_id = $id;
356 $dao->delete();
357 }
358
359 /**
100fef9d 360 * Retrieve all records for this entity-id
6a488035 361 *
6a0b768e
TO
362 * @param int $id
363 * ID of the relationship for which records needs to be retrieved.
77b97be7
EM
364 *
365 * @param string $entityTable
6a488035
TO
366 *
367 * @return array $viewNote array of note properties
368 *
6a488035
TO
369 * @static
370 */
371 public static function &getNote($id, $entityTable = 'civicrm_relationship') {
372 $viewNote = array();
373
374 $query = "
375 SELECT id,
376 note
377 FROM civicrm_note
378 WHERE entity_table=\"{$entityTable}\"
379 AND entity_id = %1
380 AND note is not null
381ORDER BY modified_date desc";
382 $params = array(1 => array($id, 'Integer'));
383
384 $dao = CRM_Core_DAO::executeQuery($query, $params);
385
386 while ($dao->fetch()) {
387 $viewNote[$dao->id] = $dao->note;
388 }
389
390 return $viewNote;
391 }
392
393 /**
100fef9d 394 * Get log record count for a Contact
6a488035 395 *
c490a46a 396 * @param int $contactID
6a488035
TO
397 *
398 * @return int $count count of log records
399 *
6a488035
TO
400 * @static
401 */
00be9182 402 public static function getContactNoteCount($contactID) {
6a488035
TO
403 $note = new CRM_Core_DAO_Note();
404 $note->entity_id = $contactID;
405 $note->entity_table = 'civicrm_contact';
406 $note->find();
407 $count = 0;
408 while ($note->fetch()) {
409 if (!self::getNotePrivacyHidden($note)) {
410 $count++;
411 }
412 }
413 return $count;
414 }
415
416 /**
100fef9d 417 * Get all descendent notes of the note with given ID
6a488035 418 *
6a0b768e
TO
419 * @param int $parentId
420 * ID of the note to start from.
421 * @param int $maxDepth
422 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
423 * @param bool $snippet
424 * If TRUE, returned values will be pre-formatted for display in a table of notes.
6a488035
TO
425 *
426 * @return array Nested associative array beginning with direct children of given note.
427 *
428 * @static
6a488035
TO
429 */
430 public static function getNoteTree($parentId, $maxDepth = 0, $snippet = FALSE) {
431 return self::buildNoteTree($parentId, $maxDepth, $snippet);
432 }
433
434 /**
435 * Get total count of direct children visible to the current user
436 *
6a0b768e
TO
437 * @param int $id
438 * Note ID.
6a488035
TO
439 *
440 * @return int $count Number of notes having the give note as parent
441 *
442 * @static
6a488035
TO
443 */
444 public static function getChildCount($id) {
445 $note = new CRM_Core_DAO_Note();
446 $note->entity_table = 'civicrm_note';
447 $note->entity_id = $id;
448 $note->find();
449 $count = 0;
450 while ($note->fetch()) {
451 if (!self::getNotePrivacyHidden($note)) {
452 $count++;
453 }
454 }
455 return $count;
456 }
457
458 /**
459 * Recursive function to get all descendent notes of the note with given ID
460 *
6a0b768e
TO
461 * @param int $parentId
462 * ID of the note to start from.
463 * @param int $maxDepth
464 * Maximum number of levels to descend into the tree; if not given, will include all descendents.
465 * @param bool $snippet
466 * If TRUE, returned values will be pre-formatted for display in a table of notes.
467 * @param array $tree
468 * (Reference) Variable to store all found descendents.
469 * @param int $depth
470 * Depth of current iteration within the descendent tree (used for comparison against maxDepth).
6a488035
TO
471 *
472 * @return array Nested associative array beginning with direct children of given note.
473 * @static
474 */
1a3fb0ce 475 private static function buildNoteTree($parentId, $maxDepth = 0, $snippet = FALSE, &$tree = array(), $depth = 0) {
6a488035 476 if ($maxDepth && $depth > $maxDepth) {
ab8a593e 477 return FALSE;
6a488035
TO
478 }
479
480 // get direct children of given parentId note
481 $note = new CRM_Core_DAO_Note();
482 $note->entity_table = 'civicrm_note';
483 $note->entity_id = $parentId;
484 $note->orderBy('modified_date asc');
485 $note->find();
486 while ($note->fetch()) {
487 // foreach child, call this function, unless the child is private/hidden
488 if (!self::getNotePrivacyHidden($note)) {
489 CRM_Core_DAO::storeValues($note, $tree[$note->id]);
490
491 // get name of user that created this note
492 $contact = new CRM_Contact_DAO_Contact();
493 $createdById = $note->contact_id;
494 $contact->id = $createdById;
495 $contact->find();
496 $contact->fetch();
497 $tree[$note->id]['createdBy'] = $contact->display_name;
498 $tree[$note->id]['createdById'] = $createdById;
499 $tree[$note->id]['modified_date'] = CRM_Utils_Date::customFormat($tree[$note->id]['modified_date']);
500
9a042a42 501 // paper icon view for attachments part
502 $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id);
1a3fb0ce 503 $tree[$note->id]['attachment'] = $paperIconAttachmentInfo ? implode('', $paperIconAttachmentInfo) : '';
9a042a42 504
6a488035
TO
505 if ($snippet) {
506 $tree[$note->id]['note'] = nl2br($tree[$note->id]['note']);
507 $tree[$note->id]['note'] = smarty_modifier_mb_truncate(
508 $tree[$note->id]['note'],
509 80,
510 '...',
511 TRUE
512 );
513 CRM_Utils_Date::customFormat($tree[$note->id]['modified_date']);
514 }
515 self::buildNoteTree(
516 $note->id,
517 $maxDepth,
518 $snippet,
519 $tree[$note->id]['child'],
520 $depth + 1
521 );
522 }
523 }
524
525 return $tree;
526 }
527
528 /**
100fef9d 529 * Given a note id, get a list of the ids of all notes that are descendents of that note
6a488035 530 *
6a0b768e
TO
531 * @param int $parentId
532 * Id of the given note.
533 * @param array $ids
534 * (reference) one-dimensional array to store found descendent ids.
6a488035
TO
535 *
536 * @return array $ids One-dimensional array containing ids of all desendent notes
537 */
c490a46a 538 public static function getDescendentIds($parentId, &$ids = array()) {
6a488035
TO
539 // get direct children of given parentId note
540 $note = new CRM_Core_DAO_Note();
541 $note->entity_table = 'civicrm_note';
542 $note->entity_id = $parentId;
543 $note->find();
544 while ($note->fetch()) {
545 // foreach child, add to ids list, and recurse
546 $ids[] = $note->id;
547 self::getDescendentIds($note->id, $ids);
548 }
549 return $ids;
550 }
551
552 /**
100fef9d 553 * Delete all note related to contact when contact is deleted
6a488035 554 *
6a0b768e
TO
555 * @param int $contactID
556 * Contact id whose notes to be deleted.
2a6da8d7 557 *
6a488035
TO
558 * @return void
559 * @static
560 */
00be9182 561 public static function cleanContactNotes($contactID) {
6a488035
TO
562 $params = array(1 => array($contactID, 'Integer'));
563
564 // delete all notes related to contribution
565 $contributeQuery = "DELETE note.*
566FROM civicrm_note note LEFT JOIN civicrm_contribution contribute ON note.entity_id = contribute.id
567WHERE contribute.contact_id = %1 AND note.entity_table = 'civicrm_contribution'";
568
569 CRM_Core_DAO::executeQuery($contributeQuery, $params);
570
571 // delete all notes related to participant
572 $participantQuery = "DELETE note.*
573FROM civicrm_note note LEFT JOIN civicrm_participant participant ON note.entity_id = participant.id
574WHERE participant.contact_id = %1 AND note.entity_table = 'civicrm_participant'";
575
576 CRM_Core_DAO::executeQuery($participantQuery, $params);
577
578 // delete all contact notes
579 $contactQuery = "SELECT id FROM civicrm_note WHERE entity_id = %1 AND entity_table = 'civicrm_contact'";
580
581 $contactNoteId = CRM_Core_DAO::executeQuery($contactQuery, $params);
582 while ($contactNoteId->fetch()) {
583 self::del($contactNoteId->id, FALSE);
584 }
585 }
586}