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