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