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