/**
  * Magic method for get/set/add/del relations
  *
  * @param string $name
  * @param array $arg
  * @throws CentreonClapiException
  */
 public function __call($name, $arg)
 {
     $name = strtolower($name);
     if (!isset($arg[0])) {
         throw new CentreonClapiException(self::MISSINGPARAMETER);
     }
     $args = explode($this->delim, $arg[0]);
     $cgIds = $this->object->getIdByParameter($this->object->getUniqueLabelField(), array($args[0]));
     if (!count($cgIds)) {
         throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $args[0]);
     }
     $cgId = $cgIds[0];
     if (preg_match("/^(get|set|add|del)contact\$/", $name, $matches)) {
         $relobj = new Centreon_Object_Relation_Contact_Group_Contact();
         $obj = new Centreon_Object_Contact();
         if ($matches[1] == "get") {
             $tab = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), $cgIds);
             echo "id" . $this->delim . "name" . "\n";
             foreach ($tab as $value) {
                 $tmp = $obj->getParameters($value, array($obj->getUniqueLabelField()));
                 echo $value . $this->delim . $tmp[$obj->getUniqueLabelField()] . "\n";
             }
         } else {
             if (!isset($args[1])) {
                 throw new CentreonClapiException(self::MISSINGPARAMETER);
             }
             $relation = $args[1];
             $relations = explode("|", $relation);
             $relationTable = array();
             foreach ($relations as $rel) {
                 $tab = $obj->getIdByParameter($obj->getUniqueLabelField(), array($rel));
                 if (!count($tab)) {
                     throw new CentreonClapiException(self::OBJECT_NOT_FOUND . ":" . $rel);
                 }
                 $relationTable[] = $tab[0];
             }
             if ($matches[1] == "set") {
                 $relobj->delete($cgId);
             }
             $existingRelationIds = $relobj->getTargetIdFromSourceId($relobj->getSecondKey(), $relobj->getFirstKey(), array($cgId));
             foreach ($relationTable as $relationId) {
                 if ($matches[1] == "del") {
                     $relobj->delete($cgId, $relationId);
                 } elseif ($matches[1] == "set" || $matches[1] == "add") {
                     if (!in_array($relationId, $existingRelationIds)) {
                         $relobj->insert($cgId, $relationId);
                     }
                 }
             }
             $acl = new CentreonACL();
             $acl->reload(true);
         }
     } else {
         throw new CentreonClapiException(self::UNKNOWN_METHOD);
     }
 }
 /**
  * Insert audit log
  *
  * @param string $actionType
  * @param int $objId
  * @param string $objName
  * @param array $objValues
  */
 public function addAuditLog($actionType, $objId, $objName, $objValues = array())
 {
     $objType = strtoupper($this->action);
     $objectTypes = array('HTPL' => 'host', 'STPL' => 'service', 'CONTACT' => 'contact', 'SG' => 'servicegroup', 'TP' => 'timeperiod', 'SERVICE' => 'service', 'CG' => 'contactgroup', 'CMD' => 'command', 'HOST' => 'host', 'HC' => 'hostcategories', 'HG' => 'hostgroup', 'SC' => 'servicecategories');
     if (!isset($objectTypes[$objType])) {
         return null;
     }
     $objType = $objectTypes[$objType];
     $contactObj = new Centreon_Object_Contact();
     $contact = $contactObj->getIdByParameter('contact_alias', CentreonUtils::getUserName());
     $userId = $contact[0];
     $dbstorage = Centreon_Db_Manager::factory('storage');
     $query = 'INSERT INTO log_action
         (action_log_date, object_type, object_id, object_name, action_type, log_contact_id)
         VALUES (?, ?, ?, ?, ?, ?)';
     $time = time();
     $dbstorage->query($query, array($time, $objType, $objId, $objName, $actionType, $userId));
     $query = 'SELECT MAX(action_log_id) as action_log_id
         FROM log_action
         WHERE action_log_date = ?';
     $stmt = $dbstorage->query($query, array($time));
     $row = $stmt->fetch();
     if (false === $row) {
         throw new CentreonClapiException("Error while inserting log action");
     }
     $stmt->closeCursor();
     $actionId = $row['action_log_id'];
     $query = 'INSERT INTO log_action_modification
         (field_name, field_value, action_log_id)
         VALUES (?, ?, ?)';
     foreach ($objValues as $name => $value) {
         try {
             if (is_array($value)) {
                 $value = implode(',', $value);
             }
             if (is_null($value)) {
                 $value = '';
             }
             $dbstorage->query($query, array($name, $value, $actionId));
         } catch (Exception $e) {
             throw $e;
         }
     }
 }