Commit | Line | Data |
---|---|---|
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 | */ |
21 | class 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')) { |
6a488035 TO |
161 | CRM_Core_BAO_Note::del($this->_id); |
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 | /** | |
fe482240 | 236 | * Delete the note object from the db. |
6a488035 | 237 | */ |
00be9182 | 238 | public function delete() { |
6a488035 TO |
239 | CRM_Core_BAO_Note::del($this->_id); |
240 | } | |
241 | ||
242 | /** | |
fe482240 | 243 | * Get action links. |
6a488035 | 244 | * |
a6c01b45 CW |
245 | * @return array |
246 | * (reference) of action links | |
6a488035 | 247 | */ |
00be9182 | 248 | public static function &links() { |
6a488035 TO |
249 | if (!(self::$_links)) { |
250 | $deleteExtra = ts('Are you sure you want to delete this note?'); | |
251 | ||
be2fb01f CW |
252 | self::$_links = [ |
253 | CRM_Core_Action::VIEW => [ | |
6a488035 TO |
254 | 'name' => ts('View'), |
255 | 'url' => 'civicrm/contact/view/note', | |
256 | 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
257 | 'title' => ts('View Note'), | |
be2fb01f CW |
258 | ], |
259 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
260 | 'name' => ts('Edit'), |
261 | 'url' => 'civicrm/contact/view/note', | |
262 | 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
263 | 'title' => ts('Edit Note'), | |
be2fb01f CW |
264 | ], |
265 | CRM_Core_Action::ADD => [ | |
6a488035 TO |
266 | 'name' => ts('Comment'), |
267 | 'url' => 'civicrm/contact/view/note', | |
268 | 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note', | |
269 | 'title' => ts('Add Comment'), | |
be2fb01f CW |
270 | ], |
271 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
272 | 'name' => ts('Delete'), |
273 | 'url' => 'civicrm/contact/view/note', | |
274 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
6a488035 | 275 | 'title' => ts('Delete Note'), |
be2fb01f CW |
276 | ], |
277 | ]; | |
6a488035 TO |
278 | } |
279 | return self::$_links; | |
280 | } | |
281 | ||
282 | /** | |
fe482240 | 283 | * Get action links for comments. |
6a488035 | 284 | * |
a6c01b45 CW |
285 | * @return array |
286 | * (reference) of action links | |
6a488035 | 287 | */ |
00be9182 | 288 | public static function &commentLinks() { |
6a488035 | 289 | if (!(self::$_commentLinks)) { |
be2fb01f CW |
290 | self::$_commentLinks = [ |
291 | CRM_Core_Action::VIEW => [ | |
6a488035 TO |
292 | 'name' => ts('View'), |
293 | 'url' => 'civicrm/contact/view/note', | |
294 | 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note', | |
295 | 'title' => ts('View Comment'), | |
be2fb01f CW |
296 | ], |
297 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
298 | 'name' => ts('Edit'), |
299 | 'url' => 'civicrm/contact/view/note', | |
300 | 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note', | |
301 | 'title' => ts('Edit Comment'), | |
be2fb01f CW |
302 | ], |
303 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
304 | 'name' => ts('Delete'), |
305 | 'url' => 'civicrm/contact/view/note', | |
306 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note', | |
6a488035 | 307 | 'title' => ts('Delete Comment'), |
be2fb01f CW |
308 | ], |
309 | ]; | |
6a488035 TO |
310 | } |
311 | return self::$_commentLinks; | |
312 | } | |
96025800 | 313 | |
6a488035 | 314 | } |