コード例 #1
0
ファイル: Tag.php プロジェクト: kcristiano/civicrm-core
 /**
  * Takes an associative array and creates a contact object.
  *
  * The function extract all the params it needs to initialize the create a
  * contact object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array $params
  *   (reference) an assoc array of name/value pairs.
  * @param array $ids
  *   (optional) the array that holds all the db ids - we are moving away from this in bao.
  * signatures
  *
  * @return object
  *   CRM_Core_DAO_Tag object on success, otherwise null
  */
 public static function add(&$params, $ids = array())
 {
     $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('tag', $ids));
     if (!$id && !self::dataExists($params)) {
         return NULL;
     }
     $tag = new CRM_Core_DAO_Tag();
     // if parent id is set then inherit used for and is hidden properties
     if (!empty($params['parent_id'])) {
         // get parent details
         $params['used_for'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $params['parent_id'], 'used_for');
     }
     $tag->copyValues($params);
     $tag->id = $id;
     $hook = !$id ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'Tag', $tag->id, $params);
     // save creator id and time
     if (!$tag->id) {
         $session = CRM_Core_Session::singleton();
         $tag->created_id = $session->get('userID');
         $tag->created_date = date('YmdHis');
     }
     $tag->save();
     CRM_Utils_Hook::post($hook, 'Tag', $tag->id, $tag);
     // if we modify parent tag, then we need to update all children
     if ($tag->parent_id === 'null') {
         CRM_Core_DAO::executeQuery("UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2", array(1 => array($params['used_for'], 'String'), 2 => array($tag->id, 'Integer')));
     }
     return $tag;
 }