Merge pull request #22865 from demeritcowboy/contributionview
[civicrm-core.git] / CRM / Mailing / BAO / MailingComponent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Mailing_BAO_MailingComponent extends CRM_Mailing_DAO_MailingComponent {
18
19 /**
20 * Retrieve DB object and copy to defaults array.
21 *
22 * @param array $params
23 * Array of criteria values.
24 * @param array $defaults
25 * Array to be populated with found values.
26 *
27 * @return self|null
28 * The DAO object, if found.
29 *
30 * @deprecated
31 */
32 public static function retrieve($params, &$defaults) {
33 return self::commonRetrieve(self::class, $params, $defaults);
34 }
35
36 /**
37 * Update the is_active flag in the db.
38 *
39 * @param int $id
40 * Id of the database record.
41 * @param bool $is_active
42 * Value we want to set the is_active field.
43 *
44 * @return bool
45 * true if we found and updated the object, else false
46 */
47 public static function setIsActive($id, $is_active) {
48 return CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_MailingComponent', $id, 'is_active', $is_active);
49 }
50
51 /**
52 * Create and Update mailing component.
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $ids
57 * (deprecated) the array that holds all the db ids.
58 *
59 * @return CRM_Mailing_BAO_MailingComponent
60 */
61 public static function add(&$params, $ids = []) {
62 $id = $params['id'] ?? $ids['id'] ?? NULL;
63 $component = new CRM_Mailing_BAO_MailingComponent();
64 if ($id) {
65 $component->id = $id;
66 $component->find(TRUE);
67 }
68
69 $component->copyValues($params);
70 if (empty($id) && empty($params['body_text'])) {
71 $component->body_text = CRM_Utils_String::htmlToText(CRM_Utils_Array::value('body_html', $params));
72 }
73
74 if ($component->is_default) {
75 if (!empty($id)) {
76 $sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1 AND id <> %2';
77 $sqlParams = [
78 1 => [$component->component_type, 'String'],
79 2 => [$id, 'Positive'],
80 ];
81 }
82 else {
83 $sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1';
84 $sqlParams = [
85 1 => [$component->component_type, 'String'],
86 ];
87 }
88 CRM_Core_DAO::executeQuery($sql, $sqlParams);
89 }
90
91 $component->save();
92 return $component;
93 }
94
95 }