Merge pull request #1909 from kenwest/CRM-13690-unittest
[civicrm-core.git] / CRM / Contact / Page / View / Note.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * @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
62 * @access public
63 */
64 function view() {
65 $note = new CRM_Core_DAO_Note();
66 $note->id = $this->_id;
67 if ($note->find(TRUE)) {
68 $values = array();
69 CRM_Core_DAO::storeValues($note, $values);
70 $values['privacy'] = CRM_Core_OptionGroup::optionLabel('note_privacy', $values['privacy']);
71 $this->assign('note', $values);
72 }
73
74 $comments = CRM_Core_BAO_Note::getNoteTree($values['id'], 1);
75 if (!empty($comments)) {
76 $this->assign('comments', $comments);
77 }
78
79 // add attachments part
80 $currentAttachmentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_note', $this->_id);
81 $this->assign('currentAttachmentInfo', $currentAttachmentInfo);
82
83 }
84
85 /**
86 * This function is called when action is browse
87 *
88 * return null
89 * @access public
90 */
91 function browse() {
92 $note = new CRM_Core_DAO_Note();
93 $note->entity_table = 'civicrm_contact';
94 $note->entity_id = $this->_contactId;
95
96 $note->orderBy('modified_date desc');
97
98 //CRM-4418, handling edit and delete separately.
99 $permissions = array($this->_permission);
100 if ($this->_permission == CRM_Core_Permission::EDIT) {
101 //previously delete was subset of edit
102 //so for consistency lets grant delete also.
103 $permissions[] = CRM_Core_Permission::DELETE;
104 }
105 $mask = CRM_Core_Action::mask($permissions);
106
107 $values = array();
108 $links = self::links();
109 $action = array_sum(array_keys($links)) & $mask;
110
111 $note->find();
112 while ($note->fetch()) {
113 if (!CRM_Core_BAO_Note::getNotePrivacyHidden($note)) {
114 CRM_Core_DAO::storeValues($note, $values[$note->id]);
115
116 $values[$note->id]['action'] = CRM_Core_Action::formLink($links,
117 $action,
118 array(
119 'id' => $note->id,
120 'cid' => $this->_contactId,
121 ),
122 ts('more'),
123 FALSE,
124 'note.selector.row',
125 'Note',
126 $note->id
127 );
128 $contact = new CRM_Contact_DAO_Contact();
129 $contact->id = $note->contact_id;
130 $contact->find();
131 $contact->fetch();
132 $values[$note->id]['createdBy'] = $contact->display_name;
133 $values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id);
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
159 /**
160 * This function is called when action is update or new
161 *
162 * return null
163 * @access public
164 */
165 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_DAO::$_nullObject
178 )) {
179 CRM_Core_BAO_Note::del($this->_id);
180 CRM_Utils_System::redirect($url);
181 }
182
183 $controller->reset();
184 $controller->set('entityTable', 'civicrm_contact');
185 $controller->set('entityId', $this->_contactId);
186 $controller->set('id', $this->_id);
187
188 $controller->process();
189 $controller->run();
190 }
191
192 function preProcess() {
193 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
194
195 if ($this->_id && CRM_Core_BAO_Note::getNotePrivacyHidden($this->_id)) {
196 CRM_Core_Error::statusBounce(ts('You do not have access to this note.'));
197 }
198
199 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
200 $this->assign('contactId', $this->_contactId);
201
202 // check logged in url permission
203 CRM_Contact_Page_View::checkUserPermission($this);
204
205 // set page title
206 CRM_Contact_Page_View::setTitle($this->_contactId);
207
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
220 * @access public
221 */
222 function run() {
223 $this->preProcess();
224
225 if ($this->_action & CRM_Core_Action::VIEW) {
226 $this->view();
227 }
228 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
229 $this->edit();
230 }
231 elseif ($this->_action & CRM_Core_Action::DELETE) {
232 // we use the edit screen the confirm the delete
233 $this->edit();
234 }
235
236 $this->browse();
237 return parent::run();
238 }
239
240 /**
241 * delete the note object from the db
242 *
243 * @return void
244 * @access public
245 */
246 function delete() {
247 CRM_Core_BAO_Note::del($this->_id);
248 }
249
250 /**
251 * Get action links
252 *
253 * @return array (reference) of action links
254 * @static
255 */
256 static function &links() {
257 if (!(self::$_links)) {
258 $deleteExtra = ts('Are you sure you want to delete this note?');
259
260 self::$_links = array(
261 CRM_Core_Action::VIEW => array(
262 'name' => ts('View'),
263 'url' => 'civicrm/contact/view/note',
264 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
265 'title' => ts('View Note'),
266 ),
267 CRM_Core_Action::UPDATE => array(
268 'name' => ts('Edit'),
269 'url' => 'civicrm/contact/view/note',
270 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
271 'title' => ts('Edit Note'),
272 ),
273 CRM_Core_Action::ADD => array(
274 'name' => ts('Comment'),
275 'url' => 'civicrm/contact/view/note',
276 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note',
277 'title' => ts('Add Comment'),
278 ),
279 CRM_Core_Action::DELETE => array(
280 'name' => ts('Delete'),
281 'url' => 'civicrm/contact/view/note',
282 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
283 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
284 'title' => ts('Delete Note'),
285 ),
286 );
287 }
288 return self::$_links;
289 }
290
291 /**
292 * Get action links for comments
293 *
294 * @return array (reference) of action links
295 * @static
296 */
297 static function &commentLinks() {
298 if (!(self::$_commentLinks)) {
299 $deleteExtra = ts('Are you sure you want to delete this comment?');
300 self::$_commentLinks = array(
301 CRM_Core_Action::VIEW => array(
302 'name' => ts('View'),
303 'url' => 'civicrm/contact/view/note',
304 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
305 'title' => ts('View Comment'),
306 ),
307 CRM_Core_Action::UPDATE => array(
308 'name' => ts('Edit'),
309 'url' => 'civicrm/contact/view/note',
310 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note',
311 'title' => ts('Edit Comment'),
312 ),
313 CRM_Core_Action::DELETE => array(
314 'name' => ts('Delete'),
315 'url' => 'civicrm/contact/view/note',
316 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
317 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
318 'title' => ts('Delete Comment'),
319 ),
320 );
321 }
322 return self::$_commentLinks;
323 }
324 }
325