public function getOptions() { $class = CrudModel::resolveClass($this->config['model']); $modelObj = new $class(); if (!empty($this->config['find']) && !empty($this->config['model'])) { $method = $method = 'selectOptions' . studly_case($this->config['find']); $val = $this->getValue(); if (!is_array($val)) { if ($val instanceof Collection) { $val = $val->toArray(); } elseif (is_scalar($val)) { $val = [$val]; } } return $modelObj->{$method}($this->getName(), $val); } if (!empty($this->config['model'])) { return CrudModelCollectionBuilder::createTree($modelObj)->fetch(); } elseif (!empty($this->config['find']) && empty($this->config['model'])) { $method = $method = 'selectOptions' . studly_case($this->config['find']); return $this->model->{$method}($this->getName()); } }
private function getModelOptions() { $class = CrudModel::resolveClass($this->config['model']); $modelObj = CrudModel::createInstance($this->config['model'], null, is_numeric($this->value) ? $this->value : null); //$modelObj = new $class(); if (!empty($this->config['find'])) { $method = 'selectOptions' . studly_case($this->config['find']); return $this->formatOptionsArray($modelObj->{$method}($this->model)); } else { if ($modelObj->confParam('tree')) { $collection = CrudModelCollectionBuilder::create($modelObj)->fetch(); } else { $query = $class::query(); if (!empty($this->config['sort'])) { foreach ($this->config['sort'] as $c => $d) { $query->orderBy($c, $d); } } $collection = $query->get(); } } if ($this->isGrouped()) { $options = $this->groupedOptions($collection); } else { $options = $this->flatOptions($collection, $modelObj); } return $options; }
public function crudListExcel($model, $scope) { $obj = CrudModel::createInstance($model, $scope); $params = $this->request->all(); $cols = $obj->getList()->getParam('columns'); $xls = []; $row = []; foreach ($cols as $col) { if ((empty($col['ctype']) || $col['ctype'] != 'checkbox') && $col['data'] != 'actions' && empty($col['invisible'])) { $row[] = $col['title']; } } $xls[] = $row; $query = $this->app['session']->get('current_query_info'); if (empty($query) || !isset($query['sql']) || !isset($query['bind'])) { $q = CrudModelCollectionBuilder::createDataTables($obj, $params)->applyContextFilter()->getCollectionQuery()->getQuery(); $query = ['sql' => $q->toSQL(), 'bind' => $q->getBindings()]; } $rs = \DB::select($query['sql'], $query['bind']); foreach ($rs as $r) { $row = []; foreach ($cols as $col) { if ((empty($col['ctype']) || $col['ctype'] != 'checkbox') && $col['data'] != 'actions' && empty($col['invisible'])) { $row[] = $r[$col['data']] ?? ''; } } $xls[] = $row; } $writer = new \XLSXWriter(); $writer->writeSheet($xls, 'Sheet1'); $data = $writer->writeToString(); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); //keeps ie happy header('Content-Disposition: attachment; filename=xls.xlsx'); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Length: ' . strlen($data)); header('Content-Transfer-Encoding: binary'); echo $data; exit; }