Merge pull request #18404 from mattwire/membershipfrontenddep
[civicrm-core.git] / CRM / Grant / Page / Tab.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
18 /**
19 * This class handle grant related functions
20 *
21 */
22 class CRM_Grant_Page_Tab extends CRM_Contact_Page_View {
23
24 /**
25 * The action links that we need to display for the browse screen.
26 *
27 * @var array
28 */
29 public static $_links = NULL;
30 public $_permission = NULL;
31 public $_contactId = NULL;
32
33 /**
34 * called when action is browse.
35 *
36 */
37 public function browse() {
38 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Search', ts('Grants'), $this->_action);
39 $controller->setEmbedded(TRUE);
40 $controller->reset();
41 $controller->set('cid', $this->_contactId);
42 $controller->set('context', 'grant');
43 $controller->process();
44 $controller->run();
45
46 if ($this->_contactId) {
47 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
48 $this->assign('displayName', $displayName);
49 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('grant', $this->_contactId);
50 }
51 }
52
53 /**
54 * called when action is view.
55 *
56 * @return null
57 */
58 public function view() {
59 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_GrantView', 'View Grant', $this->_action);
60 $controller->setEmbedded(TRUE);
61 $controller->set('id', $this->_id);
62 $controller->set('cid', $this->_contactId);
63
64 return $controller->run();
65 }
66
67 /**
68 * called when action is update or new.
69 *
70 * @return null
71 */
72 public function edit() {
73 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Grant', 'Create grant', $this->_action);
74 $controller->setEmbedded(TRUE);
75 $controller->set('id', $this->_id);
76 $controller->set('cid', $this->_contactId);
77
78 return $controller->run();
79 }
80
81 /**
82 * Build all the data structures needed to build the form.
83 *
84 * @return void
85 */
86 public function preProcess() {
87 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
88 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
89 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
90
91 if ($context == 'standalone') {
92 $this->_action = CRM_Core_Action::ADD;
93 }
94 else {
95 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
96 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
97 $this->assign('contactId', $this->_contactId);
98 $this->assign('displayName', $displayName);
99
100 // check logged in url permission
101 CRM_Contact_Page_View::checkUserPermission($this);
102 }
103 $this->assign('action', $this->_action);
104
105 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit grants')) {
106 // demote to view since user does not have edit grant rights
107 $this->_permission = CRM_Core_Permission::VIEW;
108 $this->assign('permission', 'view');
109 }
110 }
111
112 /**
113 * the main function that is called when the page loads,
114 * it decides the which action has to be taken for the page.
115 *
116 * @return null
117 */
118 public function run() {
119 $this->preProcess();
120
121 $this->setContext();
122
123 if ($this->_action & CRM_Core_Action::VIEW) {
124 $this->view();
125 }
126 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
127 $this->edit();
128 }
129 else {
130 $this->browse();
131 }
132 return parent::run();
133 }
134
135 public function setContext() {
136 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
137 $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
138 $session = CRM_Core_Session::singleton();
139
140 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
141 //validate the qfKey
142 if (!CRM_Utils_Rule::qfKey($qfKey)) {
143 $qfKey = NULL;
144 }
145
146 switch ($context) {
147 case 'search':
148 $urlParams = 'force=1';
149 if ($qfKey) {
150 $urlParams .= "&qfKey=$qfKey";
151 }
152 $this->assign('searchKey', $qfKey);
153
154 $url = CRM_Utils_System::url('civicrm/grant/search', $urlParams);
155 break;
156
157 case 'dashboard':
158 $url = CRM_Utils_System::url('civicrm/grant', 'reset=1');
159 break;
160
161 case 'edit':
162 $url = CRM_Utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant');
163 break;
164
165 case 'grant':
166 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=grant&cid=' . $this->_contactId);
167 break;
168
169 case 'standalone':
170 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
171 break;
172
173 default:
174 $cid = NULL;
175 if ($this->_contactId) {
176 $cid = '&cid=' . $this->_contactId;
177 }
178 $url = CRM_Utils_System::url('civicrm/grant/search', 'reset=1&force=1' . $cid);
179 break;
180 }
181 $session->pushUserContext($url);
182
183 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
184 CRM_Grant_BAO_Grant::del($this->_id);
185 CRM_Utils_System::redirect($url);
186 }
187 }
188
189 }