| 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 | trait CRM_Core_Page_EntityPageTrait { |
| 18 | |
| 19 | /** |
| 20 | * Pages MUST declare the following functions: |
| 21 | * public function getDefaultEntity() { |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * Page MAY want to override the following functions: |
| 26 | * public function getDefaultContext() |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * The id of the contact. |
| 31 | * |
| 32 | * @var int |
| 33 | */ |
| 34 | protected $_id; |
| 35 | |
| 36 | /** |
| 37 | * The mode of operation for this page |
| 38 | * |
| 39 | * @var int |
| 40 | */ |
| 41 | protected $_action; |
| 42 | |
| 43 | /** |
| 44 | * The context that we are working on. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $_context; |
| 49 | |
| 50 | /** |
| 51 | * Contact ID of the contact on the page. |
| 52 | * |
| 53 | * @var int |
| 54 | */ |
| 55 | public $_contactID = NULL; |
| 56 | |
| 57 | /** |
| 58 | * Contact ID of the contact on the page. |
| 59 | * |
| 60 | * @var int |
| 61 | * @deprecated Historically pages alternate between $_contactID and $_contactId. We'll standardise on one |
| 62 | */ |
| 63 | public $_contactId = NULL; |
| 64 | |
| 65 | /** |
| 66 | * @var int |
| 67 | */ |
| 68 | public $_permission = NULL; |
| 69 | |
| 70 | /** |
| 71 | * The action links that we need to display for the browse screen. |
| 72 | * |
| 73 | * @var array |
| 74 | */ |
| 75 | public static $_links = NULL; |
| 76 | |
| 77 | /** |
| 78 | * Get the entity id being edited. |
| 79 | * |
| 80 | * @return int|null |
| 81 | */ |
| 82 | public function getEntityId() { |
| 83 | return $this->_id; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the context we are working in |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getContext() { |
| 92 | return $this->_context; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the contact ID |
| 97 | * |
| 98 | * @return int |
| 99 | */ |
| 100 | public function getContactId() { |
| 101 | return $this->_contactID; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set the contact ID |
| 106 | * |
| 107 | * @param $contactId |
| 108 | */ |
| 109 | public function setContactId($contactId) { |
| 110 | $this->_contactID = $contactId; |
| 111 | $this->_contactId = $contactId; |
| 112 | } |
| 113 | |
| 114 | public function getAction() { |
| 115 | return $this->_action; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Explicitly declare the form context. |
| 120 | * |
| 121 | * @return string|null |
| 122 | */ |
| 123 | public function getDefaultContext() { |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | public function preProcessQuickEntityPage() { |
| 128 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 129 | $this->assign('action', $this->getAction()); |
| 130 | |
| 131 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 132 | $this->setContactId(CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, CRM_Core_Session::getLoggedInContactID())); |
| 133 | |
| 134 | $this->assign('contactId', $this->getContactId()); |
| 135 | |
| 136 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, $this->getDefaultContext()); |
| 137 | $this->assign('context', $this->_context); |
| 138 | |
| 139 | // check logged in url permission |
| 140 | CRM_Contact_Page_View::checkUserPermission($this); |
| 141 | |
| 142 | $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity()))); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Is the form being used in the context of a deletion. |
| 147 | * |
| 148 | * (For some reason rather than having separate forms Civi overloads one form). |
| 149 | * |
| 150 | * @return bool |
| 151 | */ |
| 152 | protected function isDeleteContext() { |
| 153 | return ($this->getAction() & CRM_Core_Action::DELETE); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Is the form being used in the context of a view. |
| 158 | * |
| 159 | * @return bool |
| 160 | */ |
| 161 | protected function isViewContext() { |
| 162 | return ($this->getAction() & CRM_Core_Action::VIEW); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Is the form being used in the context of a edit. |
| 167 | * |
| 168 | * @return bool |
| 169 | */ |
| 170 | protected function isEditContext() { |
| 171 | return ($this->getAction() & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)); |
| 172 | } |
| 173 | |
| 174 | } |