Merge pull request #22584 from civicrm/5.46
[civicrm-core.git] / CRM / Contribute / BAO / Product.php
CommitLineData
37828d4f
MW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
37828d4f 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 |
37828d4f
MW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
37828d4f
MW
16 */
17class CRM_Contribute_BAO_Product extends CRM_Contribute_DAO_Product {
18
19 /**
20 * Static holder for the default LT.
1330f57a 21 * @var int
37828d4f 22 */
1330f57a 23 public static $_defaultContributionType = NULL;
37828d4f 24
37828d4f
MW
25 /**
26 * Fetch object based on array of properties.
27 *
28 * @param array $params
29 * (reference ) an assoc array of name/value pairs.
30 * @param array $defaults
31 * (reference ) an assoc array to hold the flattened values.
32 *
33 * @return CRM_Contribute_BAO_Product
34 */
35 public static function retrieve(&$params, &$defaults) {
36 $premium = new CRM_Contribute_DAO_Product();
37 $premium->copyValues($params);
38 if ($premium->find(TRUE)) {
39 $premium->product_name = $premium->name;
40 CRM_Core_DAO::storeValues($premium, $defaults);
41 return $premium;
42 }
43 return NULL;
44 }
45
46 /**
47 * Update the is_active flag in the db.
48 *
49 * @param int $id
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
53 *
54 * @return bool
55 */
56 public static function setIsActive($id, $is_active) {
57 if (!$is_active) {
58 $dao = new CRM_Contribute_DAO_PremiumsProduct();
59 $dao->product_id = $id;
60 $dao->delete();
61 }
62 return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Product', $id, 'is_active', $is_active);
63 }
64
65 /**
66 * Add a premium product to the database, and return it.
67 *
68 * @param array $params
32f27499 69 * Update parameters.
37828d4f
MW
70 *
71 * @return CRM_Contribute_DAO_Product
72 */
32f27499 73 public static function create($params) {
9c1bc317 74 $id = $params['id'] ?? NULL;
7e49aa93 75 $op = !empty($id) ? 'edit' : 'create';
048fa8d6
MW
76 if (empty($id)) {
77 $defaultParams = [
78 'id' => $id,
79 'image' => '',
80 'thumbnail' => '',
81 'is_active' => 0,
82 'is_deductible' => FALSE,
83 'currency' => CRM_Core_Config::singleton()->defaultCurrency,
84 ];
85 $params = array_merge($defaultParams, $params);
86 }
7e49aa93 87 CRM_Utils_Hook::pre($op, 'Product', $id, $params);
37828d4f
MW
88 // Modify the submitted values for 'image' and 'thumbnail' so that we use
89 // local URLs for these images when possible.
048fa8d6
MW
90 if (isset($params['image'])) {
91 $params['image'] = CRM_Utils_String::simplifyURL($params['image'], TRUE);
92 }
93 if (isset($params['thumbnail'])) {
94 $params['thumbnail'] = CRM_Utils_String::simplifyURL($params['thumbnail'], TRUE);
95 }
37828d4f
MW
96
97 // Save and return
98 $premium = new CRM_Contribute_DAO_Product();
99 $premium->copyValues($params);
100 $premium->save();
7e49aa93 101 CRM_Utils_Hook::post($op, 'Product', $id, $premium);
37828d4f
MW
102 return $premium;
103 }
104
105 /**
106 * Delete premium Types.
107 *
108 * @param int $productID
1d4cebf3 109 * @deprecated
37828d4f
MW
110 * @throws \CRM_Core_Exception
111 */
112 public static function del($productID) {
1d4cebf3 113 static::deleteRecord(['id' => $productID]);
37828d4f
MW
114 }
115
116}