CRM-12527 - Set http headers in CMS-compatible way
[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-2015 |
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-2015
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 public function browse() {
86 $note = new CRM_Core_DAO_Note();
87 $note->entity_table = 'civicrm_contact';
88 $note->entity_id = $this->_contactId;
89
90 $note->orderBy('modified_date desc');
91
92 //CRM-4418, handling edit and delete separately.
93 $permissions = array($this->_permission);
94 if ($this->_permission == CRM_Core_Permission::EDIT) {
95 //previously delete was subset of edit
96 //so for consistency lets grant delete also.
97 $permissions[] = CRM_Core_Permission::DELETE;
98 }
99 $mask = CRM_Core_Action::mask($permissions);
100
101 $values = array();
102 $links = self::links();
103 $action = array_sum(array_keys($links)) & $mask;
104
105 $note->find();
106 while ($note->fetch()) {
107 if (!CRM_Core_BAO_Note::getNotePrivacyHidden($note)) {
108 CRM_Core_DAO::storeValues($note, $values[$note->id]);
109
110 $values[$note->id]['action'] = CRM_Core_Action::formLink($links,
111 $action,
112 array(
113 'id' => $note->id,
114 'cid' => $this->_contactId,
115 ),
116 ts('more'),
117 FALSE,
118 'note.selector.row',
119 'Note',
120 $note->id
121 );
122 $contact = new CRM_Contact_DAO_Contact();
123 $contact->id = $note->contact_id;
124 $contact->find();
125 $contact->fetch();
126 $values[$note->id]['createdBy'] = $contact->display_name;
127 $values[$note->id]['comment_count'] = CRM_Core_BAO_Note::getChildCount($note->id);
128
129 // paper icon view for attachments part
130 $paperIconAttachmentInfo = CRM_Core_BAO_File::paperIconAttachment('civicrm_note', $note->id);
131 $values[$note->id]['attachment'] = $paperIconAttachmentInfo;
132 }
133 }
134
135 $this->assign('notes', $values);
136
137 $commentLinks = self::commentLinks();
138
139 $action = array_sum(array_keys($commentLinks)) & $mask;
140
141 $commentAction = CRM_Core_Action::formLink($commentLinks,
142 $action,
143 array(
144 'id' => $note->id,
145 'pid' => $note->entity_id,
146 'cid' => $note->entity_id,
147 ),
148 ts('more'),
149 FALSE,
150 'note.comment.action',
151 'Note',
152 $note->id
153 );
154 $this->assign('commentAction', $commentAction);
155
156 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('note', $this->_contactId);
157 }
158
159 /**
160 * called when action is update or new.
161 */
162 public function edit() {
163 $controller = new CRM_Core_Controller_Simple('CRM_Note_Form_Note', ts('Contact Notes'), $this->_action);
164 $controller->setEmbedded(TRUE);
165
166 // set the userContext stack
167 $session = CRM_Core_Session::singleton();
168 $url = CRM_Utils_System::url('civicrm/contact/view',
169 'action=browse&selectedChild=note&cid=' . $this->_contactId
170 );
171 $session->pushUserContext($url);
172
173 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
174 CRM_Core_DAO::$_nullObject
175 )
176 ) {
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::UPDATE | CRM_Core_Action::ADD)) {
223 $this->edit();
224 }
225 elseif ($this->_action & CRM_Core_Action::DELETE) {
226 // we use the edit screen the confirm the delete
227 $this->edit();
228 }
229
230 $this->browse();
231 return parent::run();
232 }
233
234 /**
235 * Delete the note object from the db.
236 *
237 * @return void
238 */
239 public function delete() {
240 CRM_Core_BAO_Note::del($this->_id);
241 }
242
243 /**
244 * Get action links.
245 *
246 * @return array
247 * (reference) of action links
248 */
249 public static function &links() {
250 if (!(self::$_links)) {
251 $deleteExtra = ts('Are you sure you want to delete this note?');
252
253 self::$_links = array(
254 CRM_Core_Action::VIEW => array(
255 'name' => ts('View'),
256 'url' => 'civicrm/contact/view/note',
257 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
258 'title' => ts('View Note'),
259 ),
260 CRM_Core_Action::UPDATE => array(
261 'name' => ts('Edit'),
262 'url' => 'civicrm/contact/view/note',
263 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
264 'title' => ts('Edit Note'),
265 ),
266 CRM_Core_Action::ADD => array(
267 'name' => ts('Comment'),
268 'url' => 'civicrm/contact/view/note',
269 'qs' => 'action=add&reset=1&cid=%%cid%%&parentId=%%id%%&selectedChild=note',
270 'title' => ts('Add Comment'),
271 ),
272 CRM_Core_Action::DELETE => array(
273 'name' => ts('Delete'),
274 'url' => 'civicrm/contact/view/note',
275 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=note',
276 'title' => ts('Delete Note'),
277 ),
278 );
279 }
280 return self::$_links;
281 }
282
283 /**
284 * Get action links for comments.
285 *
286 * @return array
287 * (reference) of action links
288 */
289 public static function &commentLinks() {
290 if (!(self::$_commentLinks)) {
291 self::$_commentLinks = array(
292 CRM_Core_Action::VIEW => array(
293 'name' => ts('View'),
294 'url' => 'civicrm/contact/view/note',
295 'qs' => 'action=view&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
296 'title' => ts('View Comment'),
297 ),
298 CRM_Core_Action::UPDATE => array(
299 'name' => ts('Edit'),
300 'url' => 'civicrm/contact/view/note',
301 'qs' => 'action=update&reset=1&cid=%%cid%%&id={id}&parentId=%%pid%%&selectedChild=note',
302 'title' => ts('Edit Comment'),
303 ),
304 CRM_Core_Action::DELETE => array(
305 'name' => ts('Delete'),
306 'url' => 'civicrm/contact/view/note',
307 'qs' => 'action=delete&reset=1&cid=%%cid%%&id={id}&selectedChild=note',
308 'title' => ts('Delete Comment'),
309 ),
310 );
311 }
312 return self::$_commentLinks;
313 }
314
315 }