Return FALSE instead of throwing Exception if package throws Exception
[civicrm-core.git] / CRM / Contribute / BAO / Product.php
CommitLineData
37828d4f
MW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
37828d4f
MW
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
37828d4f
MW
32 */
33class CRM_Contribute_BAO_Product extends CRM_Contribute_DAO_Product {
34
35 /**
36 * Static holder for the default LT.
1330f57a 37 * @var int
37828d4f 38 */
1330f57a 39 public static $_defaultContributionType = NULL;
37828d4f
MW
40
41 /**
42 * Class constructor.
43 */
44 public function __construct() {
45 parent::__construct();
46 }
47
48 /**
49 * Fetch object based on array of properties.
50 *
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
53 * @param array $defaults
54 * (reference ) an assoc array to hold the flattened values.
55 *
56 * @return CRM_Contribute_BAO_Product
57 */
58 public static function retrieve(&$params, &$defaults) {
59 $premium = new CRM_Contribute_DAO_Product();
60 $premium->copyValues($params);
61 if ($premium->find(TRUE)) {
62 $premium->product_name = $premium->name;
63 CRM_Core_DAO::storeValues($premium, $defaults);
64 return $premium;
65 }
66 return NULL;
67 }
68
69 /**
70 * Update the is_active flag in the db.
71 *
72 * @param int $id
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
76 *
77 * @return bool
78 */
79 public static function setIsActive($id, $is_active) {
80 if (!$is_active) {
81 $dao = new CRM_Contribute_DAO_PremiumsProduct();
82 $dao->product_id = $id;
83 $dao->delete();
84 }
85 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
86 }
87
88 /**
89 * Add a premium product to the database, and return it.
90 *
91 * @param array $params
32f27499 92 * Update parameters.
37828d4f
MW
93 *
94 * @return CRM_Contribute_DAO_Product
95 */
32f27499 96 public static function create($params) {
97 $id = CRM_Utils_Array::value('id', $params);
048fa8d6
MW
98 if (empty($id)) {
99 $defaultParams = [
100 'id' => $id,
101 'image' => '',
102 'thumbnail' => '',
103 'is_active' => 0,
104 'is_deductible' => FALSE,
105 'currency' => CRM_Core_Config::singleton()->defaultCurrency,
106 ];
107 $params = array_merge($defaultParams, $params);
108 }
37828d4f
MW
109
110 // Modify the submitted values for 'image' and 'thumbnail' so that we use
111 // local URLs for these images when possible.
048fa8d6
MW
112 if (isset($params['image'])) {
113 $params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE);
114 }
115 if (isset($params['thumbnail'])) {
116 $params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE);
117 }
37828d4f
MW
118
119 // Save and return
120 $premium = new CRM_Contribute_DAO_Product();
121 $premium->copyValues($params);
122 $premium->save();
123 return $premium;
124 }
125
126 /**
127 * Delete premium Types.
128 *
129 * @param int $productID
130 *
131 * @throws \CRM_Core_Exception
132 */
133 public static function del($productID) {
134 //check dependencies
135 $premiumsProduct = new CRM_Contribute_DAO_PremiumsProduct();
136 $premiumsProduct->product_id = $productID;
137 if ($premiumsProduct->find(TRUE)) {
a275d4b6 138 throw new CRM_Core_Exception('Cannot delete a Premium that is linked to a Contribution page');
37828d4f 139 }
a275d4b6 140 // delete product
37828d4f
MW
141 $premium = new CRM_Contribute_DAO_Product();
142 $premium->id = $productID;
143 $premium->delete();
144 }
145
146}