Note API - Ensure child notes are deleted with parent, and hooks are called
[civicrm-core.git] / CRM / Contact / Page / View / Note.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Main page for viewing Notes.
6a488035
TO
20 */
21class CRM_Contact_Page_View_Note extends CRM_Core_Page {
22
23 /**
24 * The action links for notes that we need to display for the browse screen
25 *
26 * @var array
6a488035 27 */
69078420 28 public static $_links = NULL;
6a488035
TO
29
30 /**
31 * The action links for comments that we need to display for the browse screen
32 *
33 * @var array
6a488035 34 */
69078420 35 public static $_commentLinks = NULL;
6a488035 36
7d0c4217
SL
37 /**
38 * Notes found running the browse function
39 * @var array
40 */
41 public $values = [];
42
6a488035 43 /**
fe482240 44 * View details of a note.
6a488035 45 */
00be9182 46 public function view() {
6a488035
TO
47 $note = new CRM_Core_DAO_Note();
48 $note->id = $this->_id;
49 if ($note->find(TRUE)) {
d962cd76 50
7d0c4217
SL
51 CRM_Core_DAO::storeValues($note, $this->values);
52 $this->values['privacy'] = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Note', 'privacy', $this->values['privacy']);
53 $this->assign('note', $this->values);
6a488035
TO
54 }
55
7d0c4217 56 $comments = CRM_Core_BAO_Note::getNoteTree($this->values['id'], 1);
6a488035
TO
57 if (!empty($comments)) {
58 $this->assign('comments', $comments);
59 }
60
61 // add attachments part
62 $currentAttachmentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_note', $this->_id);
63 $this->assign('currentAttachmentInfo', $currentAttachmentInfo);
64
65 }
66
67 /**
fe482240 68 * called when action is browse.
6a488035 69 */
00be9182 70 public function browse() {
353ffa53 71 $note = new CRM_Core_DAO_Note();
6a488035 72 $note->entity_table = 'civicrm_contact';
353ffa53 73 $note->entity_id = $this->_contactId;
6a488035
TO
74
75 $note->orderBy('modified_date desc');
76
77 //CRM-4418, handling edit and delete separately.
be2fb01f 78 $permissions = [$this->_permission];
6a488035
TO
79 if ($this->_permission == CRM_Core_Permission::EDIT) {
80 //previously delete was subset of edit
81 //so for consistency lets grant delete also.
82 $permissions[] = CRM_Core_Permission::DELETE;
83 }
84 $mask = CRM_Core_Action::mask($permissions);
85
088101a4
O
86 $this->assign('canAddNotes', CRM_Core_Permission::check('add contact notes'));
87
353ffa53 88 $links = self::links();
6a488035
TO
89 $action = array_sum(array_keys($links)) & $mask;
90
91 $note->find();
92 while ($note->fetch()) {
93 if (!CRM_Core_BAO_Note::getNotePrivacyHidden($note)) {
7d0c4217 94 CRM_Core_DAO::storeValues($note, $this->values[$note->id]);
6a488035 95
7d0c4217 96 $this->values[$note->id]['action'] = CRM_Core_Action::formLink($links,
6a488035 97 $action,
be2fb01f 98 [
6a488035
TO
99 'id' => $note->id,
100 'cid' => $this->_contactId,
be2fb01f 101 ],
87dab4a4
AH
102 ts('more'),
103 FALSE,
104 'note.selector.row',
105 'Note',
106 $note->id
6a488035 107 );
faa3fe68
SL
108 if (!empty($note->contact_id)) {
109 $contact = new CRM_Contact_DAO_Contact();
110 $contact->id = $note->contact_id;
111 $contact->find();
112 $contact->fetch();
7d0c4217 113 $this->values[$note->id]['createdBy'] = $contact->display_name;
faa3fe68 114 }
7d0c4217 115 $this->values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id);
34f51a07
N
116
117 // paper icon view for attachments part
118 $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id);
7d0c4217 119 $this->values[$note->id]['attachment'] = $paperIconAttachmentInfo;
6a488035
TO
120 }
121 }
7d0c4217 122 $this->assign('notes', $this->values);
6a488035
TO
123
124 $commentLinks = self::commentLinks();
125
126 $action = array_sum(array_keys($commentLinks)) & $mask;
127
128 $commentAction = CRM_Core_Action::formLink($commentLinks,
129 $action,
be2fb01f 130 [
6a488035
TO
131 'id' => $note->id,
132 'pid' => $note->entity_id,
133 'cid' => $note->entity_id,
be2fb01f 134 ],
87dab4a4
AH
135 ts('more'),
136 FALSE,
137 'note.comment.action',
138 'Note',
139 $note->id
6a488035
TO
140 );
141 $this->assign('commentAction', $commentAction);
4e8065a9
CW
142
143 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('note', $this->_contactId);
6a488035
TO
144 }
145
146 /**
fe482240 147 * called when action is update or new.
6a488035 148 */
00be9182 149 public function edit() {
6a488035
TO
150 $controller = new CRM_Core_Controller_Simple('CRM_Note_Form_Note', ts('Contact Notes'), $this->_action);
151 $controller->setEmbedded(TRUE);
152
153 // set the userContext stack
154 $session = CRM_Core_Session::singleton();
155 $url = CRM_Utils_System::url('civicrm/contact/view',
156 'action=browse&selectedChild=note&cid=' . $this->_contactId
157 );
158 $session->pushUserContext($url);
159
1273d77c 160 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
6623e554 161 $this->delete();
6a488035
TO
162 CRM_Utils_System::redirect($url);
163 }
164
165 $controller->reset();
166 $controller->set('entityTable', 'civicrm_contact');
167 $controller->set('entityId', $this->_contactId);
168 $controller->set('id', $this->_id);
169
170 $controller->process();
171 $controller->run();
172 }
173
00be9182 174 public function preProcess() {
6a488035
TO
175 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
176
177 if ($this->_id && CRM_Core_BAO_Note::getNotePrivacyHidden($this->_id)) {
178 CRM_Core_Error::statusBounce(ts('You do not have access to this note.'));
179 }
180
181 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
182 $this->assign('contactId', $this->_contactId);
183
184 // check logged in url permission
185 CRM_Contact_Page_View::checkUserPermission($this);
186
6a488035
TO
187 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
188 CRM_Utils_System::setTitle(ts('Notes for') . ' ' . $displayName);
189
190 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
191 $this->assign('action', $this->_action);
192 }
193
194 /**
dc195289 195 * the main function that is called when the page loads,
6a488035
TO
196 * it decides the which action has to be taken for the page.
197 *
76e7a76c 198 * @return null
6a488035 199 */
00be9182 200 public function run() {
6a488035
TO
201 $this->preProcess();
202
203 if ($this->_action & CRM_Core_Action::VIEW) {
204 $this->view();
205 }
088101a4
O
206 elseif ($this->_action & CRM_Core_Action::ADD) {
207 if (
208 $this->_permission != CRM_Core_Permission::EDIT &&
209 !CRM_Core_Permission::check('add contact notes')
210 ) {
211 CRM_Core_Error::statusBounce(ts('You do not have access to add notes.'));
212 }
213
214 $this->edit();
215 }
216 elseif ($this->_action & CRM_Core_Action::UPDATE) {
217 if ($this->_permission != CRM_Core_Permission::EDIT) {
218 CRM_Core_Error::statusBounce(ts('You do not have access to edit this note.'));
219 }
220
6a488035
TO
221 $this->edit();
222 }
223 elseif ($this->_action & CRM_Core_Action::DELETE) {
088101a4
O
224 if ($this->_permission != CRM_Core_Permission::EDIT) {
225 CRM_Core_Error::statusBounce(ts('You do not have access to delete this note.'));
226 }
6a488035
TO
227 // we use the edit screen the confirm the delete
228 $this->edit();
229 }
230
231 $this->browse();
232 return parent::run();
233 }
234
235 /**
6623e554 236 * Delete the note object from the db and set a status msg.
6a488035 237 */
00be9182 238 public function delete() {
6623e554
CW
239 CRM_Core_BAO_Note::deleteRecord(['id' => $this->_id]);
240 $status = ts('Selected Note has been deleted successfully.');
241 CRM_Core_Session::setStatus($status, ts('Deleted'), 'success');
6a488035
TO
242 }
243
244 /**
fe482240 245 * Get action links.
6a488035 246 *
a6c01b45
CW
247 * @return array
248 * (reference) of action links
6a488035 249 */
00be9182 250 public static function &links() {
6a488035
TO
251 if (!(self::$_links)) {
252 $deleteExtra = ts('Are you sure you want to delete this note?');
253
be2fb01f
CW
254 self::$_links = [
255 CRM_Core_Action::VIEW => [
6a488035
TO
256 'name' => ts('View'),
257 'url' => 'civicrm/contact/view/note',
258 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
259 'title' => ts('View Note'),
be2fb01f
CW
260 ],
261 CRM_Core_Action::UPDATE => [
6a488035
TO
262 'name' => ts('Edit'),
263 'url' => 'civicrm/contact/view/note',
264 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
265 'title' => ts('Edit Note'),
be2fb01f
CW
266 ],
267 CRM_Core_Action::ADD => [
6a488035
TO
268 'name' => ts('Comment'),
269 'url' => 'civicrm/contact/view/note',
270 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note',
271 'title' => ts('Add Comment'),
be2fb01f
CW
272 ],
273 CRM_Core_Action::DELETE => [
6a488035
TO
274 'name' => ts('Delete'),
275 'url' => 'civicrm/contact/view/note',
276 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
6a488035 277 'title' => ts('Delete Note'),
be2fb01f
CW
278 ],
279 ];
6a488035
TO
280 }
281 return self::$_links;
282 }
283
284 /**
fe482240 285 * Get action links for comments.
6a488035 286 *
a6c01b45
CW
287 * @return array
288 * (reference) of action links
6a488035 289 */
00be9182 290 public static function &commentLinks() {
6a488035 291 if (!(self::$_commentLinks)) {
be2fb01f
CW
292 self::$_commentLinks = [
293 CRM_Core_Action::VIEW => [
6a488035
TO
294 'name' => ts('View'),
295 'url' => 'civicrm/contact/view/note',
296 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
297 'title' => ts('View Comment'),
be2fb01f
CW
298 ],
299 CRM_Core_Action::UPDATE => [
6a488035
TO
300 'name' => ts('Edit'),
301 'url' => 'civicrm/contact/view/note',
302 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note',
303 'title' => ts('Edit Comment'),
be2fb01f
CW
304 ],
305 CRM_Core_Action::DELETE => [
6a488035
TO
306 'name' => ts('Delete'),
307 'url' => 'civicrm/contact/view/note',
308 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
6a488035 309 'title' => ts('Delete Comment'),
be2fb01f
CW
310 ],
311 ];
6a488035
TO
312 }
313 return self::$_commentLinks;
314 }
96025800 315
6a488035 316}