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