Exemple #1
0
<?php

require_once '../vendor/autoload.php';
require_once '../lib/autoload.php';
session_name(\My\Config::get('session_name'));
session_start();
try {
    \My\System::run();
} catch (\My\Exception_system_exit $e) {
    die;
} catch (\My\Exception_view_not_found $e) {
    echo "<h1>View-Template not found.</h1>";
    echo "<h2>" . $e->getMessage() . "</h2>";
    ddd($e);
} catch (\Exception $e) {
    echo "<h1>Whoops</h1>";
    ddd($e);
}
Exemple #2
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(['\\My\\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;
 }