Beispiel #1
0
 public static function Set($messageId, $params = array())
 {
     global $DB;
     $messageId = intval($messageId);
     if (!(is_array($params) || is_null($params)) || $messageId <= 0) {
         return false;
     }
     if (is_null($params) || count($params) <= 0) {
         $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID'), 'filter' => array('=MESSAGE_ID' => $messageId)));
         while ($parameterInfo = $messageParameters->fetch()) {
             IM\MessageParamTable::delete($parameterInfo['ID']);
         }
         return true;
     }
     $default = self::GetDefault();
     $arToDelete = array();
     foreach ($params as $key => $val) {
         if (isset($default[$key]) && $default[$key] == $val) {
             $arToDelete[$key] = array("=MESSAGE_ID" => $messageId, "=PARAM_NAME" => $key);
         }
     }
     $arToInsert = array();
     foreach ($params as $k1 => $v1) {
         $name = substr(trim($k1), 0, 100);
         if (strlen($name)) {
             if (!is_array($v1)) {
                 $v1 = array($v1);
             }
             if (empty($v1)) {
                 $arToDelete[$name] = array("=MESSAGE_ID" => $messageId, "=PARAM_NAME" => $name);
             } else {
                 foreach ($v1 as $v2) {
                     $value = substr(trim($v2), 0, 100);
                     if (strlen($value)) {
                         $key = md5($name) . md5($value);
                         $arToInsert[$key] = array("MESSAGE_ID" => $messageId, "PARAM_NAME" => $name, "PARAM_VALUE" => $value);
                     }
                 }
             }
         }
     }
     if (!empty($arToInsert)) {
         $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID', 'PARAM_NAME', 'PARAM_VALUE'), 'filter' => array('=MESSAGE_ID' => $messageId)));
         while ($ar = $messageParameters->fetch()) {
             $key = md5($ar["PARAM_NAME"]) . md5($ar["PARAM_VALUE"]);
             if (array_key_exists($key, $arToInsert)) {
                 unset($arToInsert[$key]);
             } else {
                 if (isset($params[$ar["PARAM_NAME"]])) {
                     IM\MessageParamTable::delete($ar['ID']);
                 }
             }
         }
     }
     foreach ($arToInsert as $parameterInfo) {
         IM\MessageParamTable::add($parameterInfo);
     }
     foreach ($arToDelete as $filter) {
         $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID'), 'filter' => $filter));
         while ($parameterInfo = $messageParameters->fetch()) {
             IM\MessageParamTable::delete($parameterInfo['ID']);
         }
     }
 }
 public static function Set($messageId, $params = array())
 {
     $messageId = intval($messageId);
     if (!(is_array($params) || is_null($params)) || $messageId <= 0) {
         return false;
     }
     if (is_null($params) || count($params) <= 0) {
         return self::DeleteAll($messageId);
     }
     $default = self::GetDefault();
     $arToDelete = array();
     foreach ($params as $key => $val) {
         if (isset($default[$key]) && $default[$key] == $val) {
             $arToDelete[$key] = array("=MESSAGE_ID" => $messageId, "=PARAM_NAME" => $key);
         }
     }
     $arToInsert = array();
     foreach ($params as $k1 => $v1) {
         $name = substr(trim($k1), 0, 100);
         if (strlen($name)) {
             if (is_object($v1) && $v1 instanceof CIMMessageParamAttach) {
                 $v1 = array($v1);
             } else {
                 if (is_array($v1) && \Bitrix\Main\Type\Collection::isAssociative($v1)) {
                     $v1 = array($v1);
                 } else {
                     if (!is_array($v1)) {
                         $v1 = array($v1);
                     }
                 }
             }
             if (empty($v1)) {
                 $arToDelete[$name] = array("=MESSAGE_ID" => $messageId, "=PARAM_NAME" => $name);
             } else {
                 foreach ($v1 as $v2) {
                     if (is_array($v2)) {
                         $value = \Bitrix\Main\Web\Json::encode($v2);
                         if (strlen($value)) {
                             $key = md5($name . $value);
                             $arToInsert[$key] = array("MESSAGE_ID" => $messageId, "PARAM_NAME" => $name, "PARAM_VALUE" => isset($v2['ID']) ? $v2['ID'] : time(), "PARAM_JSON" => $value);
                         }
                     } else {
                         if (is_object($v2) && $v2 instanceof CIMMessageParamAttach) {
                             $value = $v2->GetJSON();
                             $valueArray = $v2->GetArray();
                             if (strlen($value)) {
                                 $key = md5($name . $value);
                                 $arToInsert[$key] = array("MESSAGE_ID" => $messageId, "PARAM_NAME" => $name, "PARAM_VALUE" => $valueArray['ID'], "PARAM_JSON" => $value);
                             }
                         } else {
                             $value = substr(trim($v2), 0, 100);
                             if (strlen($value)) {
                                 $key = md5($name . $value);
                                 $arToInsert[$key] = array("MESSAGE_ID" => $messageId, "PARAM_NAME" => $name, "PARAM_VALUE" => $value);
                             }
                         }
                     }
                 }
             }
         }
     }
     if (!empty($arToInsert)) {
         $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID', 'PARAM_NAME', 'PARAM_VALUE', 'PARAM_JSON'), 'filter' => array('=MESSAGE_ID' => $messageId)));
         while ($ar = $messageParameters->fetch()) {
             if (strlen($ar['PARAM_JSON'])) {
                 $key = md5($ar["PARAM_NAME"] . $ar["PARAM_JSON"]);
             } else {
                 $key = md5($ar["PARAM_NAME"] . $ar["PARAM_VALUE"]);
             }
             if (array_key_exists($key, $arToInsert)) {
                 unset($arToInsert[$key]);
             } else {
                 if (isset($params[$ar["PARAM_NAME"]])) {
                     IM\MessageParamTable::delete($ar['ID']);
                 }
             }
         }
     }
     foreach ($arToInsert as $parameterInfo) {
         IM\MessageParamTable::add($parameterInfo);
     }
     foreach ($arToDelete as $filter) {
         $messageParameters = IM\MessageParamTable::getList(array('select' => array('ID'), 'filter' => $filter));
         while ($parameterInfo = $messageParameters->fetch()) {
             IM\MessageParamTable::delete($parameterInfo['ID']);
         }
     }
     return true;
 }