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