Merge pull request #15351 from civicrm/5.18
[civicrm-core.git] / CRM / Contribute / Page / ManagePremiums.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * Page for displaying list of Premiums.
36 */
37 class CRM_Contribute_Page_ManagePremiums extends CRM_Core_Page_Basic {
38
39 public $useLivePageJS = TRUE;
40
41 /**
42 * The action links that we need to display for the browse screen.
43 *
44 * @var array
45 */
46 public static $_links = NULL;
47
48 /**
49 * Get BAO Name.
50 *
51 * @return string
52 * Classname of BAO.
53 */
54 public function getBAOName() {
55 return 'CRM_Contribute_BAO_Product';
56 }
57
58 /**
59 * Get action Links.
60 *
61 * @return array
62 * (reference) of action links
63 */
64 public function &links() {
65 if (!(self::$_links)) {
66 self::$_links = [
67 CRM_Core_Action::UPDATE => [
68 'name' => ts('Edit'),
69 'url' => 'civicrm/admin/contribute/managePremiums',
70 'qs' => 'action=update&id=%%id%%&reset=1',
71 'title' => ts('Edit Premium'),
72 ],
73 CRM_Core_Action::PREVIEW => [
74 'name' => ts('Preview'),
75 'url' => 'civicrm/admin/contribute/managePremiums',
76 'qs' => 'action=preview&id=%%id%%',
77 'title' => ts('Preview Premium'),
78 ],
79 CRM_Core_Action::DISABLE => [
80 'name' => ts('Disable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Disable Premium'),
83 ],
84 CRM_Core_Action::ENABLE => [
85 'name' => ts('Enable'),
86 'ref' => 'crm-enable-disable',
87 'title' => ts('Enable Premium'),
88 ],
89 CRM_Core_Action::DELETE => [
90 'name' => ts('Delete'),
91 'url' => 'civicrm/admin/contribute/managePremiums',
92 'qs' => 'action=delete&id=%%id%%',
93 'title' => ts('Delete Premium'),
94 ],
95 ];
96 }
97 return self::$_links;
98 }
99
100 /**
101 * Run the page.
102 *
103 * This method is called after the page is created. It checks for the
104 * type of action and executes that action.
105 * Finally it calls the parent's run method.
106 */
107 public function run() {
108 $id = $this->getIdAndAction();
109
110 // what action to take ?
111 if (!($this->_action & CRM_Core_Action::BROWSE)) {
112 $this->edit($this->_action, $id, TRUE);
113 }
114 // finally browse the custom groups
115 $this->browse();
116
117 // parent run
118 return CRM_Core_Page::run();
119 }
120
121 /**
122 * Browse all custom data groups.
123 */
124 public function browse() {
125 // get all custom groups sorted by weight
126 $premiums = [];
127 $dao = new CRM_Contribute_DAO_Product();
128 $dao->orderBy('name');
129 $dao->find();
130
131 while ($dao->fetch()) {
132 $premiums[$dao->id] = [];
133 CRM_Core_DAO::storeValues($dao, $premiums[$dao->id]);
134 // form all action links
135 $action = array_sum(array_keys($this->links()));
136
137 if ($dao->is_active) {
138 $action -= CRM_Core_Action::ENABLE;
139 }
140 else {
141 $action -= CRM_Core_Action::DISABLE;
142 }
143
144 $premiums[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(),
145 $action,
146 ['id' => $dao->id],
147 ts('more'),
148 FALSE,
149 'premium.manage.row',
150 'Premium',
151 $dao->id
152 );
153 // Financial Type
154 if (!empty($dao->financial_type_id)) {
155 $premiums[$dao->id]['financial_type'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Product', 'financial_type_id', $dao->financial_type_id);
156 }
157 }
158 $this->assign('rows', $premiums);
159 }
160
161 /**
162 * Get name of edit form.
163 *
164 * @return string
165 * Classname of edit form.
166 */
167 public function editForm() {
168 return 'CRM_Contribute_Form_ManagePremiums';
169 }
170
171 /**
172 * Get edit form name.
173 *
174 * @return string
175 * name of this page.
176 */
177 public function editName() {
178 return 'Manage Premiums';
179 }
180
181 /**
182 * Get user context.
183 *
184 * @param null $mode
185 *
186 * @return string
187 * user context.
188 */
189 public function userContext($mode = NULL) {
190 return 'civicrm/admin/contribute/managePremiums';
191 }
192
193 }