Merge pull request #18963 from samuelsov/nli18n
[civicrm-core.git] / CRM / Core / Page / EntityPageTrait.php
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 /**
128 * @return string
129 */
130 protected function getDefaultAction() {
131 return 'browse';
132 }
133
134 public function preProcessQuickEntityPage() {
135 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, $this->getDefaultAction());
136 $this->assign('action', $this->getAction());
137
138 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
139 $this->setContactId(CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, CRM_Core_Session::getLoggedInContactID()));
140
141 $this->assign('contactId', $this->getContactId());
142
143 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, $this->getDefaultContext());
144 $this->assign('context', $this->_context);
145
146 // check logged in url permission
147 CRM_Contact_Page_View::checkUserPermission($this);
148
149 $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
150 }
151
152 /**
153 * Is the form being used in the context of a deletion.
154 *
155 * (For some reason rather than having separate forms Civi overloads one form).
156 *
157 * @return bool
158 */
159 protected function isDeleteContext() {
160 return ($this->getAction() & CRM_Core_Action::DELETE);
161 }
162
163 /**
164 * Is the form being used in the context of a view.
165 *
166 * @return bool
167 */
168 protected function isViewContext() {
169 return ($this->getAction() & CRM_Core_Action::VIEW);
170 }
171
172 /**
173 * Is the form being used in the context of a edit.
174 *
175 * @return bool
176 */
177 protected function isEditContext() {
178 return ($this->getAction() & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD));
179 }
180
181 }