Merge pull request #5338 from kurund/CRM-15756
[civicrm-core.git] / CRM / Mailing / BAO / Component.php
index 3e094e55de164d9c42e14a4644808252b7ad79b6..7a632418a3b67a246a42d7b281bbadac26837a5b 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
 class CRM_Mailing_BAO_Component extends CRM_Mailing_DAO_Component {
 
   /**
-   * class constructor
+   * Class constructor.
    */
-  function __construct() {
+  public function __construct() {
     parent::__construct();
   }
 
   /**
-   * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. Typically the valid params are only
-   * contact_id. We'll tweak this function to be more full featured over a period
-   * of time. This is the inverse function of create. It also stores all the retrieved
-   * values in the default array
+   * Fetch object based on array of properties.
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
-   * @param array $defaults (reference ) an assoc array to hold the flattened values
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $defaults
+   *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return object CRM_Core_BAO_LocaationType object
-   * @access public
-   * @static
+   * @return CRM_Core_BAO_LocaationType
    */
-  static function retrieve(&$params, &$defaults) {
+  public static function retrieve(&$params, &$defaults) {
     $component = new CRM_Mailing_DAO_Component();
     $component->copyValues($params);
     if ($component->find(TRUE)) {
@@ -66,56 +62,46 @@ class CRM_Mailing_BAO_Component extends CRM_Mailing_DAO_Component {
   }
 
   /**
-   * update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
-   * @param int      $id        id of the database record
-   * @param boolean  $is_active value we want to set the is_active field
+   * @param int $id
+   *   Id of the database record.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
-   * @return Object             DAO object on sucess, null otherwise
-   * @static
+   * @return Object
+   *   DAO object on sucess, null otherwise
    */
-  static function setIsActive($id, $is_active) {
+  public static function setIsActive($id, $is_active) {
     return CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Component', $id, 'is_active', $is_active);
   }
 
   /**
-   * Create and Update mailing component
+   * Create and Update mailing component.
    *
-   * @param array $params (reference ) an assoc array of name/value pairs
-   * @param array $ids (reference ) the array that holds all the db ids
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $ids
+   *   (deprecated) the array that holds all the db ids.
    *
-   * @return object CRM_Mailing_BAO_Component object
+   * @return CRM_Mailing_BAO_Component
    *
-   * @access public
-   * @static
    */
-  static function add(&$params, &$ids = array()) {
-    // action is taken depending upon the mode
+  public static function add(&$params, $ids = array()) {
+    $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
     $component = new CRM_Mailing_DAO_Component();
-    $component->name = $params['name'];
-    $component->component_type = CRM_Utils_Array::value('component_type', $params);
-    $component->subject = CRM_Utils_Array::value('subject', $params);
-    if (CRM_Utils_Array::value('body_text', $params)) {
-      $component->body_text = CRM_Utils_Array::value('body_text', $params);
-    }
-    else {
+    $component->id = $id;
+    $component->copyValues($params);
+    if (empty($id) && empty($params['body_text'])) {
       $component->body_text = CRM_Utils_String::htmlToText(CRM_Utils_Array::value('body_html', $params));
     }
-    $component->body_html = CRM_Utils_Array::value('body_html', $params);
-    $component->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
-    $component->is_default = CRM_Utils_Array::value('is_default', $params, FALSE);
 
-    if ($component->is_default) {
-      $query = "UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type ='{$component->component_type}'";
-      CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
+    if ($component->is_default && !empty($id)) {
+      CRM_Core_DAO::executeQuery("UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type ='{$component->component_type}' AND id <> $id");
     }
 
-    $component->id = CRM_Utils_Array::value('id', $ids);
-
     $component->save();
-
-    CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.',
-        array(1 => $component->name)
-      ), ts('Saved'), 'success');
+    return $component;
   }
+
 }