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