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