Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | class CRM_Contribute_BAO_ManagePremiums extends CRM_Contribute_DAO_Product { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Static holder for the default LT. |
6a488035 TO |
37 | */ |
38 | static $_defaultContributionType = NULL; | |
39 | ||
40 | /** | |
fe482240 | 41 | * Class constructor. |
6a488035 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | parent::__construct(); |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 48 | * Fetch object based on array of properties. |
6a488035 | 49 | * |
014c4014 TO |
50 | * @param array $params |
51 | * (reference ) an assoc array of name/value pairs. | |
52 | * @param array $defaults | |
53 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 54 | * |
16b10e64 | 55 | * @return CRM_Contribute_BAO_ManagePremium |
6a488035 | 56 | */ |
00be9182 | 57 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
58 | $premium = new CRM_Contribute_DAO_Product(); |
59 | $premium->copyValues($params); | |
60 | if ($premium->find(TRUE)) { | |
61 | $premium->product_name = $premium->name; | |
62 | CRM_Core_DAO::storeValues($premium, $defaults); | |
63 | return $premium; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | /** | |
fe482240 | 69 | * Update the is_active flag in the db. |
6a488035 | 70 | * |
014c4014 TO |
71 | * @param int $id |
72 | * Id of the database record. | |
73 | * @param bool $is_active | |
74 | * Value we want to set the is_active field. | |
6a488035 | 75 | * |
de22d771 | 76 | * @return bool |
6a488035 | 77 | */ |
00be9182 | 78 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
79 | if (!$is_active) { |
80 | $dao = new CRM_Contribute_DAO_PremiumsProduct(); | |
81 | $dao->product_id = $id; | |
82 | $dao->delete(); | |
83 | } | |
84 | return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active); | |
85 | } | |
86 | ||
87 | /** | |
706f6a01 | 88 | * Add a premium product to the database, and return it. |
6a488035 | 89 | * |
014c4014 TO |
90 | * @param array $params |
91 | * Reference array contains the values submitted by the form. | |
92 | * @param array $ids | |
93 | * Reference array contains the id. | |
6a488035 | 94 | * |
706f6a01 | 95 | * @return CRM_Contribute_DAO_Product |
6a488035 | 96 | */ |
00be9182 | 97 | public static function add(&$params, &$ids) { |
bb80d2f9 | 98 | $params = array_merge(array( |
706f6a01 | 99 | 'id' => CRM_Utils_Array::value('premium', $ids), |
b28ddfe8 SM |
100 | 'image' => '', |
101 | 'thumbnail' => '', | |
bb80d2f9 | 102 | 'is_active' => 0, |
706f6a01 SM |
103 | 'is_deductible' => FALSE, |
104 | 'currency' => CRM_Core_Config::singleton()->defaultCurrency, | |
bb80d2f9 | 105 | ), $params); |
6a488035 | 106 | |
b28ddfe8 SM |
107 | // Modify the submitted values for 'image' and 'thumbnail' so that we use |
108 | // local URLs for these images when possible. | |
109 | $params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE); | |
110 | $params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE); | |
111 | ||
706f6a01 | 112 | // Save and return |
6a488035 TO |
113 | $premium = new CRM_Contribute_DAO_Product(); |
114 | $premium->copyValues($params); | |
6a488035 TO |
115 | $premium->save(); |
116 | return $premium; | |
117 | } | |
118 | ||
119 | /** | |
fe482240 | 120 | * Delete premium Types. |
6a488035 TO |
121 | * |
122 | * @param int $productID | |
de22d771 | 123 | * |
124 | * @throws \CRM_Core_Exception | |
6a488035 | 125 | */ |
00be9182 | 126 | public static function del($productID) { |
6a488035 TO |
127 | //check dependencies |
128 | $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct(); | |
129 | $premiumsProduct->product_id = $productID; | |
130 | if ($premiumsProduct->find(TRUE)) { | |
131 | $session = CRM_Core_Session::singleton(); | |
132 | $message .= ts('This Premium is being linked to <a href=\'%1\'>Online Contribution page</a>. Please remove it in order to delete this Premium.', array(1 => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')), ts('Deletion Error'), 'error'); | |
133 | CRM_Core_Session::setStatus($message); | |
134 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse')); | |
135 | } | |
136 | ||
874c9be7 | 137 | //delete from financial Type table |
6a488035 TO |
138 | $premium = new CRM_Contribute_DAO_Product(); |
139 | $premium->id = $productID; | |
140 | $premium->delete(); | |
141 | } | |
96025800 | 142 | |
6a488035 | 143 | } |