Merge pull request #14015 from seamuslee001/nfc_code_style_fixes
[civicrm-core.git] / CRM / Contact / Page / View / Note.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Main page for viewing Notes.
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
43 */
44 static $_links = NULL;
45
46 /**
47 * The action links for comments that we need to display for the browse screen
48 *
49 * @var array
50 */
51 static $_commentLinks = NULL;
52
53 /**
54 * View details of a note.
55 */
56 public function view() {
57 $note = new CRM_Core_DAO_Note();
58 $note->id = $this->_id;
59 if ($note->find(TRUE)) {
60 $values = [];
61
62 CRM_Core_DAO::storeValues($note, $values);
63 $values['privacy'] = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Note', 'privacy', $values['privacy']);
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 /**
79 * called when action is browse.
80 */
81 public function browse() {
82 $note = new CRM_Core_DAO_Note();
83 $note->entity_table = 'civicrm_contact';
84 $note->entity_id = $this->_contactId;
85
86 $note->orderBy('modified_date desc');
87
88 //CRM-4418, handling edit and delete separately.
89 $permissions = [$this->_permission];
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
97 $this->assign('canAddNotes', CRM_Core_Permission::check('add contact notes'));
98
99 $values = [];
100 $links = self::links();
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,
110 [
111 'id' => $note->id,
112 'cid' => $this->_contactId,
113 ],
114 ts('more'),
115 FALSE,
116 'note.selector.row',
117 'Note',
118 $note->id
119 );
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 }
127 $values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id);
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;
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,
143 [
144 'id' => $note->id,
145 'pid' => $note->entity_id,
146 'cid' => $note->entity_id,
147 ],
148 ts('more'),
149 FALSE,
150 'note.comment.action',
151 'Note',
152 $note->id
153 );
154 $this->assign('commentAction', $commentAction);
155
156 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('note', $this->_contactId);
157 }
158
159 /**
160 * called when action is update or new.
161 */
162 public function edit() {
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
173 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
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
187 public function preProcess() {
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
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 /**
208 * the main function that is called when the page loads,
209 * it decides the which action has to be taken for the page.
210 *
211 * @return null
212 */
213 public function run() {
214 $this->preProcess();
215
216 if ($this->_action & CRM_Core_Action::VIEW) {
217 $this->view();
218 }
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
234 $this->edit();
235 }
236 elseif ($this->_action & CRM_Core_Action::DELETE) {
237 if ($this->_permission != CRM_Core_Permission::EDIT) {
238 CRM_Core_Error::statusBounce(ts('You do not have access to delete this note.'));
239 }
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 /**
249 * Delete the note object from the db.
250 */
251 public function delete() {
252 CRM_Core_BAO_Note::del($this->_id);
253 }
254
255 /**
256 * Get action links.
257 *
258 * @return array
259 * (reference) of action links
260 */
261 public static function &links() {
262 if (!(self::$_links)) {
263 $deleteExtra = ts('Are you sure you want to delete this note?');
264
265 self::$_links = [
266 CRM_Core_Action::VIEW => [
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'),
271 ],
272 CRM_Core_Action::UPDATE => [
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'),
277 ],
278 CRM_Core_Action::ADD => [
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'),
283 ],
284 CRM_Core_Action::DELETE => [
285 'name' => ts('Delete'),
286 'url' => 'civicrm/contact/view/note',
287 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
288 'title' => ts('Delete Note'),
289 ],
290 ];
291 }
292 return self::$_links;
293 }
294
295 /**
296 * Get action links for comments.
297 *
298 * @return array
299 * (reference) of action links
300 */
301 public static function &commentLinks() {
302 if (!(self::$_commentLinks)) {
303 self::$_commentLinks = [
304 CRM_Core_Action::VIEW => [
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'),
309 ],
310 CRM_Core_Action::UPDATE => [
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'),
315 ],
316 CRM_Core_Action::DELETE => [
317 'name' => ts('Delete'),
318 'url' => 'civicrm/contact/view/note',
319 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
320 'title' => ts('Delete Comment'),
321 ],
322 ];
323 }
324 return self::$_commentLinks;
325 }
326
327 }