Merge pull request #18404 from mattwire/membershipfrontenddep
[civicrm-core.git] / CRM / Grant / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class handle grant related functions
20 *
21 */
22class CRM_Grant_Page_Tab extends CRM_Contact_Page_View {
23
24 /**
fe482240 25 * The action links that we need to display for the browse screen.
6a488035
TO
26 *
27 * @var array
6a488035 28 */
7e8c8317 29 public static $_links = NULL;
6a488035
TO
30 public $_permission = NULL;
31 public $_contactId = NULL;
32
33 /**
fe482240 34 * called when action is browse.
6a488035 35 *
95ea96be 36 */
79d7553f 37 public function browse() {
6a488035
TO
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);
4e8065a9 49 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('grant', $this->_contactId);
6a488035
TO
50 }
51 }
52
53 /**
fe482240 54 * called when action is view.
6a488035 55 *
76e7a76c 56 * @return null
6a488035 57 */
00be9182 58 public function view() {
6a488035
TO
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 /**
fe482240 68 * called when action is update or new.
6a488035 69 *
76e7a76c 70 * @return null
6a488035 71 */
00be9182 72 public function edit() {
6a488035
TO
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 /**
fe482240 82 * Build all the data structures needed to build the form.
6a488035
TO
83 *
84 * @return void
6a488035 85 */
00be9182 86 public function preProcess() {
edc80cda 87 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 88 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
353ffa53 89 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
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);
6a488035
TO
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 /**
dc195289 113 * the main function that is called when the page loads,
6a488035
TO
114 * it decides the which action has to be taken for the page.
115 *
76e7a76c 116 * @return null
6a488035 117 */
00be9182 118 public function run() {
6a488035
TO
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
00be9182 135 public function setContext() {
edc80cda 136 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 137 $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
353ffa53 138 $session = CRM_Core_Session::singleton();
6a488035
TO
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':
c33f1df1 162 $url = CRM_Utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant');
6a488035
TO
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
1273d77c 183 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
6a488035
TO
184 CRM_Grant_BAO_Grant::del($this->_id);
185 CRM_Utils_System::redirect($url);
186 }
187 }
96025800 188
6a488035 189}