Example #1
0
 public function __construct($request)
 {
     // Get the token from $request which has been set in the headers
     $this->token = $request['token'];
     // Get the args which is simply the url without domain and ...
     $args = $request['args'];
     // This will check if user is authenticated, if not Auth will throw a Error(7) and kills the page
     Auth::authenticate($this->token);
     // Get the all arguments from url
     $this->args = explode('/', rtrim($args, '/'));
     // Get the Controller name
     $this->endpoint = ucfirst($this->args[0]);
     // always the first one is our endpoint , E.g : api/v1/ -> products
     // Do a loop on all arguments to find ids and popo names
     foreach ($this->args as $arg) {
         // Look for an id , either mongo id , or product id !
         if (is_mongo_id($arg)) {
             $this->id = $arg;
             continue;
             // continue if the condition is met , go next loop
         }
         // Check if there is popo with this arg in popo folder
         if (popo_exists($this->endpoint, uc_first($arg))) {
             $this->popo = uc_first($arg);
         }
     }
     // Request type
     $this->request_type = $this->get_request_method();
     // PUT and DELETE can be hidden inside of an POST request , check them :
     if ($this->request_type == 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
         if ($_SERVER['HTTP_X_HTTP_METHOD'] == "DELETE") {
             $this->request_type = 'DELETE';
         } else {
             if ($_SERVER['HTTP_X_HTTP_METHOD'] == 'PUT') {
                 $this->request_type = 'PUT';
             }
         }
     }
     // Get all inputs
     $this->input = @file_get_contents('php://input');
     $this->input = json_decode($this->input);
     // Check if request method is either POST or PUT and if yes , check if input is empty or not
     validate_input($this->input, $this->request_type);
     // Get params from GET , if is set
     if (isset($_GET)) {
         $this->params = $_GET;
         // first param is like : /produtcs/34534543  , So we dont need it
         array_shift($this->params);
     }
     // Get params from POST , if is set
     if (isset($_POST)) {
         foreach ($_POST as $k => $v) {
             $this->params[$k] = $v;
         }
     }
     // Define the protocol
     $this->protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https" : "http";
 }
Example #2
0
 public function renderJSON($from, $arrayselect, $criteria)
 {
     if (is_array($arrayselect)) {
         $return = array();
         $select = substr_replace(implode(', ', $arrayselect), '', -2);
         $select1 = $arrayselect[0];
         $qtxt = "SELECT {$select} FROM {$from} WHERE {$select1} LIKE :{$select1}";
         $wheres = array();
         if ($criteria != array()) {
             foreach ($criteria as $parcol => $value) {
                 foreach ($value as $parenttbl => $vcol) {
                     foreach ($vcol as $col => $val) {
                     }
                 }
             }
             $pcommand = Yii::app()->db->createCommand()->select("id")->from($parenttbl)->where(array('and', "{$col}=:{$col}"), array(":{$col}" => $val))->queryAll();
             foreach ($pcommand as $key => $b) {
                 foreach ($b as $c => $d) {
                     $wheres[] = " AND {$parcol}={$d}";
                 }
             }
             if ($wheres != array()) {
                 $qtxt .= implode(' ', $wheres);
             }
         }
         $command = Yii::app()->db->createCommand($qtxt);
         $command->bindValue(":{$select1}", '' . $_GET['term'] . '%', PDO::PARAM_STR);
         $res = $command->queryAll();
         foreach ($res as $v) {
             if (isset($v['type'])) {
                 $v[$select1] = uc_first($v['type']) . "." . $v[$select1];
             }
             $v['value'] = $v[$select1];
             $return[] = $v;
         }
         echo CJSON::encode($return);
         Yii::app()->end();
     } else {
         return false;
     }
 }
 /**
  * Set options method to merge default options with controller options and view options.
  * Specific options are given within an array of type chart => [options]
  *
  * @param $opts array of options given
  * @param $data array of chart data
  * @return json object
  */
 protected function setOptions($opts, $data)
 {
     // priority for options set from view
     if (isset($data['Options'])) {
         $opts['Options'] = isset($opts['Options']) ? array_merge($opts['Options'], $data['Options']) : $data['Options'];
     }
     foreach ($this->config('Options') as $key => $value) {
         if (!in_array($key, ['Line', 'Bar', 'Radar', 'Polar', 'Pie', 'Doughnut'])) {
             $options[$key] = isset($opts['Options'][$key]) ? $opts['Options'][$key] : $this->config('Options.' . $key);
         }
     }
     // SPECIFIC OPTIONS
     $chartType = strtolower($data['Chart']['type'] === 'doughnut') ? 'Pie' : uc_first(strtolower($data['Chart']['type']));
     foreach ($this->config('Options.' . $chartType) as $label => $val) {
         // chart type given without capital letters
         $options[$label] = isset($opts['Options'][$chartType][$label]) ? $opts['Options'][$chartType][$label] : $this->config('Options.' . $chartType . '.' . $label);
         // chart type given with capital letter first
         $options[$label] = isset($opts['Options'][ucfirst($chartType)][$label]) ? $opts['Options'][$chartType][$label] : $this->config('Options.' . $chartType . '.' . $label);
     }
     // solution for javascript functions encoded as strings with json_encode
     // http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/
     $value_arr = array();
     $replace_keys = array();
     foreach ($options as $key => &$value) {
         if (!is_array($value)) {
             // Look for values starting with 'function('
             if (strpos($value, 'function(') === 0) {
                 // Store function string.
                 $value_arr[] = $value;
                 // Replace function string in $foo with a 'unique' special key.
                 $value = '%' . $key . '%';
                 // Later on, we'll look for the value, and replace it.
                 $replace_keys[] = '"' . $value . '"';
             }
         }
     }
     $options = json_encode($options);
     // Replace the special keys with the original string.
     $options = str_replace($replace_keys, $value_arr, $options);
     return $options;
 }