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