Пример #1
0
 /**
  * Renders the title tag.
  *
  * @param string $title
  * @return string
  */
 public function title($title = null)
 {
     if ($title) {
         $title .= ' - ' . Config::get('app.title');
     } else {
         $title = Config::get('app.title');
     }
     return '<title>' . $title . '</title>';
 }
Пример #2
0
 /**
  * Checks if the captcha code is valid, 
  * using Google ReCAPTCHA.
  * 
  * @param   string  $value The potential captcha code
  * @return  bool
  */
 public function checkReCaptcha($code)
 {
     $data = ['secret' => Config::get('app.recaptcha_secret'), 'response' => $code, 'remoteip' => Request::getClientIp()];
     $url = 'https://www.google.com/recaptcha/api/siteverify';
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_POST, true);
     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     $result = curl_exec($curl);
     $resultArray = json_decode($result, true);
     return isset($resultArray['success']) and $resultArray['success'];
 }
Пример #3
0
 /**
  * Returns comments and respects pagination
  * 
  * @param  string $foreignType Identifier for the content the comments are related to (usually a model class)
  * @param  int    $foreignId   ID, if the comments are related to a certain model instance
  * @return string
  */
 public function paginate($foreignType, $foreignId)
 {
     $perPage = Config::get('app.frontItemsPerPage');
     $comments = Comment::where('foreign_type', '=', $foreignType)->where('foreign_id', '=', $foreignId)->paginate($perPage)->setPath(Request::url());
     return View::make('comments.paginate', compact('comments'))->render();
 }
Пример #4
0
 /**
  * Generates an index page from a model and $data
  * 
  * @param  array  $data             Array with information how to build the form. See $defaults for details.
  * @param  string $userInterface    Frontend ("front") or backend ("admin")?
  * @return void
  */
 public function index($data, $userInterface = 'admin')
 {
     $controller = $this->getControllerOrFail();
     /*
      * Access checking is only available for the backend.
      * Frontend controllers have to perform it on their own.
      */
     if ($userInterface == 'admin') {
         if (!$controller->checkAccessRead()) {
             return;
         }
     }
     /*
      * Set default values
      */
     $defaults = ['buttons' => ['new'], 'search' => '', 'searchFor' => 'title', 'tableHead' => [], 'tableRow' => [], 'actions' => ['edit', 'delete', 'restore'], 'brightenFirst' => true, 'sortby' => 'id', 'order' => 'desc', 'filter' => false, 'permaFilter' => null, 'dataSource' => null, 'infoText' => ''];
     $data = array_merge($defaults, $data);
     $modelClass = $controller->getModelClass();
     /*
      * Generate Buttons
      */
     $buttons = '';
     if (is_array($data['buttons'])) {
         foreach ($data['buttons'] as $button) {
             $type = strtolower($button);
             switch ($type) {
                 case 'new':
                     $url = route($userInterface . '.' . strtolower($controller->getControllerName()) . '.create');
                     $buttons .= button(trans('app.create'), $url, 'plus-circle');
                     break;
                 case 'category':
                     $url = route($userInterface . '.' . str_singular(strtolower($controller->getModuleName())) . 'cats.index');
                     $buttons .= button(trans('app.categories'), $url, 'folder');
                     break;
                 case 'config':
                     $url = url($userInterface . '/' . strtolower($controller->getModuleName()) . '/config');
                     $buttons .= button(trans('app.config'), $url, 'cog');
                     break;
                 default:
                     $buttons = $button;
             }
         }
     }
     /*
      * Get search string.
      */
     if (Input::old('search')) {
         $data['search'] = Input::old('search');
     }
     if (Input::get('search')) {
         $data['search'] = Input::get('search');
     }
     /*
      * Get sort attributes.
      */
     if (!is_array($data['dataSource'])) {
         if (Input::get('sortby')) {
             $sortby = strtolower(Input::get('sortby'));
             if (in_array($sortby, $data['tableHead'])) {
                 $data['sortby'] = $sortby;
             }
             $order = strtolower(Input::get('order'));
             if ($order === 'desc' or $order === 'asc') {
                 $data['order'] = $order;
             }
         }
         $sortSwitcher = sort_switcher($data['sortby'], $data['order'], $data['search']);
     } else {
         $sortSwitcher = null;
     }
     /*
      * Switch recycle bin mode: Show soft deleted models if recycle bin mode is enabled.
      */
     if ($userInterface == 'admin' and (new $modelClass())->isSoftDeleting()) {
         // isSoftDeleting() is instance-tied
         $recycleBinMode = Input::get('binmode');
         if ($recycleBinMode !== null) {
             Session::put('recycleBinMode', (bool) $recycleBinMode);
         }
         $recycleBin = recycle_bin_button();
     } else {
         $recycleBin = '';
     }
     /*
      * Retrieve models from DB (or array) and create paginator
      */
     $perPage = Config::get('app.' . $userInterface . 'ItemsPerPage');
     if (is_array($data['dataSource'])) {
         $page = (int) Paginator::resolveCurrentPage();
         if ($page) {
             // Ensure $page always starts at 0:
             $page--;
         } else {
             $page = 0;
         }
         $offset = $page * $perPage;
         $models = array_slice($data['dataSource'], $offset, $perPage);
         // We have to take the models from the array
         $models = new Paginator($models, sizeof($data['dataSource']), $perPage);
     } else {
         $models = $modelClass::orderBy($data['sortby'], $data['order']);
         if ($userInterface == 'admin' and Session::get('recycleBinMode')) {
             $models = $models->withTrashed();
             // Show trashed
         }
         if ($data['filter'] === true) {
             $models = $models->filter();
         } elseif ($data['filter'] instanceof Closure) {
             $models = $data['filter']($models);
         }
         if ($data['permaFilter']) {
             $models = $data['permaFilter']($models);
         }
         if ($data['search']) {
             $pos = strpos($data['search'], ':');
             if ($pos === false) {
                 if (is_array($data['searchFor'])) {
                     $models = $models->whereHas($data['searchFor'][0], function ($query) use($data) {
                         $query->where($data['searchFor'][1], 'LIKE', '%' . $data['search'] . '%');
                     });
                 } elseif ($data['searchFor']) {
                     $models = $models->where($data['searchFor'], 'LIKE', '%' . $data['search'] . '%');
                 }
             } else {
                 $searchFor = substr($data['search'], 0, $pos);
                 $search = substr($data['search'], $pos + 1);
                 $models = $models->where($searchFor, 'LIKE', '%' . $search . '%');
                 // TODO: Check if attribute $searchFor exists?
                 // Use fillables &/ tableHead attribute names
             }
         }
         $models = $models->paginate($perPage)->setPath(Request::url());
     }
     $paginator = $models->appends(['sortby' => $data['sortby'], 'order' => $data['order'], 'search' => $data['search']])->render();
     /*
      * Prepare the table head
      */
     $tableHead = array();
     foreach ($data['tableHead'] as $title => $sortby) {
         if ($sortby != null) {
             $tableHead[] = HTML::link(URL::current() . '?sortby=' . urlencode($sortby), $title);
         } else {
             $tableHead[] = $title;
         }
     }
     if (sizeof($data['actions']) > 0) {
         $tableHead[] = trans('app.actions');
     }
     /*
      * Prepare the rows
      */
     $tableRows = array();
     foreach ($models as $model) {
         $row = $data['tableRow']($model);
         if (is_array($data['actions']) and sizeof($data['actions']) > 0) {
             $actionsCode = '';
             foreach ($data['actions'] as $action) {
                 if (is_string($action)) {
                     $action = strtolower($action);
                     switch ($action) {
                         case 'edit':
                             if ($model->modifiable()) {
                                 $actionsCode .= icon_link('edit', trans('app.edit'), route($userInterface . '.' . strtolower($controller->getControllerName()) . '.edit', [$model->id]));
                             }
                             break;
                         case 'delete':
                             $urlParams = '?method=DELETE&_token=' . csrf_token();
                             if ($model->modifiable()) {
                                 $actionsCode .= icon_link('trash', trans('app.delete'), route($userInterface . '.' . strtolower($controller->getControllerName()) . '.destroy', [$model->id]) . $urlParams, false, ['data-confirm-delete' => true]);
                             }
                             break;
                         case 'restore':
                             if ($model->isSoftDeleting() and $model->trashed()) {
                                 $actionsCode .= icon_link('undo', trans('app.restore'), route($userInterface . '.' . strtolower($controller->getControllerName()) . '.restore', [$model->id]));
                             }
                             break;
                     }
                     $actionsCode .= ' ';
                 }
                 if ($action instanceof Closure) {
                     $actionsCode .= $action($model);
                 }
             }
             $row[] = raw($actionsCode);
         }
         if ($data['actions'] instanceof Closure) {
             $row[] = $data['actions']($model);
         }
         $tableRows[] = $row;
     }
     /*
      * Generate the table
      */
     $modelTable = HTML::table($tableHead, $tableRows, $data['brightenFirst']);
     /*
      * Generate the view
      */
     $controller->pageView('model_index', ['buttons' => $buttons, 'infoText' => $data['infoText'], 'modelTable' => $modelTable, 'sortSwitcher' => $sortSwitcher, 'recycleBin' => $recycleBin, 'paginator' => $paginator, 'searchString' => $data['search'], 'showSearchBox' => $data['searchFor'] and !$data['dataSource'] ? true : false]);
 }