Example #1
0
 public function __call($event, $args)
 {
     if (substr($event, 0, 3) == 'get' && strlen($event) > 3) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return $this->get($key);
     } elseif (substr($event, 0, 3) == 'set' && strlen($event) > 3) {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return $this->set($key, $value);
     }
     if (true === $this->__has($event)) {
         array_push($args, $this);
         return $this->__fire($event, $args);
     } else {
         if (method_exists($this, $event)) {
             throw new Exception("The method {$event} is a native class' method. Please choose an other name.");
         }
         $value = Arrays::first($args);
         if ($value instanceof Closure) {
             $eventable = $this->__event($event, $value);
         } else {
             $set = function () use($value) {
                 return $value;
             };
             $eventable = $this->__event($event, $set);
         }
         return $this;
     }
 }
Example #2
0
File: Di.php Project: schpill/thin
 public function __call($event, $args)
 {
     if (substr($event, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return self::get($key);
     } elseif (substr($event, 0, 3) == 'set') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($event, 3)));
         $key = Inflector::lower($uncamelizeMethod);
         return self::set($key, $value);
     }
     if (true === self::__has($event)) {
         return self::__fire($event, $args);
     } else {
         $value = Arrays::first($args);
         if ($value instanceof Closure) {
             $eventable = self::__event($event, $value);
         } else {
             $set = function () use($value) {
                 return $value;
             };
             $eventable = self::__event($event, $set);
         }
     }
 }
Example #3
0
 /**
  * Construct new RangeQuery component
  *
  * @return \ElasticSearch\DSL\RangeQuery
  * @param array $options
  */
 public function __construct(array $options = [])
 {
     $this->fieldname = key($options);
     $values = Arrays::first($options);
     if (Arrays::is($values)) {
         foreach ($values as $key => $val) {
             $this->{$key} = $val;
         }
     }
 }
Example #4
0
 private function getOrm($seg)
 {
     if (!count($seg)) {
         throw new \Exception('Query is invalid.');
     }
     $seg = Arrays::first($seg);
     $table = isAke($seg, 'table', false);
     if (!$table) {
         throw new \Exception('Query is invalid.');
     }
     if (fnmatch('*.*', $table)) {
         list($database, $table) = explode('.', $table);
     } else {
         $database = SITE_NAME;
     }
     return jdb($database, $table);
 }
Example #5
0
 public static function __callStatic($method, $args)
 {
     $db = Inflector::uncamelize($method);
     if (fnmatch('*_*', $db)) {
         list($database, $table) = explode('_', $db, 2);
     } else {
         $database = SITE_NAME;
         $table = $db;
     }
     if (empty($args)) {
         return Db::instance($database, $table);
     } elseif (count($args) == 1) {
         $id = Arrays::first($args);
         if (is_numeric($id)) {
             return Db::instance($database, $table)->find($id);
         }
     }
 }
Example #6
0
 /**
  * Transform search parameters to string
  *
  * @return string
  * @param mixed $dslSort
  */
 protected function transformDSLSortToString($dslSort)
 {
     $string = "";
     if (Arrays::is($dslSort)) {
         foreach ($dslSort as $sort) {
             if (Arrays::is($sort)) {
                 $field = key($sort);
                 $info = Arrays::first($sort);
             } else {
                 $field = $sort;
             }
             $string .= "&sort=" . $field;
             if (isset($info)) {
                 if (is_string($info) && $info == "desc") {
                     $string .= ":reverse";
                 } elseif (Arrays::is($info) && Arrays::exists("reverse", $info) && $info['reverse']) {
                     $string .= ":reverse";
                 }
             }
         }
     }
     return $string;
 }
Example #7
0
 /**
  * First item
  *
  * @return Model
  */
 public function first($callback = null, $default = null)
 {
     if (is_null($callback)) {
         return count($this->_items) > 0 ? Arrays::first($this->_items) : $default;
     } else {
         foreach ($this->_items as $key => $value) {
             if (call_user_func($callback, $key, $value)) {
                 return $value;
             }
         }
         return value($default);
     }
 }
Example #8
0
File: Fly.php Project: schpill/thin
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) == 1 ? Arrays::first($args) : null;
         return isAke($this->_data, $field, $default);
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($args)) {
             $val = Arrays::first($args);
         } else {
             $val = null;
         }
         $this->_data[$field] = $val;
         return $this;
     } else {
         $cb = isake($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         }
         dd("{$func} is not a model function of this object.");
     }
 }
Example #9
0
 public function get($var)
 {
     if (isset($this->{$var})) {
         if (isset($this->thin_type)) {
             Data::getModel($this->thin_type);
             $type = $this->thin_type;
             $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
             if (Arrays::exists('relationships', $settings)) {
                 if (Arrays::exists($var, $settings['relationships'])) {
                     return Data::getById($var, $this->{$var});
                 }
             }
         }
         return $this->{$var};
     } else {
         if (isset($this->thin_type)) {
             Data::getModel($this->thin_type);
             $type = $this->thin_type;
             $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
             if (Arrays::exists($var, $settings['relationships']) && 's' == $var[strlen($var) - 1]) {
                 if (Arrays::exists($var, $settings['relationships'])) {
                     $res = Data::query(substr($var, 0, -1), "{$type} = " . $this->id);
                     $collection = [];
                     if (count($res)) {
                         foreach ($res as $row) {
                             $obj = Data::getObject($row);
                             $collection[] = $obj;
                         }
                     }
                     return 1 == count($collection) ? Arrays::first($collection) : $collection;
                 }
             }
         }
     }
     return null;
 }
Example #10
0
 public function listing($customFields = false)
 {
     $fields = $this->fields();
     $fieldInfos = isAke($this->config, 'fields');
     $before_list = isAke($this->config, 'before_list', false);
     if (false !== $before_list) {
         $before_list([]);
     }
     $fieldsSettings = Db::instance('core', 'datafieldssettings')->where("table = " . $this->model->table)->where("database = " . $this->model->db)->where('user_id = ' . auth()->user()->getId())->exec();
     $userSettings = [];
     if (count($fieldsSettings)) {
         foreach ($fieldsSettings as $fieldSettings) {
             foreach ($fieldSettings as $k => $v) {
                 if (strstr($k, 'is_')) {
                     $userSettings[$fieldSettings['field']][$k] = 1 == $v ? true : false;
                 }
             }
         }
     }
     $tableSettings = Db::instance('core', 'datatablesettings')->where("table = " . $this->model->table)->where('database = ' . $this->model->db)->where('user_id = ' . auth()->user()->getId())->first();
     if (request()->getKill() == 1) {
         session('dataTableCrudRedis::' . $this->model->table)->setPage(null)->setOrder(null)->setOrderDirection(null)->setWhere(null);
     }
     $limit = isAke($tableSettings, 'rows', isAke($this->config, 'items_by_page', Config::get('crud.items.number', 25)));
     $defaultOrder = isAke($tableSettings, 'sort', isAke($this->config, 'default_order', $this->model->pk()));
     $defaultDir = isAke($this->config, 'default_order_direction', 'ASC');
     $many = isAke($this->config, 'many');
     $plus = isAke($this->config, 'options_form', '');
     $readable = isAke($this->config, 'readable', true);
     $updatable = isAke($this->config, 'updatable', true);
     $duplicatable = isAke($this->config, 'duplicatable', true);
     $deletable = isAke($this->config, 'deletable', true);
     $optionsConfig = ['readable' => $readable, 'updatable' => $updatable, 'duplicatable' => $duplicatable, 'deletable' => $deletable];
     $where = isAke($_REQUEST, 'crud_where', null);
     $page = isAke($_REQUEST, 'crud_page', 1);
     $order = isAke($_REQUEST, 'crud_order', $defaultOrder);
     $orderDirection = isAke($_REQUEST, 'crud_order_direction', $defaultDir);
     $export = isAke($_REQUEST, 'crud_type_export', false);
     $updated_at = isAke($this->config, 'updated_at', false);
     $created_at = isAke($this->config, 'created_at', false);
     $export = !strlen($export) ? false : $export;
     $offset = $page * $limit - $limit;
     if (!count($_POST)) {
         $sessionWhere = session('dataTableCrudRedis::' . $this->model->table)->getWhere();
         $sessionPage = session('dataTableCrudRedis::' . $this->model->table)->getPage();
         $sessionOrder = session('dataTableCrudRedis::' . $this->model->table)->getOrder();
         $sessionOrderDirection = session('dataTableCrudRedis::' . $this->model->table)->getOrderDirection();
         $where = !strlen($sessionWhere) ? $where : $sessionWhere;
         $page = !strlen($sessionPage) ? $where : $sessionPage;
         $order = !strlen($sessionOrder) ? $order : $sessionOrder;
         $orderDirection = !strlen($sessionOrderDirection) ? $orderDirection : $sessionOrderDirection;
     }
     $page = !is_numeric($page) ? 1 : $page;
     session('dataTableCrudRedis::' . $this->model->table)->setPage($page)->setOrder($order)->setOrderDirection($orderDirection)->setWhere($where);
     $whereData = '';
     if (!empty($where)) {
         $whereData = $this->parseQuery($where);
     }
     $db = call_user_func_array(['\\Dbredis\\Db', 'instance'], [$this->model->db, $this->model->table]);
     if (strstr($whereData, ' && ') || strstr($whereData, ' || ')) {
         $db = $this->model->query($whereData);
     } else {
         if (strlen($whereData)) {
             $db = $this->model->where($whereData);
         } else {
             $db = $this->model->full();
         }
     }
     $results = $db->order($order, $orderDirection)->exec();
     if (count($results) < 1) {
         if (strlen($where)) {
             return '<div class="alert alert-danger col-md-4 col-md-pull-4 col-md-push-4">La requête ne remonte aucun résultat.</div>';
         } else {
             return '<div class="alert alert-info col-md-4 col-md-pull-4 col-md-push-4">Aucune donnée à afficher..</div>';
         }
     }
     if (false !== $export) {
         return $this->export($export, $results);
     }
     $total = count($results);
     $last = ceil($total / $limit);
     $paginator = new Paginator($results, $page, $total, $limit, $last);
     $data = $paginator->getItemsByPage();
     $pagination = $paginator->links();
     $start = $limit * $page - ($limit - 1);
     $end = $limit * $page;
     $end = $end > $total ? $total : $end;
     if (strlen($pagination)) {
         $pagination = '<div class="row">
             <div class="col-md-3">
             Enregistrements ' . $start . ' à ' . $end . ' sur ' . $total . '
             </div>
             <div class="col-md-9">
             ' . $pagination . '
             </div>
             </div>';
     } else {
         $pagination = '<div class="row"><div class="col-md-12">' . $total . ' enregistrements<br /><br /></div></div>';
     }
     $html = $pagination . '<div class="row"><div class="col-md-12"><form action="' . urlAction('list') . '/table/' . $this->model->table . '/database/' . $this->model->db . '" id="listForm" method="post">
         <input type="hidden" name="crud_page" id="crud_page" value="' . $page . '" /><input type="hidden" name="crud_order" id="crud_order" value="' . $order . '" /><input type="hidden" name="crud_order_direction" id="crud_order_direction"  value="' . $orderDirection . '" /><input type="hidden" id="crud_where" name="crud_where" value="' . \Thin\Crud::checkEmpty('crud_where') . '" /><input type="hidden" id="crud_type_export" name="crud_type_export" value="" />';
     if ($order != 'updated_at') {
         $html .= '<a rel="tooltip" title="Classer du plus récent au plus ancien" href="#" class="btn btn-default" onclick="recent(); return false;"><i class="fa fa-plus"></i> <i class="fa fa-clock-o"></i></a>&nbsp;&nbsp;<a rel="tooltip" title="Classer du plus ancien au plus récent" href="#" class="btn btn-default" onclick="old(); return false;"><i class="fa fa-minus"></i> <i class="fa fa-clock-o"></i></a><br><br>';
     } else {
         if ($orderDirection == 'ASC') {
             $html .= '<a rel="tooltip" title="Classer du plus récent au plus ancien" href="#" class="btn btn-default" onclick="recent(); return false;"><i class="fa fa-plus"></i> <i class="fa fa-clock-o"></i></a><br><br>';
         } else {
             $html .= '<a rel="tooltip" title="Classer du plus ancien au plus récent" href="#" class="btn btn-default" onclick="old(); return false;"><i class="fa fa-minus"></i> <i class="fa fa-clock-o"></i></a><br><br>';
         }
     }
     $html .= '<table style="clear: both;" class="table table-striped tablesorter table-bordered table-condensed table-hover">
                     <thead>
                     <tr>';
     if (Arrays::is($created_at)) {
         $label = isAke($created_at, 'label', 'Créé');
         if ('created_at' == $order) {
             $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC';
             $js = 'orderGoPage(\'created_at\', \'' . $directionJs . '\');';
             $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting ' . Inflector::lower($orderDirection) . '" rel="created_at">' . \Thin\Html\Helper::display($label) . '</div></th>';
         } else {
             $js = 'orderGoPage(\'created_at\', \'ASC\');';
             $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting" rel="created_at">' . \Thin\Html\Helper::display($label) . '</div></th>';
         }
     }
     if (Arrays::is($updated_at)) {
         $label = isAke($updated_at, 'label', 'MAJ');
         if ('updated_at' == $order) {
             $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC';
             $js = 'orderGoPage(\'updated_at\', \'' . $directionJs . '\');';
             $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting ' . Inflector::lower($orderDirection) . '" rel="updated_at">' . \Thin\Html\Helper::display($label) . '</div></th>';
         } else {
             $js = 'orderGoPage(\'updated_at\', \'ASC\');';
             $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting" rel="updated_at">' . \Thin\Html\Helper::display($label) . '</div></th>';
         }
     }
     foreach ($fields as $field) {
         $userInfos = isAke($userSettings, $field, []);
         $fieldSettings = isAke($fieldInfos, $field);
         $listable = isAke($userInfos, 'is_listable', isAke($fieldSettings, 'is_listable', false));
         $sortable = isAke($userInfos, 'is_sortable', isAke($fieldSettings, 'is_sortable', false));
         $fieldSettings = isAke($fieldInfos, $field);
         $label = isAke($fieldSettings, 'label', ucfirst($field));
         if (!$listable || $field == $this->model->pk()) {
             continue;
         }
         if (!$sortable) {
             $html .= '<th>' . \Thin\Html\Helper::display($label) . '</th>';
         } else {
             if ($field == $order) {
                 $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC';
                 $js = 'orderGoPage(\'' . $field . '\', \'' . $directionJs . '\');';
                 $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting ' . Inflector::lower($orderDirection) . '" rel="' . $field . '">' . \Thin\Html\Helper::display($label) . '</div></th>';
             } else {
                 $js = 'orderGoPage(\'' . $field . '\', \'ASC\');';
                 $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting" rel="' . $field . '">' . \Thin\Html\Helper::display($label) . '</div></th>';
             }
         }
     }
     if (true === $customFields) {
         $html .= '<th style="text-align: center;">Attr.</th>';
     }
     if (count($many)) {
         $html .= '<th style="text-align: center;">Rel.</th>';
     }
     $html .= '<th style="text-align: center;">Action</th></tr></thead><tbody>';
     foreach ($data as $item) {
         $id = isAke($item, $this->model->pk(), null);
         $html .= '<tr ondblclick="document.location.href = \'' . urlAction('update') . '/table/' . $this->model->table . '/database/' . $this->model->db . '/id/' . $id . '\';">';
         if (Arrays::is($created_at)) {
             $format = isAke($created_at, 'format', 'd/m/Y H:i:s');
             $value = date($format, isAke($item, 'created_at', time()));
             $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>';
         }
         if (Arrays::is($updated_at)) {
             $format = isAke($updated_at, 'format', 'd/m/Y H:i:s');
             $value = date($format, isAke($item, 'updated_at', time()));
             $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>';
         }
         foreach ($fields as $field) {
             $userInfos = isAke($userSettings, $field, []);
             $fieldSettings = isAke($fieldInfos, $field);
             $listable = isAke($userInfos, 'is_listable', isAke($fieldSettings, 'is_listable', false));
             $languages = isAke($fieldSettings, 'languages');
             if (!$listable || $field == $this->model->pk()) {
                 continue;
             }
             $value = !count($languages) ? isAke($item, $field, null) : isAke($item, $field . '_' . Arrays::first($languages), null);
             $closure = isAke($fieldSettings, 'content_view', false);
             if (false === $closure || !is_callable($closure)) {
                 $continue = true;
                 if (ake('form_type', $fieldSettings)) {
                     if ($fieldSettings['form_type'] == 'image' && strlen($value)) {
                         $html .= '<td><img src="' . $value . '" style="max-width: 200px;" /></td>';
                         $continue = false;
                     }
                     if ($fieldSettings['form_type'] == 'email' && strlen($value)) {
                         $html .= '<td><a href="mailto:' . $value . '">' . \Thin\Html\Helper::display($this->truncate($value)) . '</a></td>';
                         $continue = false;
                     }
                     if ($fieldSettings['form_type'] == 'file' && strlen($value)) {
                         $html .= '<td><a class="btn btn-small btn-success" href="' . $value . '"><i class="fa fa-download"></i></td>';
                         $continue = false;
                     }
                 }
                 if (true === $continue) {
                     if ('email' == $field) {
                         $html .= '<td><a href="mailto:' . $value . '">' . \Thin\Html\Helper::display($this->truncate($value)) . '</a></td>';
                     } else {
                         $html .= '<td>';
                         if (is_array($value)) {
                             $value = 'array';
                         }
                         if (strlen($value) >= 20) {
                             $html .= '<span rel="tooltip" title="' . \Thin\Html\Helper::display($value) . '">';
                         }
                         $html .= \Thin\Html\Helper::display($this->truncate($value));
                         if (strlen($value) >= 20) {
                             $html .= '</span>';
                         }
                         $html .= '</td>';
                     }
                 }
             } else {
                 $value = call_user_func_array($closure, array($item));
                 $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>';
             }
         }
         if (true === $customFields) {
             $html .= '<td style="text-align: center;"><a href="' . urlAction('customfields') . '/type/' . $this->model->table . '/row_id/' . $id . '" target="_blank" rel="tooltip" title="Gestion des attributs supplémentaires" class="btn btn-success"><i class="fa fa-tags"></i></a></td>';
         }
         if (count($many)) {
             $html .= '<td style="text-align: center;"><ul class="list-inline">';
             foreach ($many as $rel) {
                 $foreignCrud = new self(Db::instance($this->model->db, $rel));
                 $nameRel = isAke($foreignCrud->config(), 'plural', $rel . 's');
                 $html .= '<li style="margin-right: 5px;"><a rel="tooltip" title="Afficher les ' . strtolower($nameRel) . ' en relation" class="btn btn-primary" target="_blank" href="' . urlAction('many') . '/table/' . $rel . '/foreign/' . $this->model->table . '_id/id/' . $id . '/database/' . $this->model->db . '"><i class="fa fa-chain"></i></a></li>';
             }
             $html .= '</ul></td>';
         }
         $html .= $this->options($id, $optionsConfig, $plus);
         $html .= '</tr>';
     }
     $html .= '</tbody></table></form>' . $pagination . '</div></div>';
     return $html;
 }
Example #11
0
 private function getTime()
 {
     $time = microtime();
     $time = explode(' ', $time, 2);
     return Arrays::last($time) + Arrays::first($time);
 }
Example #12
0
 private function siren2($siren)
 {
     $infos = [];
     if (is_numeric($siren) && strlen($siren)) {
         $cache = redis()->get('siren::' . $siren);
         if (!strlen($cache)) {
             $data = dwn("http://www.verif.com/imprimer/{$siren}/1/1/");
             redis()->set('siren::' . $siren, $data);
         } else {
             $data = $cache;
         }
         $formeJuridique = $registre = $capital = $dirigeant = $immatriculation = $departement = $codePostal = $ville = $adresse = $ape = $creation = $activite = $tel = $fax = $effectif = $siret = null;
         $cmdTel = "curl 'http://www.pagespro.com/recherche.php' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' -H 'Connection: keep-alive' -H 'Cookie: EIRAM=1; xtvrn=\$486926\$; xtan=-; xtant=1' -H 'Host: www.pagespro.com' -H 'Referer: http://www.pagespro.com/recherche.php' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0' -H 'Content-Type: application/x-www-form-urlencoded' --data 'p_ACTION=&p_ORDRE=AfficheRes&a_PAGE=1&a_TAG=&a_OccRecherche=&typeRecherche=express&satellite=siret&a_activ=&a_rai_soc=&a_naf=&a_siren={$siren}&a_tel=&a_geo=&typeTri=&ordreTri=&a_session='";
         $cacheTel = redis()->get('siren::tel::' . $siren);
         if (!strlen($cacheTel)) {
             exec($cmdTel, $dataTel);
             $dataTel = implode("", $dataTel);
             redis()->set('siren::tel::' . $siren, $dataTel);
         } else {
             $dataTel = $cacheTel;
         }
         if (strstr($dataTel, '<span itemprop="tel">')) {
             $tel = strReplaceFirst('0', '+33', str_replace(["\t", '&nbsp;', ' '], '', Utils::cut('<span itemprop="tel">', '</span>', $dataTel)));
         }
         $dataFax = str_replace(["\t"], '', $dataTel);
         if (strstr($dataFax, "<span>fax")) {
             $segFax = Utils::cut('<span>fax', '</d', $dataFax);
             $fax = strReplaceFirst('0', '+33', str_replace(["\t", '&nbsp;', ' '], '', Utils::cut('<span itemprop="tel">', '</span>', $segFax)));
         }
         if (strstr($dataTel, '<b>Effectif')) {
             $segEffectif = Utils::cut('<b>Effectif', '/div>', $dataTel);
             $effectif = str_replace(["\t", '&nbsp;'], '', Utils::cut('</b>', '<', $segEffectif));
         }
         if (strstr($dataTel, '<b>Siret')) {
             $segSiret = Utils::cut('<b>Siret', '/div>', $dataTel);
             $siret = str_replace(["\t", '&nbsp;'], '', Utils::cut('</b>', '<', $segSiret));
         }
         $seg = Utils::cut('<h4>Informations g&eacute;n&eacute;rales</h4>', '</table>', $data);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Raison sociale</td>', '</tr>', $seg);
         $raisonSociale = Utils::cut('<td>', '</td>', $sousSeg);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">APE</td>', '</tr>', $seg);
         $ape = Utils::cut('<td>', '</td>', $sousSeg);
         list($ape, $activite) = explode(' / ', Utils::cut('>', '</', $ape), 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Forme juridique', '</tr>', $seg);
         list($formeJuridique, $creation) = explode(', cr&eacute;&eacute;e le ', Utils::cut('<td>', '</td>', $sousSeg), 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Adresse', '</tr>', $seg);
         $adr = Utils::cut('<td>', '</td>', $sousSeg);
         $adr = explode("\n", $adr);
         array_pop($adr);
         $adresse = Arrays::first($adr);
         $cpville = Arrays::last($adr);
         $adresse = $this->clean(str_replace(["\t", "<br />"], '', $adresse));
         list($codePostal, $ville) = explode('&nbsp;', $cpville, 2);
         $codePostal = $this->clean(str_replace(["\t", " "], '', $codePostal));
         $departement = substr($codePostal, 0, 2);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Capital Social', '</tr>', $seg);
         $capital = Utils::cut('<td>', '&', $sousSeg);
         $sousSeg = Utils::cut('<td class="fiche_tdhead">Registre du commerce', '</tr>', $seg);
         $registre = Utils::cut('<td>', '<', $sousSeg);
         $registre = str_replace(' ', '', $registre);
         if (null === $siret && strstr($seg, 'tdhead">SIRET</td>')) {
             $sousSeg = Utils::cut('<td class="fiche_tdhead">SIRET</td>', '</tr>', $seg);
             $siret = str_replace(' ', '', Utils::cut('<td>', '</td>', $sousSeg));
         }
         if (strstr($data, '<H4>Dirigeants</H4>')) {
             $segDirigeant = Utils::cut('<H4>Dirigeants</H4>', '</div>', $data);
             $d = Utils::cut('<table', '</table>', $segDirigeant);
             $d = Utils::cut('<tr', '</tr>', $d);
             $rows = explode("\n", $d);
             array_shift($rows);
             array_pop($rows);
             $fonction = Utils::cut('>', '<', Arrays::first($rows));
             $segPersonne = Arrays::last($rows);
             if (fnmatch('*(*', $segPersonne)) {
                 $personne = Utils::cut('>', '(', $segPersonne);
                 $personne = str_replace(['&nbsp;'], '', $personne);
             } else {
                 $personne = Utils::cut('>', '</', $segPersonne);
             }
             $dirigeant = "{$personne} - {$fonction}";
         }
         $infos = ['siren' => $this->clean($siren), 'siret' => $this->clean($siret), 'raison_sociale' => $this->clean($raisonSociale), 'ape' => $this->clean($ape), 'activite' => $this->clean($activite), 'forme_juridique' => $this->clean($formeJuridique), 'telephone' => $this->clean($tel), 'fax' => $this->clean($fax), 'adresse' => $this->clean($adresse), 'code_postal' => $this->clean($codePostal), 'ville' => $this->clean($ville), 'departement' => $this->clean($departement), 'effectif' => $this->clean($effectif), 'date_immatriculation' => $this->clean($creation), 'registre' => $this->clean($registre), 'capital' => $this->clean($capital), 'dirigeant' => $this->clean($dirigeant)];
     }
     return $infos;
 }
Example #13
0
 public function detach($model)
 {
     if (!isset($this->_data['id'])) {
         throw new Exception("detach method requires a valid model.");
     }
     $m = !is_array($model) ? $model : Arrays::first($model);
     if ($m instanceof Db) {
         $m = $m->model();
     }
     $all = false;
     if (empty($m->id)) {
         $all = true;
     }
     $mTable = $m->db()->table;
     $names = [$this->_db->table, $mTable];
     asort($names);
     $pivot = Inflector::lower('pivot' . implode('', $names));
     $db = Db::instance($this->_db->db, $pivot);
     if (is_array($model)) {
         foreach ($model as $mod) {
             $id = (int) $mod->id;
             $row = $mod->db()->find($id);
             if ($row) {
                 $fieldAttach = $mTable . '_id';
                 $fieldModel = $this->_db->table . '_id';
                 $attach = $db->where([$fieldAttach, '=', (int) $id])->where([$fieldModel, '=', (int) $this->_data['id']])->first(true);
                 if ($attach) {
                     $attach->delete();
                 }
             }
         }
     } else {
         if (false === $all) {
             $id = (int) $model->id;
             $row = $model->db()->find($id);
             if ($row) {
                 $fieldAttach = $mTable . '_id';
                 $fieldModel = $this->_db->table . '_id';
                 $attach = $db->where([$fieldAttach, '=', (int) $id])->where([$fieldModel, '=', (int) $this->_data['id']])->first(true);
                 if ($attach) {
                     $attach->delete();
                 }
             }
         } else {
             $fieldModel = $this->_db->table . '_id';
             $attachs = $db->where([$fieldModel, '=', (int) $this->_data['id']])->exec(true);
             if (!empty($attachs)) {
                 foreach ($attachs as $attach) {
                     $attach->delete();
                 }
             }
         }
     }
     return $this;
 }
Example #14
0
 private function checkTable($table)
 {
     $res = $this->db->prepare("SHOW TABLES");
     $res->execute();
     if (Arrays::is($res)) {
         $count = count($res);
     } else {
         $count = $res->rowCount();
     }
     if ($count < 1) {
         return false;
     }
     foreach ($res as $row) {
         $tabletmp = Arrays::first($row);
         if ($table == $tabletmp) {
             return true;
         }
     }
     return false;
 }
Example #15
0
File: Eav.php Project: noikiy/inovi
 public function first($object = false)
 {
     $res = $this->results;
     $this->reset();
     if (true === $object) {
         return count($res) ? $this->makeObject(Arrays::first($res)) : null;
     } else {
         return count($res) ? Arrays::first($res) : array();
     }
 }
Example #16
0
 public function __call($method, $args)
 {
     if (substr($method, 0, strlen('findBy')) == 'findBy') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $var = Inflector::lower($uncamelizeMethod);
         return $this->findBy($var, $value);
     } elseif (substr($method, 0, strlen('findOneBy')) == 'findOneBy') {
         $value = Arrays::first($args);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $var = Inflector::lower($uncamelizeMethod);
         return $this->findBy($var, $value, true);
     }
 }
Example #17
0
 /**
  * [fromSql description]
  *
  * @method fromSql
  *
  * @param  [type]  $sql [description]
  *
  * @return [type]       [description]
  */
 public static function fromSql($sql)
 {
     $select = Utils::cut('SELECT ', ' FROM', $sql);
     $from = Utils::cut(' FROM ', ' ', $sql);
     $wheres = '';
     if (fnmatch('* WHERE *', $sql)) {
         $wheres = Arrays::last(explode(' WHERE ', $sql));
     }
     $joins = [];
     if (fnmatch('* JOIN *', $sql)) {
         $segs = explode(' JOIN ', $sql);
         array_shift($segs);
         foreach ($segs as $seg) {
             $fk = Arrays::first(explode(' ', $seg));
             if (!in_array($joins, $joins)) {
                 $joins[] = $fk;
             }
         }
     }
     if (fnmatch('*.*', $from)) {
         list($db, $table) = explode('.', Inflector::lower($from), 2);
     } else {
         $db = SITE_NAME;
         $table = Inflector::lower($from);
     }
     $instance = self::instance($db, $table);
     if (!empty($select) && $select != '*') {
         $selects = explode(',', str_replace(' ', '', Inflector::lower($select)));
         foreach ($selects as $field) {
             $instance->select($field);
         }
     }
     if (fnmatch('* ORDER BY *', $wheres)) {
         list($wheres, $orders) = explode(' ORDER BY ', $wheres, 2);
     }
     $whs = [$wheres];
     $or = false;
     if (fnmatch('* && *', $wheres)) {
         $whs = explode(' && ', $wheres);
     }
     if (fnmatch('* || *', $wheres)) {
         $whs = explode(' || ', $wheres);
         $or = true;
     }
     foreach ($whs as $wh) {
         list($f, $o, $v) = explode(' ', $wh, 3);
         if ($v[0] == "'") {
             $v = substr($v, 1);
         }
         if ($v[strlen($v) - 1] == "'") {
             $v = substr($v, 0, -1);
         }
         if (is_numeric($v)) {
             if ($v == intval($v)) {
                 $v = (int) $v;
             }
         }
         if (!$or) {
             $instance = $instance->where([$f, $o, $v]);
         } else {
             $instance = $instance->where([$f, $o, $v], 'OR');
         }
     }
     if (isset($orders)) {
         if (fnmatch('*,*', $orders)) {
             $orders = explode(',', str_replace(', ', ',', $orders));
         } else {
             $orders = [$orders];
         }
         foreach ($orders as $order) {
             if (fnmatch('* *', $order)) {
                 list($f, $d) = explode(' ', $order, 2);
             } else {
                 $f = $order;
                 $d = 'ASC';
             }
             $instance = $instance->order($f, $d);
         }
     }
     return $instance;
 }
Example #18
0
File: Ini.php Project: schpill/thin
 /**
  * Assign the key's value to the property list. Handles the
  * nest separator for sub-properties.
  *
  * @param  array  $config
  * @param  string $key
  * @param  string $value
  * @throws Exception
  * @return array
  */
 protected function _processKey($config, $key, $value)
 {
     if (strpos($key, $this->_nestSeparator) !== false) {
         $parts = explode($this->_nestSeparator, $key, 2);
         if (strlen(Arrays::first($parts)) && strlen(Arrays::last($parts))) {
             if (!isset($config[Arrays::first($parts)])) {
                 if (Arrays::first($parts) === '0' && !empty($config)) {
                     // convert the current values in $config into an array
                     $config = array(Arrays::first($parts) => $config);
                 } else {
                     $config[Arrays::first($parts)] = array();
                 }
             } elseif (!Arrays::is($config[Arrays::first($parts)])) {
                 throw new Exception("Cannot create sub-key for '{Arrays::first({$parts})}' as key already exists");
             }
             $config[Arrays::first($parts)] = $this->_processKey($config[Arrays::first($parts)], Arrays::last($parts), $value);
         } else {
             throw new Exception("Invalid key '{$key}'");
         }
     } else {
         $config[$key] = $value;
     }
     return $config;
 }
Example #19
0
 public function __call($method, $parameters)
 {
     if (substr($method, 0, 6) == 'findBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value);
     } elseif (substr($method, 0, 9) == 'findOneBy') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $field = Inflector::lower($uncamelizeMethod);
         $value = Arrays::first($parameters);
         return $this->findBy($field, $value, true);
     }
 }
Example #20
0
 /**
  * Main call
  *
  * @param string $filename  Name of file to write sql dump to
  * @return null
  */
 public function start($filename = '')
 {
     // Output file can be redefined here
     if (!empty($filename)) {
         $this->fileName = $filename;
     }
     // We must set a name to continue
     if (empty($this->fileName)) {
         throw new Exception("Output file name is not set");
     }
     // Connect to database
     $this->connect();
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
     $this->compressManager->open($this->fileName);
     // Formating dump file
     $this->compressManager->write($this->getHeader());
     if ($this->dumpSettings['add-drop-database']) {
         $this->compressManager->write($this->typeAdapter->add_drop_database($this->db));
     }
     // Listing all tables from database
     $this->tables = array();
     if (empty($this->dumpSettings['include-tables'])) {
         // include all tables for now, blacklisting happens later
         foreach ($this->dbHandler->query($this->typeAdapter->showTables($this->db)) as $row) {
             array_push($this->tables, current($row));
         }
     } else {
         // include only the tables mentioned in include-tables
         foreach ($this->dbHandler->query($this->typeAdapter->showTables($this->db)) as $row) {
             if (in_array(Arrays::first($row), $this->dumpSettings['include-tables'], true)) {
                 array_push($this->tables, Arrays::first($row));
                 $elem = array_search(Arrays::first($row), $this->dumpSettings['include-tables']);
                 unset($this->dumpSettings['include-tables'][$elem]);
             }
         }
     }
     // If there still are some tables in include-tables array, that means
     // that some tables weren't found. Give proper error and exit.
     if (0 < count($this->dumpSettings['include-tables'])) {
         $table = implode(",", $this->dumpSettings['include-tables']);
         throw new Exception("Table (" . $table . ") not found in database");
     }
     // Disable checking foreign keys
     if ($this->dumpSettings['disable-foreign-keys-check']) {
         $this->compressManager->write($this->typeAdapter->startDisableForeignKeysCheck());
     }
     // Exporting tables one by one
     foreach ($this->tables as $table) {
         if (in_array($table, $this->dumpSettings['exclude-tables'], true)) {
             continue;
         }
         $isTable = $this->getTableStructure($table);
         if (true === $isTable && false === $this->dumpSettings['no-data']) {
             $this->listValues($table);
         }
     }
     // Exporting views one by one
     foreach ($this->views as $view) {
         $this->compressManager->write($view);
     }
     // Enable checking foreign keys if needed
     if ($this->dumpSettings['disable-foreign-keys-check']) {
         $this->compressManager->write($this->typeAdapter->endDisableForeignKeysCheck());
     }
     $this->compressManager->close();
 }
Example #21
0
 public function __call($fn, $args)
 {
     $fields = $this->fields();
     $method = substr($fn, 0, 2);
     $object = lcfirst(substr($fn, 2));
     if ('is' === $method && strlen($fn) > 2) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}");
         } else {
             return $this->where($field . ' = ' . Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 4);
     $object = lcfirst(substr($fn, 4));
     if ('orIs' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         if (!Arrays::in($field, $fields)) {
             $field = $field . '_id';
             $model = Arrays::first($args);
             if ($model instanceof Container) {
                 $idFk = $model->id();
             } else {
                 $idFk = $model;
             }
             return $this->where("{$field} = {$idFk}", 'OR');
         } else {
             return $this->where($field . ' = ' . Arrays::first($args), 'OR');
         }
     } elseif ('like' === $method && strlen($fn) > 4) {
         $field = Inflector::uncamelize($object);
         $op = count($args) == 2 ? Arrays::last($args) : 'AND';
         return $this->like($field, Arrays::first($args), $op);
     }
     $method = substr($fn, 0, 5);
     $object = lcfirst(substr($fn, 5));
     if (strlen($fn) > 5) {
         if ('where' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         } elseif ('xorIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andIs' === $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' = ' . Arrays::first($args));
             }
         }
     }
     $method = substr($fn, 0, 6);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 6)));
     if (strlen($fn) > 6) {
         if ('findBy' == $method) {
             return $this->findBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 7);
     $object = lcfirst(substr($fn, 7));
     if (strlen($fn) > 7) {
         if ('orWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'OR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'OR');
             }
         } elseif ('orderBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = count($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $object = Inflector::uncamelize(lcfirst(substr($fn, 7)));
             if ($object == 'id') {
                 $object = $this->pk();
             }
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, 9);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 9)));
     if (strlen($fn) > 9) {
         if ('findOneBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args));
         }
     }
     $method = substr($fn, 0, 13);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 13)));
     if (strlen($fn) > 13) {
         if ('findObjectsBy' == $method) {
             return $this->findBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 15);
     $object = Inflector::uncamelize(lcfirst(substr($fn, 15)));
     if (strlen($fn) > 15) {
         if ('findOneObjectBy' == $method) {
             return $this->findOneBy($object, Arrays::first($args), true);
         }
     }
     $method = substr($fn, 0, 8);
     $object = lcfirst(substr($fn, 8));
     if (strlen($fn) > 8) {
         if ('xorWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}", 'XOR');
             } else {
                 return $this->where($field . ' ' . Arrays::first($args), 'XOR');
             }
         } elseif ('andWhere' == $method) {
             $field = Inflector::uncamelize($object);
             if (!Arrays::in($field, $fields)) {
                 $field = $field . '_id';
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$field} = {$idFk}");
             } else {
                 return $this->where($field . ' ' . Arrays::first($args));
             }
         }
     } else {
         $field = $fn;
         $fieldFk = $fn . '_id';
         $op = count($args) == 2 ? Inflector::upper(Arrays::last($args)) : 'AND';
         if (Arrays::in($field, $fields)) {
             return $this->where($field . ' = ' . Arrays::first($args), $op);
         } else {
             if (Arrays::in($fieldFk, $fields)) {
                 $model = Arrays::first($args);
                 if ($model instanceof Container) {
                     $idFk = $model->id();
                 } else {
                     $idFk = $model;
                 }
                 return $this->where("{$fieldFk} = {$idFk}", $op);
             }
         }
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Example #22
0
 private function queryParamsCallback($matches)
 {
     return preg_replace('/./Uui', '*', Arrays::first($matches));
 }
Example #23
0
 /**
  * Extract the type and alias from a given definition.
  *
  * @param  array  $definition
  * @return array
  */
 protected function extractAlias(array $definition)
 {
     return array(key($definition), Arrays::first($definition));
 }
Example #24
0
 public function listing($customFields = false)
 {
     $fields = $this->fields();
     $fieldInfos = isAke($this->config, 'fields');
     $defaultOrder = isAke($this->config, 'default_order', $this->model->pk());
     $defaultDir = isAke($this->config, 'default_order_direction', 'ASC');
     $limit = isAke($this->config, 'items_by_page', Config::get('crud.items.number', 25));
     $many = isAke($this->config, 'many');
     $where = isAke($_REQUEST, 'crud_where', null);
     $page = isAke($_REQUEST, 'crud_page', 1);
     $order = isAke($_REQUEST, 'crud_order', $defaultOrder);
     $orderDirection = isAke($_REQUEST, 'crud_order_direction', $defaultDir);
     $export = isAke($_REQUEST, 'crud_type_export', false);
     $export = !strlen($export) ? false : $export;
     $offset = $page * $limit - $limit;
     $whereData = '';
     if (!empty($where)) {
         $whereData = $this->parseQuery($where);
     }
     $db = call_user_func_array(['\\Dbarray\\Dbarray', 'instance'], [$this->model->db, $this->model->table]);
     if (strstr($whereData, ' && ') || strstr($whereData, ' || ')) {
         $wheres = explode(' && ', $whereData);
         foreach ($wheres as $tmpWhere) {
             $db = $this->model->where($tmpWhere);
         }
         $wheres = explode(' || ', $whereData);
         foreach ($wheres as $tmpWhere) {
             $db = $this->model->where($tmpWhere, 'OR');
         }
     } else {
         if (strlen($whereData)) {
             $db = $this->model->where($whereData);
         } else {
             $db = $this->model->fetch();
         }
     }
     $results = $db->order($order, $orderDirection)->exec();
     if (count($results) < 1) {
         if (strlen($where)) {
             return '<div class="alert alert-danger col-md-4 col-md-pull-4 col-md-push-4">La requête ne remonte aucun résultat.</div>';
         } else {
             return '<div class="alert alert-info col-md-4 col-md-pull-4 col-md-push-4">Aucune donnée à afficher..</div>';
         }
     }
     if (false !== $export) {
         return $this->export($export, $results);
     }
     $total = count($results);
     $last = ceil($total / $limit);
     $paginator = new Paginator($results, $page, $total, $limit, $last);
     $data = $paginator->getItemsByPage();
     $pagination = $paginator->links();
     $pagination = '<div class="row">
         <div class="col-md-12">
         ' . $pagination . '
         </div>
         </div>';
     $html = $pagination . '<div class="row"><div class="col-md-12"><form action="' . urlAction('list') . '/table/' . $this->model->table . '" id="listForm" method="post">
         <input type="hidden" name="crud_page" id="crud_page" value="' . $page . '" /><input type="hidden" name="crud_order" id="crud_order" value="' . $order . '" /><input type="hidden" name="crud_order_direction" id="crud_order_direction"  value="' . $orderDirection . '" /><input type="hidden" id="crud_where" name="crud_where" value="' . \Thin\Crud::checkEmpty('crud_where') . '" /><input type="hidden" id="crud_type_export" name="crud_type_export" value="" />
         <table style="clear: both;" class="table table-striped tablesorter table-bordered table-condensed table-hover">
                     <thead>
                     <tr>';
     foreach ($fields as $field) {
         $fieldSettings = isAke($fieldInfos, $field);
         $label = isAke($fieldSettings, 'label', ucfirst($field));
         $listable = isAke($fieldSettings, 'is_listable', true);
         $sortable = isAke($fieldSettings, 'is_sortable', true);
         if (!$listable || $field == $this->model->pk()) {
             continue;
         }
         if (!$sortable) {
             $html .= '<th>' . \Thin\Html\Helper::display($label) . '</th>';
         } else {
             if ($field == $order) {
                 $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC';
                 $js = 'orderGoPage(\'' . $field . '\', \'' . $directionJs . '\');';
                 $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting ' . Inflector::lower($orderDirection) . '" rel="' . $field . '">' . \Thin\Html\Helper::display($label) . '</div></th>';
             } else {
                 $js = 'orderGoPage(\'' . $field . '\', \'ASC\');';
                 $html .= '<th><div onclick="' . $js . '" class="text-left field-sorting" rel="' . $field . '">' . \Thin\Html\Helper::display($label) . '</div></th>';
             }
         }
     }
     if (true === $customFields) {
         $html .= '<th style="text-align: center;">Attr.</th>';
     }
     if (count($many)) {
         $html .= '<th style="text-align: center;">Rel.</th>';
     }
     $html .= '<th style="text-align: center;">Action</th></tr></thead><tbody>';
     foreach ($data as $item) {
         $id = isAke($item, $this->model->pk(), null);
         $html .= '<tr ondblclick="document.location.href = \'' . urlAction('update') . '/table/' . $this->model->table . '/id/' . $id . '\';">';
         foreach ($fields as $field) {
             $fieldSettings = isAke($fieldInfos, $field);
             $listable = isAke($fieldSettings, 'is_listable', true);
             $languages = isAke($fieldSettings, 'languages');
             if (!$listable || $field == $this->model->pk()) {
                 continue;
             }
             $value = !count($languages) ? isAke($item, $field, null) : isAke($item, $field . '_' . Arrays::first($languages), null);
             $closure = isAke($fieldSettings, 'content_view', false);
             if (false === $closure || !is_callable($closure)) {
                 $continue = true;
                 if (ake('form_type', $fieldSettings)) {
                     if ($fieldSettings['form_type'] == 'image' && strlen($value)) {
                         $html .= '<td><img src="' . $value . '" style="max-width: 200px;" /></td>';
                         $continue = false;
                     }
                     if ($fieldSettings['form_type'] == 'email' && strlen($value)) {
                         $html .= '<td><a href="mailto:' . $value . '">' . \Thin\Html\Helper::display($this->truncate($value)) . '</a></td>';
                         $continue = false;
                     }
                     if ($fieldSettings['form_type'] == 'file' && strlen($value)) {
                         $html .= '<td><a class="btn btn-small btn-success" href="' . $value . '"><i class="fa fa-download"></i></td>';
                         $continue = false;
                     }
                 }
                 if (true === $continue) {
                     if ('email' == $field) {
                         $html .= '<td><a href="mailto:' . $value . '">' . \Thin\Html\Helper::display($this->truncate($value)) . '</a></td>';
                     } else {
                         $html .= '<td>' . \Thin\Html\Helper::display($this->truncate($value)) . '</td>';
                     }
                 }
             } else {
                 $value = call_user_func_array($closure, array($item));
                 $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>';
             }
         }
         if (true === $customFields) {
             $html .= '<td style="text-align: center;"><a href="' . urlAction('customfields') . '/type/' . $this->model->table . '/row_id/' . $id . '" target="_blank" rel="tooltip" title="Gestion des attributs supplémentaires" class="btn btn-success"><i class="fa fa-tags"></i></a></td>';
         }
         if (count($many)) {
             $html .= '<td style="text-align: center;"><ul class="list-inline">';
             foreach ($many as $rel) {
                 $foreignCrud = new self(adb($this->model->db, $rel));
                 $nameRel = isAke($foreignCrud->config(), 'plural', $rel . 's');
                 $html .= '<li><a rel="tooltip" title="Afficher les ' . strtolower($nameRel) . ' en relation" class="btn btn-primary" target="_blank" href="' . urlAction('many') . '/table/' . $rel . '/foreign/' . $this->model->table . '_id/id/' . $id . '"><i class="fa fa-chain"></i></a></li>';
             }
             $html .= '</ul></td>';
         }
         $html .= $this->options($id);
         $html .= '</tr>';
     }
     $html .= '</tbody></table></form>' . $pagination . '</div></div>';
     return $html;
 }
Example #25
0
 public function findBy($field, $value, $one = false, $object = false)
 {
     if ($field == 'id') {
         $field = $this->pk();
     }
     $q = "SELECT " . $this->db . '.' . $this->table . '.' . implode(', ' . $this->db . '.' . $this->table . '.', $this->fields()) . "\n                FROM {$this->db}.{$this->table}\n                WHERE {$this->db}.{$this->table}.{$field} = " . $this->quote($value);
     $res = $this->query($q, $object);
     if (true === $one && !empty($res)) {
         return Arrays::first($res);
     }
     return true === $one ? null : $res;
 }
Example #26
0
 private static function defaultRoute()
 {
     $tab = explode('/', substr(static::$_uri, 1));
     if (count($tab) > 1) {
         if (3 != count($tab)) {
             $module = container()->getConfig()->getDefaultModule();
             $module = Inflector::lower($module);
             $controller = Inflector::lower(Arrays::first($tab));
             $action = $tab[1];
         } else {
             list($module, $controller, $action) = $tab;
             $module = Inflector::lower($module);
             $controller = Inflector::lower($controller);
             $action = Inflector::lower($action);
         }
         $action = repl(array('.html', '.php', '.asp', '.jsp', '.cfm', '.py', '.pl'), array('', '', '', '', '', '', ''), $action);
         if (true === container()->getMultiSite()) {
             $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module;
         } else {
             $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module;
         }
         $controllerDir = $moduleDir . DS . 'controllers';
         $controllerFile = $controllerDir . DS . $controller . 'Controller.php';
         if (true === File::exists($controllerFile)) {
             require_once $controllerFile;
             $controllerClass = 'Thin\\' . $controller . 'Controller';
             $controllerInstance = new $controllerClass();
             $actions = get_class_methods($controllerInstance);
             $actionName = $action . 'Action';
             if (Arrays::inArray($actionName, $actions)) {
                 $dispatch = new Container();
                 $dispatch->setModule($module);
                 $dispatch->setController($controller);
                 $dispatch->setAction(Inflector::uncamelize($action, '-'));
                 Utils::set('appDispatch', $dispatch);
                 return true;
             }
         }
     }
     return null;
 }
Example #27
0
 public function order($fieldOrder, $orderDirection = 'ASC', $results = array())
 {
     $res = count($results) ? $results : $this->results;
     if (empty($res)) {
         return $this;
     }
     $fields = array_keys(Arrays::first($res));
     $sort = array();
     foreach ($res as $i => $tab) {
         foreach ($fields as $k) {
             $value = isAke($tab, $k, null);
             $sort[$k][] = $value;
         }
     }
     $asort = array();
     foreach ($sort as $key => $rows) {
         for ($i = 0; $i < count($rows); $i++) {
             if (empty(${$key}) || is_string(${$key})) {
                 ${$key} = array();
             }
             $asort[$i][$key] = $rows[$i];
             array_push(${$key}, $rows[$i]);
         }
     }
     if (Arrays::is($fieldOrder) && !Arrays::is($orderDirection)) {
         $t = array();
         foreach ($fieldOrder as $tmpField) {
             array_push($t, $orderDirection);
         }
         $orderDirection = $t;
     }
     if (Arrays::is($fieldOrder) && Arrays::is($orderDirection)) {
         if (count($orderDirection) < count($fieldOrder)) {
             throw new Exception('You must provide the same arguments number of fields sorting and directions sorting.');
         }
         if (count($fieldOrder) == 1) {
             $fieldOrder = Arrays::first($fieldOrder);
             if ('ASC' == Inflector::upper(Arrays::first($orderDirection))) {
                 array_multisort(${$fieldOrder}, SORT_ASC, $asort);
             } else {
                 array_multisort(${$fieldOrder}, SORT_DESC, $asort);
             }
         } elseif (count($fieldOrder) > 1) {
             $params = array();
             foreach ($fieldOrder as $k => $tmpField) {
                 $tmpSort = isset($orderDirection[$k]) ? $orderDirection[$k] : 'ASC';
                 $params[] = ${$tmpField};
                 $params[] = 'ASC' == $tmpSort ? SORT_ASC : SORT_DESC;
             }
             $params[] = $asort;
             call_user_func_array('array_multisort', $params);
         }
     } else {
         if ('ASC' == Inflector::upper($orderDirection)) {
             array_multisort(${$fieldOrder}, SORT_ASC, $asort);
         } else {
             array_multisort(${$fieldOrder}, SORT_DESC, $asort);
         }
     }
     $collection = array();
     foreach ($asort as $key => $row) {
         array_push($collection, $row);
     }
     $this->results = $collection;
     return $this;
 }
Example #28
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $default = count($args) == 1 ? Arrays::first($args) : null;
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return $res;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 $db = Db::instance($this->_db->db, $field);
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 return $db->find($resFk, $object);
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $object = count($args) == 1 ? $args[0] : false;
                     if (!is_bool($object)) {
                         $object = false;
                     }
                     $idField = $this->_db->table . '_id';
                     return $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->exec($object);
                 } else {
                     return $default;
                 }
             }
         }
     } elseif (substr($func, 0, strlen('has')) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('has'))));
         $field = Inflector::lower($uncamelizeMethod);
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return true;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 return true;
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $idField = $this->_db->table . '_id';
                     $count = $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->count();
                     return $count > 0 ? true : false;
                 }
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $fields = $this->_db->fieldsSave();
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!in_array($field, $fields) && $field != $this->_db->pk()) {
             throw new Exception("The field {$field} does not exist in the model.");
         }
         if (!empty($args)) {
             $val = Arrays::first($args);
         } else {
             $val = null;
         }
         if (is_object($val)) {
             $val = $val->id;
         }
         if (fnmatch('*_id', $field)) {
             if (is_numeric($val)) {
                 $val = (int) $val;
             }
         }
         $this->_data[$field] = $val;
         $autosave = isAke($this->_data, 'autosave', false);
         return !$autosave ? $this : $this->save();
     } else {
         $cb = isake($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         } else {
             if ($func[strlen($func) - 1] == 's' && isset($this->_data[$this->_db->pk()]) && $func[0] != '_') {
                 $db = Db::instance($this->_db->db, substr($func, 0, -1));
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 $idField = $this->_db->table . '_id';
                 return $db->where([$idField, '=', $this->_data[$this->_db->pk()]])->exec($object);
             } else {
                 $auth = ['checkIndices', '_hooks'];
                 if (Arrays::in($func, $auth)) {
                     return true;
                 }
                 throw new Exception("{$func} is not a model function of {$this->_db}.");
             }
         }
     }
 }
Example #29
0
 public function __call($method, $parameters)
 {
     return $this->write($method, Arrays::first($parameters));
 }
Example #30
0
 function option()
 {
     $options = null !== container()->getThinOptions() ? container()->getThinOptions() : array();
     $args = func_get_args();
     if (func_num_args() > 0) {
         $name = array_shift($args);
         if (is_null($name)) {
             $options = array();
             return $options;
         }
         if (Arrays::is($name)) {
             $options = array_merge($options, $name);
             container()->setThinOptions($options);
         }
         $nargs = count($args);
         if ($nargs > 0) {
             $value = $nargs > 1 ? $args : Arrays::first($args);
             $options[$name] = value($value);
         }
         return Arrays::exists($name, $options) ? $options[$name] : null;
     } else {
         container()->setThinOptions(array());
     }
     return container()->getThinOptions();
 }