Esempio n. 1
0
 /**
  * Parse a route based on it's POST.
  * It's assumed here that all fields are already validated
  *
  * @param array $postData optional for ovewrite post data
  * @return PBX_Rule
  */
 protected function parseRuleFromPost($post = null)
 {
     $post = $post === null ? $_POST : $post;
     $rule = new PBX_Rule();
     // Adicionando dias da semana
     $weekDays = array("sun", "mon", "tue", "wed", "thu", "fri", "sat");
     $rule->cleanValidWeekList();
     foreach ($weekDays as $day) {
         if (in_array($day, $post['week'])) {
             $rule->addWeekDay($day);
         }
     }
     // Adicionando Origens
     foreach (explode(',', $post['srcValue']) as $src) {
         if (!strpos($src, ':')) {
             $rule->addSrc(array("type" => $src, "value" => ""));
         } else {
             $info = explode(':', $src);
             if (!is_array($info) or count($info) != 2) {
                 throw new PBX_Exception_BadArg("Valor errado para origem da regra de negocio.");
             }
             if ($info[0] == "T") {
                 try {
                     PBX_Trunks::get($info[1]);
                 } catch (PBX_Exception_NotFound $ex) {
                     throw new PBX_Exception_BadArg("Tronco inválido para origem da regra");
                 }
             }
             $rule->addSrc(array("type" => $info[0], "value" => $info[1]));
         }
     }
     // Adding destinys
     foreach (explode(',', $post['dstValue']) as $dst) {
         if (!strpos($dst, ':')) {
             $rule->addDst(array("type" => $dst, "value" => ""));
         } else {
             $info = explode(':', $dst);
             if (!is_array($info) or count($info) != 2) {
                 throw new PBX_Exception_BadArg("Valor errado para destino da regra de negocio.");
             }
             if ($info[0] == "T") {
                 try {
                     PBX_Trunks::get($info[1]);
                 } catch (PBX_Exception_NotFound $ex) {
                     throw new PBX_Exception_BadArg("Tronco inválido para destino da regra");
                 }
             }
             $rule->addDst(array("type" => $info[0], "value" => $info[1]));
         }
     }
     // Adding time
     $rule->cleanValidTimeList();
     foreach (explode(',', $post['timeValue']) as $time_period) {
         $rule->addValidTime($time_period);
     }
     // Adding Description
     $rule->setDesc($post['desc']);
     // Defining recording order
     if (isset($post['record']) && $post['record']) {
         $rule->record();
     }
     // Defining priority
     $rule->setPriority(substr($post['prio'], 1));
     if (isset($post['actions_order'])) {
         parse_str($post['actions_order'], $actions_order);
         foreach ($actions_order['actions_list'] as $action) {
             $real_action = new $post["action_{$action}"]["action_type"]();
             $action_config = new Snep_Rule_ActionConfig($real_action->getConfig());
             $real_action->setConfig($action_config->parseConfig($post["action_{$action}"]));
             $rule->addAction($real_action);
         }
     }
     return $rule;
 }
Esempio n. 2
0
 /**
  * Obtém Regras de negócio do banco de dados.
  *
  * A REGRA É DEVOLVIDA SEM A CLASSE DE COMUNICAÇÂO COM O ASTERISK
  *
  * @param int $id Numero de identificação da regra de negócio que se deseja
  * obter do banco de dados.
  * @return PBX_Rule  $regra de negócio corresponde ao id da chamada
  */
 public static function get($id)
 {
     $db = Snep_Db::getInstance();
     $select = $db->select()->from('regras_negocio')->where("id = '{$id}'");
     $regra_raw = $db->query($select)->fetchObject();
     if (!$regra_raw) {
         throw new PBX_Exception_NotFound("Regra {$id} nao encontrada");
     }
     $regra = new PBX_Rule();
     $regra->setPriority($regra_raw->prio);
     $regra->setDesc($regra_raw->desc);
     $regra->setId($id);
     foreach (explode(',', $regra_raw->origem) as $src) {
         if (!strpos($src, ':')) {
             $regra->addSrc(array("type" => $src, "value" => ""));
         } else {
             $info = explode(':', $src);
             if (!is_array($info) or count($info) != 2) {
                 throw new PBX_Exception_DatabaseIntegrity("Valor errado para origem da regra de negocio {$regra_raw->id}: {$regra_raw->origem}");
             }
             $regra->addSrc(array("type" => $info[0], "value" => $info[1]));
         }
     }
     foreach (explode(',', $regra_raw->destino) as $dst) {
         if (!strpos($dst, ':')) {
             $regra->addDst(array("type" => $dst, "value" => ""));
         } else {
             $info = explode(':', $dst);
             if (!is_array($info) or count($info) != 2) {
                 throw new PBX_Exception_DatabaseIntegrity("Valor errado para destino da regra de negocio {$regra_raw->id}: {$regra_raw->destino}");
             }
             $regra->addDst(array("type" => $info[0], "value" => $info[1]));
         }
     }
     $regra->cleanValidWeekList();
     foreach (explode(',', $regra_raw->diasDaSemana) as $diaDaSemana) {
         if ($diaDaSemana != "") {
             $regra->addWeekDay($diaDaSemana);
         }
     }
     foreach (explode(',', $regra_raw->validade) as $time) {
         $regra->addValidTime($time);
     }
     if (!$regra_raw->ativa) {
         $regra->disable();
     }
     if ($regra_raw->record == "1") {
         $regra->record();
     }
     $select = $db->select()->from('regras_negocio_actions')->where("regra_id = {$id}")->order('prio');
     $actions = $db->query($select)->fetchAll();
     // Processing rule actions
     if (count($actions) > 0) {
         $select = $db->select()->from('regras_negocio_actions_config')->where("regra_id = {$id}")->order('prio');
         $configs_raw = $db->query($select)->fetchAll();
         // Rearranging configuration array
         $configs = array();
         if (count($configs_raw) > 0) {
             foreach ($configs_raw as $config) {
                 $configs[$config['prio']][$config['key']] = $config['value'];
             }
         }
         foreach ($actions as $acao_raw) {
             $acao = $acao_raw['action'];
             if (class_exists($acao)) {
                 $config = isset($configs[$acao_raw['prio']]) ? $configs[$acao_raw['prio']] : array();
                 $acao_object = new $acao();
                 $acao_object->setConfig($config);
                 $acao_object->setDefaultConfig(PBX_Registry::getAll($acao));
                 $regra->addAcao($acao_object);
             }
         }
     }
     return $regra;
 }