CRM-21473: Add new permission to add contact notes and prevent users without edit...
[civicrm-core.git] / CRM / Contact / Page / View / Note.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
35 * Main page for viewing Notes.
6a488035
TO
36 */
37class 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
TO
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
6a488035
TO
50 */
51 static $_commentLinks = NULL;
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)) {
60 $values = array();
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.
89 $permissions = array($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
088101a4
O
97 $this->assign('canAddNotes', CRM_Core_Permission::check('add contact notes'));
98
6a488035 99 $values = array();
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,
110 array(
111 'id' => $note->id,
112 'cid' => $this->_contactId,
87dab4a4
AH
113 ),
114 ts('more'),
115 FALSE,
116 'note.selector.row',
117 'Note',
118 $note->id
6a488035
TO
119 );
120 $contact = new CRM_Contact_DAO_Contact();
121 $contact->id = $note->contact_id;
122 $contact->find();
123 $contact->fetch();
124 $values[$note->id]['createdBy'] = $contact->display_name;
125 $values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id);
34f51a07
N
126
127 // paper icon view for attachments part
128 $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id);
129 $values[$note->id]['attachment'] = $paperIconAttachmentInfo;
6a488035
TO
130 }
131 }
132
133 $this->assign('notes', $values);
134
135 $commentLinks = self::commentLinks();
136
137 $action = array_sum(array_keys($commentLinks)) & $mask;
138
139 $commentAction = CRM_Core_Action::formLink($commentLinks,
140 $action,
141 array(
142 'id' => $note->id,
143 'pid' => $note->entity_id,
144 'cid' => $note->entity_id,
87dab4a4
AH
145 ),
146 ts('more'),
147 FALSE,
148 'note.comment.action',
149 'Note',
150 $note->id
6a488035
TO
151 );
152 $this->assign('commentAction', $commentAction);
4e8065a9
CW
153
154 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('note', $this->_contactId);
6a488035
TO
155 }
156
157 /**
fe482240 158 * called when action is update or new.
6a488035 159 */
00be9182 160 public function edit() {
6a488035
TO
161 $controller = new CRM_Core_Controller_Simple('CRM_Note_Form_Note', ts('Contact Notes'), $this->_action);
162 $controller->setEmbedded(TRUE);
163
164 // set the userContext stack
165 $session = CRM_Core_Session::singleton();
166 $url = CRM_Utils_System::url('civicrm/contact/view',
167 'action=browse&selectedChild=note&cid=' . $this->_contactId
168 );
169 $session->pushUserContext($url);
170
1273d77c 171 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
6a488035
TO
172 CRM_Core_BAO_Note::del($this->_id);
173 CRM_Utils_System::redirect($url);
174 }
175
176 $controller->reset();
177 $controller->set('entityTable', 'civicrm_contact');
178 $controller->set('entityId', $this->_contactId);
179 $controller->set('id', $this->_id);
180
181 $controller->process();
182 $controller->run();
183 }
184
00be9182 185 public function preProcess() {
6a488035
TO
186 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
187
188 if ($this->_id && CRM_Core_BAO_Note::getNotePrivacyHidden($this->_id)) {
189 CRM_Core_Error::statusBounce(ts('You do not have access to this note.'));
190 }
191
192 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
193 $this->assign('contactId', $this->_contactId);
194
195 // check logged in url permission
196 CRM_Contact_Page_View::checkUserPermission($this);
197
6a488035
TO
198 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
199 CRM_Utils_System::setTitle(ts('Notes for') . ' ' . $displayName);
200
201 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
202 $this->assign('action', $this->_action);
203 }
204
205 /**
dc195289 206 * the main function that is called when the page loads,
6a488035
TO
207 * it decides the which action has to be taken for the page.
208 *
76e7a76c 209 * @return null
6a488035 210 */
00be9182 211 public function run() {
6a488035
TO
212 $this->preProcess();
213
214 if ($this->_action & CRM_Core_Action::VIEW) {
215 $this->view();
216 }
088101a4
O
217 elseif ($this->_action & CRM_Core_Action::ADD) {
218 if (
219 $this->_permission != CRM_Core_Permission::EDIT &&
220 !CRM_Core_Permission::check('add contact notes')
221 ) {
222 CRM_Core_Error::statusBounce(ts('You do not have access to add notes.'));
223 }
224
225 $this->edit();
226 }
227 elseif ($this->_action & CRM_Core_Action::UPDATE) {
228 if ($this->_permission != CRM_Core_Permission::EDIT) {
229 CRM_Core_Error::statusBounce(ts('You do not have access to edit this note.'));
230 }
231
6a488035
TO
232 $this->edit();
233 }
234 elseif ($this->_action & CRM_Core_Action::DELETE) {
088101a4
O
235 if ($this->_permission != CRM_Core_Permission::EDIT) {
236 CRM_Core_Error::statusBounce(ts('You do not have access to delete this note.'));
237 }
6a488035
TO
238 // we use the edit screen the confirm the delete
239 $this->edit();
240 }
241
242 $this->browse();
243 return parent::run();
244 }
245
246 /**
fe482240 247 * Delete the note object from the db.
6a488035 248 */
00be9182 249 public function delete() {
6a488035
TO
250 CRM_Core_BAO_Note::del($this->_id);
251 }
252
253 /**
fe482240 254 * Get action links.
6a488035 255 *
a6c01b45
CW
256 * @return array
257 * (reference) of action links
6a488035 258 */
00be9182 259 public static function &links() {
6a488035
TO
260 if (!(self::$_links)) {
261 $deleteExtra = ts('Are you sure you want to delete this note?');
262
263 self::$_links = array(
264 CRM_Core_Action::VIEW => array(
265 'name' => ts('View'),
266 'url' => 'civicrm/contact/view/note',
267 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
268 'title' => ts('View Note'),
269 ),
270 CRM_Core_Action::UPDATE => array(
271 'name' => ts('Edit'),
272 'url' => 'civicrm/contact/view/note',
273 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
274 'title' => ts('Edit Note'),
275 ),
276 CRM_Core_Action::ADD => array(
277 'name' => ts('Comment'),
278 'url' => 'civicrm/contact/view/note',
279 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note',
280 'title' => ts('Add Comment'),
281 ),
282 CRM_Core_Action::DELETE => array(
283 'name' => ts('Delete'),
284 'url' => 'civicrm/contact/view/note',
285 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
6a488035
TO
286 'title' => ts('Delete Note'),
287 ),
288 );
289 }
290 return self::$_links;
291 }
292
293 /**
fe482240 294 * Get action links for comments.
6a488035 295 *
a6c01b45
CW
296 * @return array
297 * (reference) of action links
6a488035 298 */
00be9182 299 public static function &commentLinks() {
6a488035 300 if (!(self::$_commentLinks)) {
6a488035
TO
301 self::$_commentLinks = array(
302 CRM_Core_Action::VIEW => array(
303 'name' => ts('View'),
304 'url' => 'civicrm/contact/view/note',
305 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
306 'title' => ts('View Comment'),
307 ),
308 CRM_Core_Action::UPDATE => array(
309 'name' => ts('Edit'),
310 'url' => 'civicrm/contact/view/note',
311 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note',
312 'title' => ts('Edit Comment'),
313 ),
314 CRM_Core_Action::DELETE => array(
315 'name' => ts('Delete'),
316 'url' => 'civicrm/contact/view/note',
317 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
6a488035
TO
318 'title' => ts('Delete Comment'),
319 ),
320 );
321 }
322 return self::$_commentLinks;
323 }
96025800 324
6a488035 325}