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