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 | ||
7d0c4217 SL |
23 | /** |
24 | * Notes found running the browse function | |
25 | * @var array | |
26 | */ | |
27 | public $values = []; | |
28 | ||
6a488035 | 29 | /** |
fe482240 | 30 | * View details of a note. |
6a488035 | 31 | */ |
00be9182 | 32 | public function view() { |
6a488035 TO |
33 | $note = new CRM_Core_DAO_Note(); |
34 | $note->id = $this->_id; | |
35 | if ($note->find(TRUE)) { | |
d962cd76 | 36 | |
7d0c4217 SL |
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); | |
6a488035 TO |
40 | } |
41 | ||
7d0c4217 | 42 | $comments = CRM_Core_BAO_Note::getNoteTree($this->values['id'], 1); |
6a488035 TO |
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 | /** | |
fe482240 | 54 | * called when action is browse. |
6a488035 | 55 | */ |
00be9182 | 56 | public function browse() { |
353ffa53 | 57 | $note = new CRM_Core_DAO_Note(); |
6a488035 | 58 | $note->entity_table = 'civicrm_contact'; |
353ffa53 | 59 | $note->entity_id = $this->_contactId; |
6a488035 TO |
60 | |
61 | $note->orderBy('modified_date desc'); | |
62 | ||
63 | //CRM-4418, handling edit and delete separately. | |
be2fb01f | 64 | $permissions = [$this->_permission]; |
6a488035 TO |
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 | ||
088101a4 O |
72 | $this->assign('canAddNotes', CRM_Core_Permission::check('add contact notes')); |
73 | ||
353ffa53 | 74 | $links = self::links(); |
6a488035 TO |
75 | $action = array_sum(array_keys($links)) & $mask; |
76 | ||
77 | $note->find(); | |
78 | while ($note->fetch()) { | |
79 | if (!CRM_Core_BAO_Note::getNotePrivacyHidden($note)) { | |
7d0c4217 | 80 | CRM_Core_DAO::storeValues($note, $this->values[$note->id]); |
6a488035 | 81 | |
7d0c4217 | 82 | $this->values[$note->id]['action'] = CRM_Core_Action::formLink($links, |
6a488035 | 83 | $action, |
be2fb01f | 84 | [ |
6a488035 TO |
85 | 'id' => $note->id, |
86 | 'cid' => $this->_contactId, | |
be2fb01f | 87 | ], |
87dab4a4 AH |
88 | ts('more'), |
89 | FALSE, | |
90 | 'note.selector.row', | |
91 | 'Note', | |
92 | $note->id | |
6a488035 | 93 | ); |
faa3fe68 SL |
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(); | |
7d0c4217 | 99 | $this->values[$note->id]['createdBy'] = $contact->display_name; |
faa3fe68 | 100 | } |
7d0c4217 | 101 | $this->values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id); |
34f51a07 N |
102 | |
103 | // paper icon view for attachments part | |
104 | $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id); | |
7d0c4217 | 105 | $this->values[$note->id]['attachment'] = $paperIconAttachmentInfo; |
6a488035 TO |
106 | } |
107 | } | |
7d0c4217 | 108 | $this->assign('notes', $this->values); |
6a488035 TO |
109 | |
110 | $commentLinks = self::commentLinks(); | |
111 | ||
112 | $action = array_sum(array_keys($commentLinks)) & $mask; | |
113 | ||
114 | $commentAction = CRM_Core_Action::formLink($commentLinks, | |
115 | $action, | |
be2fb01f | 116 | [ |
6a488035 TO |
117 | 'id' => $note->id, |
118 | 'pid' => $note->entity_id, | |
119 | 'cid' => $note->entity_id, | |
be2fb01f | 120 | ], |
87dab4a4 AH |
121 | ts('more'), |
122 | FALSE, | |
123 | 'note.comment.action', | |
124 | 'Note', | |
125 | $note->id | |
6a488035 TO |
126 | ); |
127 | $this->assign('commentAction', $commentAction); | |
4e8065a9 CW |
128 | |
129 | $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('note', $this->_contactId); | |
6a488035 TO |
130 | } |
131 | ||
132 | /** | |
fe482240 | 133 | * called when action is update or new. |
6a488035 | 134 | */ |
00be9182 | 135 | public function edit() { |
6a488035 TO |
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 | ||
1273d77c | 146 | if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) { |
6623e554 | 147 | $this->delete(); |
6a488035 TO |
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 | ||
00be9182 | 160 | public function preProcess() { |
6a488035 TO |
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 | ||
6a488035 TO |
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 | /** | |
dc195289 | 181 | * the main function that is called when the page loads, |
6a488035 TO |
182 | * it decides the which action has to be taken for the page. |
183 | * | |
76e7a76c | 184 | * @return null |
6a488035 | 185 | */ |
00be9182 | 186 | public function run() { |
6a488035 TO |
187 | $this->preProcess(); |
188 | ||
189 | if ($this->_action & CRM_Core_Action::VIEW) { | |
190 | $this->view(); | |
191 | } | |
088101a4 O |
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 | ||
6a488035 TO |
207 | $this->edit(); |
208 | } | |
209 | elseif ($this->_action & CRM_Core_Action::DELETE) { | |
088101a4 O |
210 | if ($this->_permission != CRM_Core_Permission::EDIT) { |
211 | CRM_Core_Error::statusBounce(ts('You do not have access to delete this note.')); | |
212 | } | |
6a488035 TO |
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 | /** | |
6623e554 | 222 | * Delete the note object from the db and set a status msg. |
6a488035 | 223 | */ |
00be9182 | 224 | public function delete() { |
6623e554 CW |
225 | CRM_Core_BAO_Note::deleteRecord(['id' => $this->_id]); |
226 | $status = ts('Selected Note has been deleted successfully.'); | |
227 | CRM_Core_Session::setStatus($status, ts('Deleted'), 'success'); | |
6a488035 TO |
228 | } |
229 | ||
230 | /** | |
fe482240 | 231 | * Get action links. |
6a488035 | 232 | * |
26b82f6e | 233 | * @return array[] |
6a488035 | 234 | */ |
26b82f6e CW |
235 | public static function links() { |
236 | return [ | |
237 | CRM_Core_Action::VIEW => [ | |
238 | 'name' => ts('View'), | |
239 | 'url' => 'civicrm/contact/view/note', | |
240 | 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
241 | 'title' => ts('View Note'), | |
242 | ], | |
243 | CRM_Core_Action::UPDATE => [ | |
244 | 'name' => ts('Edit'), | |
245 | 'url' => 'civicrm/contact/view/note', | |
246 | 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
247 | 'title' => ts('Edit Note'), | |
248 | ], | |
249 | CRM_Core_Action::ADD => [ | |
250 | 'name' => ts('Comment'), | |
251 | 'url' => 'civicrm/contact/view/note', | |
252 | 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note', | |
253 | 'title' => ts('Add Comment'), | |
254 | ], | |
255 | CRM_Core_Action::DELETE => [ | |
256 | 'name' => ts('Delete'), | |
257 | 'url' => 'civicrm/contact/view/note', | |
258 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note', | |
259 | 'title' => ts('Delete Note'), | |
260 | ], | |
261 | ]; | |
6a488035 TO |
262 | } |
263 | ||
264 | /** | |
fe482240 | 265 | * Get action links for comments. |
6a488035 | 266 | * |
26b82f6e | 267 | * @return array[] |
6a488035 | 268 | */ |
26b82f6e CW |
269 | public static function commentLinks() { |
270 | return [ | |
271 | CRM_Core_Action::VIEW => [ | |
272 | 'name' => ts('View'), | |
273 | 'url' => 'civicrm/contact/view/note', | |
274 | 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note', | |
275 | 'title' => ts('View Comment'), | |
276 | ], | |
277 | CRM_Core_Action::UPDATE => [ | |
278 | 'name' => ts('Edit'), | |
279 | 'url' => 'civicrm/contact/view/note', | |
280 | 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note', | |
281 | 'title' => ts('Edit Comment'), | |
282 | ], | |
283 | CRM_Core_Action::DELETE => [ | |
284 | 'name' => ts('Delete'), | |
285 | 'url' => 'civicrm/contact/view/note', | |
286 | 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note', | |
287 | 'title' => ts('Delete Comment'), | |
288 | ], | |
289 | ]; | |
6a488035 | 290 | } |
96025800 | 291 | |
6a488035 | 292 | } |