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