| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class handle grant related functions |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Grant_Page_Tab extends CRM_Contact_Page_View { |
| 41 | |
| 42 | /** |
| 43 | * The action links that we need to display for the browse screen. |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | static $_links = NULL; |
| 48 | public $_permission = NULL; |
| 49 | public $_contactId = NULL; |
| 50 | |
| 51 | /** |
| 52 | * called when action is browse. |
| 53 | * |
| 54 | */ |
| 55 | public function browse() { |
| 56 | $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Search', ts('Grants'), $this->_action); |
| 57 | $controller->setEmbedded(TRUE); |
| 58 | $controller->reset(); |
| 59 | $controller->set('cid', $this->_contactId); |
| 60 | $controller->set('context', 'grant'); |
| 61 | $controller->process(); |
| 62 | $controller->run(); |
| 63 | |
| 64 | if ($this->_contactId) { |
| 65 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); |
| 66 | $this->assign('displayName', $displayName); |
| 67 | $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('grant', $this->_contactId); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * called when action is view. |
| 73 | * |
| 74 | * @return null |
| 75 | */ |
| 76 | public function view() { |
| 77 | $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_GrantView', 'View Grant', $this->_action); |
| 78 | $controller->setEmbedded(TRUE); |
| 79 | $controller->set('id', $this->_id); |
| 80 | $controller->set('cid', $this->_contactId); |
| 81 | |
| 82 | return $controller->run(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * called when action is update or new. |
| 87 | * |
| 88 | * @return null |
| 89 | */ |
| 90 | public function edit() { |
| 91 | $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Grant', 'Create grant', $this->_action); |
| 92 | $controller->setEmbedded(TRUE); |
| 93 | $controller->set('id', $this->_id); |
| 94 | $controller->set('cid', $this->_contactId); |
| 95 | |
| 96 | return $controller->run(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Build all the data structures needed to build the form. |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function preProcess() { |
| 105 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 106 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 107 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 108 | |
| 109 | if ($context == 'standalone') { |
| 110 | $this->_action = CRM_Core_Action::ADD; |
| 111 | } |
| 112 | else { |
| 113 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); |
| 114 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); |
| 115 | $this->assign('contactId', $this->_contactId); |
| 116 | $this->assign('displayName', $displayName); |
| 117 | |
| 118 | // check logged in url permission |
| 119 | CRM_Contact_Page_View::checkUserPermission($this); |
| 120 | } |
| 121 | $this->assign('action', $this->_action); |
| 122 | |
| 123 | if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit grants')) { |
| 124 | // demote to view since user does not have edit grant rights |
| 125 | $this->_permission = CRM_Core_Permission::VIEW; |
| 126 | $this->assign('permission', 'view'); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * the main function that is called when the page loads, |
| 132 | * it decides the which action has to be taken for the page. |
| 133 | * |
| 134 | * @return null |
| 135 | */ |
| 136 | public function run() { |
| 137 | $this->preProcess(); |
| 138 | |
| 139 | $this->setContext(); |
| 140 | |
| 141 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 142 | $this->view(); |
| 143 | } |
| 144 | elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) { |
| 145 | $this->edit(); |
| 146 | } |
| 147 | else { |
| 148 | $this->browse(); |
| 149 | } |
| 150 | return parent::run(); |
| 151 | } |
| 152 | |
| 153 | public function setContext() { |
| 154 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 155 | $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this); |
| 156 | $session = CRM_Core_Session::singleton(); |
| 157 | |
| 158 | $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this); |
| 159 | //validate the qfKey |
| 160 | if (!CRM_Utils_Rule::qfKey($qfKey)) { |
| 161 | $qfKey = NULL; |
| 162 | } |
| 163 | |
| 164 | switch ($context) { |
| 165 | case 'search': |
| 166 | $urlParams = 'force=1'; |
| 167 | if ($qfKey) { |
| 168 | $urlParams .= "&qfKey=$qfKey"; |
| 169 | } |
| 170 | $this->assign('searchKey', $qfKey); |
| 171 | |
| 172 | $url = CRM_Utils_System::url('civicrm/grant/search', $urlParams); |
| 173 | break; |
| 174 | |
| 175 | case 'dashboard': |
| 176 | $url = CRM_Utils_System::url('civicrm/grant', 'reset=1'); |
| 177 | break; |
| 178 | |
| 179 | case 'edit': |
| 180 | $url = CRM_utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant'); |
| 181 | break; |
| 182 | |
| 183 | case 'grant': |
| 184 | $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=grant&cid=' . $this->_contactId); |
| 185 | break; |
| 186 | |
| 187 | case 'standalone': |
| 188 | $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); |
| 189 | break; |
| 190 | |
| 191 | default: |
| 192 | $cid = NULL; |
| 193 | if ($this->_contactId) { |
| 194 | $cid = '&cid=' . $this->_contactId; |
| 195 | } |
| 196 | $url = CRM_Utils_System::url('civicrm/grant/search', 'reset=1&force=1' . $cid); |
| 197 | break; |
| 198 | } |
| 199 | $session->pushUserContext($url); |
| 200 | |
| 201 | if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', |
| 202 | CRM_Core_DAO::$_nullObject |
| 203 | ) |
| 204 | ) { |
| 205 | CRM_Grant_BAO_Grant::del($this->_id); |
| 206 | CRM_Utils_System::redirect($url); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | } |