$scenario_db->setSchedule(array()); utils::a2o($scenario_db, $scenario_ajax); $scenario_db->setConfiguration('timeDependency', $time_dependance); $scenario_db->save(); $scenario_element_list = array(); if (isset($scenario_ajax['elements'])) { foreach ($scenario_ajax['elements'] as $element_ajax) { $scenario_element_list[] = scenarioElement::saveAjaxElement($element_ajax); } $scenario_db->setScenarioElement($scenario_element_list); } $scenario_db->save(); ajax::success(utils::o2a($scenario_db)); } if (init('action') == 'actionToHtml') { ajax::success(scenarioExpression::getExpressionOptions(init('expression'), init('option'))); } if (init('action') == 'templateupload') { $uploaddir = dirname(__FILE__) . '/../../core/config/scenario/'; if (!file_exists($uploaddir)) { mkdir($uploaddir); } if (!file_exists($uploaddir)) { throw new Exception(__('Répertoire d\'upload non trouvé : ', __FILE__) . $uploaddir); } if (!isset($_FILES['file'])) { throw new Exception(__('Aucun fichier trouvé. Vérifié parametre PHP (post size limit)', __FILE__)); } $extension = strtolower(strrchr($_FILES['file']['name'], '.')); if (!in_array($extension, array('.json'))) { throw new Exception('Extension du fichier non valide (autorisé .json) : ' . $extension);
public function executeAndReply($_parameters) { $interactDef = interactDef::byId($this->getInteractDef_id()); if (!is_object($interactDef)) { return __('Inconsistance de la base de données', __FILE__); } if (isset($_parameters['profile']) && trim($interactDef->getPerson()) != '') { $person = strtolower($interactDef->getPerson()); $person = explode('|', $person); if (!in_array($_parameters['profile'], $person)) { return __('Vous n\'êtes pas autorisé à exécuter cette action', __FILE__); } } $reply = $interactDef->selectReply(); $replace = array(); $tags = interactDef::getTagFromQuery($this->getQuery(), $_parameters['dictation']); $tags_replace = array(); foreach ($tags as $key => $value) { $tags_replace['#' . $key . '#'] = $value; $replace['#' . $key . '#'] = $value; } $executeDate = null; $dateConvert = array('heure' => 'hour', 'mois' => 'month', 'semaine' => 'week', 'année' => 'year'); if (isset($tags_replace['#duration#'])) { $tags_replace['#duration#'] = str_replace(array_keys($dateConvert), $dateConvert, $tags_replace['#duration#']); $executeDate = strtotime('+' . $tags_replace['#duration#']); } if (isset($tags_replace['#time#'])) { $time = str_replace(array('h'), array(':'), $tags_replace['#time#']); if (strlen($time) == 2) { $time .= ':00'; } else { if (strlen($time) == 3) { $time .= '00'; } } $executeDate = strtotime($time); if ($executeDate < strtotime('now')) { $executeDate += 3600; } } if ($executeDate !== null && !isset($_parameters['execNow'])) { if (date('Y', $executeDate) < 2000) { return __('Erreur impossible de calculer la date de programmation', __FILE__); } if ($executeDate < strtotime('now') + 60) { $executeDate = strtotime('now') + 60; } $crons = cron::searchClassAndFunction('interactQuery', 'doIn', '"interactQuery_id":' . $this->getId()); if (is_array($crons)) { foreach ($crons as $cron) { if ($cron->getState() != 'run') { $cron->remove(); } } } $cron = new cron(); $cron->setClass('interactQuery'); $cron->setFunction('doIn'); $cron->setOption(array_merge(array('interactQuery_id' => intval($this->getId())), $_parameters)); $cron->setLastRun(date('Y-m-d H:i:s')); $cron->setOnce(1); $cron->setSchedule(date('i', $executeDate) . ' ' . date('H', $executeDate) . ' ' . date('d', $executeDate) . ' ' . date('m', $executeDate) . ' * ' . date('Y', $executeDate)); $cron->save(); $replace['#value#'] = date('Y-m-d H:i:s', $executeDate); $result = scenarioExpression::setTags(str_replace(array_keys($replace), $replace, $reply)); return $result; } $colors = config::byKey('convertColor'); foreach ($this->getActions('cmd') as $action) { try { $options = array(); if (isset($action['options'])) { $options = $action['options']; } if ($tags != null) { foreach ($options as &$option) { $option = str_replace(array_keys($tags_replace), $tags_replace, $option); } if (isset($options['color']) && isset($colors[strtolower($options['color'])])) { $options['color'] = $colors[strtolower($options['color'])]; } } $cmd = cmd::byId(str_replace('#', '', $action['cmd'])); if (is_object($cmd) && $cmd->getType() == 'info') { $replace['#unite#'] = $cmd->getUnite(); $replace['#commande#'] = $cmd->getName(); $replace['#objet#'] = ''; $replace['#equipement#'] = ''; $eqLogic = $cmd->getEqLogic(); if (is_object($eqLogic)) { $replace['#equipement#'] = $eqLogic->getName(); $object = $eqLogic->getObject(); if (is_object($object)) { $replace['#objet#'] = $object->getName(); } } } $options['tags'] = $tags_replace; $return = scenarioExpression::createAndExec('action', $action['cmd'], $options); if (trim($return) != '') { $replace['#valeur#'] = $return; } } catch (Exception $e) { log::add('interact', 'error', __('Erreur lors de l\'éxecution de ', __FILE__) . $action['cmd'] . __('. Détails : ', __FILE__) . $e->getMessage()); } } $replace['#profile#'] = isset($_parameters['profile']) ? $_parameters['profile'] : ''; if ($interactDef->getOptions('convertBinary') != '') { $convertBinary = $interactDef->getOptions('convertBinary'); $convertBinary = explode('|', $convertBinary); $replace['1'] = $convertBinary[1]; $replace['0'] = $convertBinary[0]; } $result = scenarioExpression::setTags(str_replace(array_keys($replace), $replace, $reply)); return $result; }
public static function evaluateExpression($_input) { try { $_input = scenarioExpression::setTags($_input); $result = evaluate($_input); if (is_bool($result) || is_numeric($result)) { return $result; } return $_input; } catch (Exception $exc) { return $_input; } }
public function event($_value, $_loop = 1) { if ($this->getLogicalId() == 'nbimpulsionminute') { try { $calcul = $this->getConfiguration('calcul'); $calcul = preg_replace("/#brut#/", $_value, $calcul); $calcul = scenarioExpression::setTags($calcul); $test = new evaluate(); $result = $test->Evaluer($calcul); parent::event($result, $_loop); } catch (Exception $e) { $EqLogic = $this->getEqLogic(); log::add('ipx800', 'error', $EqLogic->getName() . " error in " . $this->getConfiguration('calcul') . " : " . $e->getMessage()); return scenarioExpression::setTags(str_replace('"', '', cmd::cmdToValue($this->getConfiguration('calcul')))); } } else { parent::event($_value, $_loop); } }
public function getScenario() { $scenario = scenario::byElement($this->getId()); if (is_object($scenario)) { return $scenario; } $expression = scenarioExpression::byElement($this->getId()); if (is_object($expression)) { return $expression->getSubElement()->getElement()->getScenario(); } return null; }
<?php try { foreach (scenarioExpression::all() as $expression) { if ($expression->getType() == 'condition') { $value = $expression->getExpression(); $value = str_replace('==', '=', $value); $value = str_replace('=', '==', $value); $value = str_replace('<==', '<=', $value); $value = str_replace('>==', '>=', $value); $value = str_replace('!==', '!=', $value); $expression->setExpression($value); $expression->save(); } } } catch (Exception $e) { }
public static function byUsedCommand($_cmd_id, $_variable = false) { $scenarios = null; if ($_variable) { $return = array(); $expressions = array_merge(scenarioExpression::searchExpression('variable(' . $_cmd_id . ')'), scenarioExpression::searchExpression('variable', $_cmd_id, true)); } else { $return = self::byTrigger($_cmd_id); $expressions = scenarioExpression::searchExpression('#' . $_cmd_id . '#', '#' . $_cmd_id . '#', false); } if (is_array($expressions)) { foreach ($expressions as $expression) { $scenarios[] = $expression->getSubElement()->getElement()->getScenario(); } } if (is_array($scenarios)) { foreach ($scenarios as $scenario) { if (is_object($scenario)) { $find = false; foreach ($return as $existScenario) { if ($scenario->getId() == $existScenario->getId()) { $find = true; break; } } if (!$find) { $return[] = $scenario; } } } } return $return; }
public function executeAndReply($_parameters) { $interactDef = interactDef::byId($this->getInteractDef_id()); if (!is_object($interactDef)) { return __('Inconsistance de la base de données', __FILE__); } if (isset($_parameters['profile']) && trim($interactDef->getPerson()) != '') { $person = strtolower($interactDef->getPerson()); $person = explode('|', $person); if (!in_array($_parameters['profile'], $person)) { return __('Vous n\'êtes pas autorisé à exécuter cette action', __FILE__); } } if ($this->getLink_type() == 'whatDoYouKnow') { $object = object::byId($this->getLink_id()); if (is_object($object)) { $reply = self::whatDoYouKnow($object); if (trim($reply) == '') { return __('Je ne sais rien sur ', __FILE__) . $object->getName(); } return $reply; } return self::whatDoYouKnow(); } if ($this->getLink_type() == 'scenario') { $scenario = scenario::byId($this->getLink_id()); if (!is_object($scenario)) { return __('Impossible de trouver le scénario correspondant', __FILE__); } log::add('interact', 'debug', 'Execution du scénario : ' . $scenario->getHumanName() . ' => ' . $interactDef->getOptions('scenario_action')); $interactDef = $this->getInteractDef(); if (!is_object($interactDef)) { return __('Impossible de trouver la définition de l\'interaction', __FILE__); } $reply = $interactDef->selectReply(); if (trim($reply) == '') { $reply = self::replyOk(); } $replace = array(); $replace['#profile#'] = isset($_parameters['profile']) ? $_parameters['profile'] : ''; $reply = scenarioExpression::setTags(str_replace(array_keys($replace), $replace, $reply)); switch ($interactDef->getOptions('scenario_action')) { case 'start': $scenario->setTags(array('#query#' => $this->getQuery(), '#profile#' => $replace['#profile#'])); $return = $scenario->launch(false, 'interact', __('Scénario exécuté sur interaction (S.A.R.A.H, SMS...)', __FILE__), 1); if (is_string($return) && $return != '') { $return = str_replace(array_keys($replace), $replace, $return); return $return; } return $reply; case 'stop': $scenario->stop(); return $reply; case 'activate': $scenario->setIsActive(1); $scenario->save(); return $reply; case 'deactivate': $scenario->setIsActive(0); $scenario->save(); return $reply; default: return __('Aucune action n\'est définie dans l\'interaction sur le scénario : ', __FILE__) . $scenario->getHumanName(); } } $reply = $interactDef->selectReply(); $synonymes = array(); if ($interactDef->getOptions('synonymes') != '') { foreach (explode('|', $interactDef->getOptions('synonymes')) as $value) { $values = explode('=', $value); $synonymes[strtolower($values[0])] = explode(',', $values[1]); } } $replace = array(); $replace['#profile#'] = isset($_parameters['profile']) ? $_parameters['profile'] : ''; if ($this->getLink_type() == 'cmd') { foreach (explode('&&', $this->getLink_id()) as $cmd_id) { $cmd = cmd::byId($cmd_id); if (!is_object($cmd)) { continue; } $replace['#commande#'] = $cmd->getName(); if (isset($synonymes[strtolower($cmd->getName())])) { $replace['#commande#'] = $synonymes[strtolower($cmd->getName())][rand(0, count($synonymes[strtolower($cmd->getName())]) - 1)]; } $replace['#objet#'] = ''; $replace['#equipement#'] = ''; $eqLogic = $cmd->getEqLogic(); if (is_object($eqLogic)) { $replace['#equipement#'] = $eqLogic->getName(); $object = $eqLogic->getObject(); if (is_object($object)) { $replace['#objet#'] = $object->getName(); } } $replace['#unite#'] = $cmd->getUnite(); if ($cmd->getType() == 'action') { $options = null; if ($cmd->getSubType() == 'slider') { preg_match_all("/([0-9]*)/", $_parameters['dictation'], $matches); foreach ($matches[1] as $number) { if (is_numeric($number)) { $options['slider'] = $number; } } } if ($cmd->getSubType() == 'color') { $colors = config::byKey('convertColor'); foreach (explode(' ', $_parameters['dictation']) as $word) { if (isset($colors[strtolower($word)])) { $options['color'] = $colors[strtolower($word)]; } } } try { log::add('interact', 'debug', 'Execution de la commande : ' . $cmd->getHumanName() . ' => ' . print_r($options, true)); if ($cmd->execCmd($options) === false) { return __('Impossible d\'exécuter la commande', __FILE__); } } catch (Exception $exc) { return $exc->getMessage(); } if ($options != null) { foreach ($options as $key => $value) { $replace['#' . $key . '#'] = $value; } } } if ($cmd->getType() == 'info') { $value = $cmd->execCmd(); if ($value === null) { return __('Impossible de récupérer la valeur de la commande', __FILE__); } else { $replace['#valeur#'] = $value; if ($cmd->getSubType() == 'binary' && $interactDef->getOptions('convertBinary') != '') { $convertBinary = $interactDef->getOptions('convertBinary'); $convertBinary = explode('|', $convertBinary); $replace['#valeur#'] = $convertBinary[$replace['#valeur#']]; } } } } } return scenarioExpression::setTags(str_replace(array_keys($replace), $replace, $reply)); }
public function doAction($_mode, $_type) { if (!is_array($this->getConfiguration('modes'))) { return; } $actions = array(); foreach ($this->getConfiguration('modes') as $key => $value) { if ($value['name'] == $_mode) { foreach ($value[$_type] as $action) { try { $options = array(); if (isset($action['options'])) { $options = $action['options']; } scenarioExpression::createAndExec('action', $action['cmd'], $options); } catch (Exception $e) { log::add('alarm', 'error', __('Erreur lors de l\'éxecution de ', __FILE__) . $action['cmd'] . __('. Détails : ', __FILE__) . $e->getMessage()); } } return; } } }
public function getExpression() { if (count($this->_expression) > 0) { return $this->_expression; } $this->_expression = scenarioExpression::byscenarioSubElementId($this->getId()); return $this->_expression; }