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