public static function index($loginRedirect) { $startUrl = ""; AdminHistoryManager::clearHistory(); if ($loginRedirect) { $defaultModel = AdminController::getDefaultModelForUser(); $startUrl = "/admin/" . $defaultModel; AdminHistoryManager::addHistoryLink($startUrl, $defaultModel); } else { AdminHistoryManager::addHistoryLink(); } $ajaxRequest = \Request::ajax() && \Request::get('getIgnore_isAjax') ? true : false; $getSearchResults = isset($_GET['getIgnore_getSearchResults']) && $ajaxRequest ? true : false; $searchIsOpen = isset($_GET['getIgnore_searchIsOpen']) && $ajaxRequest ? true : false; /** @var \App\BaseModel $entity */ $entity = self::$entity; /** @var \App\BaseModel $fullEntity */ $fullEntity = self::$modelConfig->myFullEntityName(); $indexType = self::$modelConfig->index ? ucfirst(self::$modelConfig->index) : ''; $searchParams = array(); $searchDataWithFieldValues = array(); $ordering = Tools::getSearchAndOrderGets(false, false, false, true); if ($indexType == 'Tree') { $objects = $fullEntity::where('depth', 0)->orderBy('position', 'asc')->get(); } else { $searchDataWithFieldValues = AdminHelper::getSearchData(self::$modelConfig, true); $input = array(); AdminHelper::standaloneCheck(self::$modelConfig, 'index', $input); $orderAndDirection = AdminHelper::getOrderParams(self::$modelConfig); $objects = $fullEntity::searchResultsEntities(self::$modelConfig)->where(function ($query) { if (self::$modelConfig->name == 'User' && !\Auth::user()->is_superadmin) { $query->where('is_superadmin', 0); } })->orderBy($orderAndDirection['orderBy'], $orderAndDirection['direction'])->paginate(self::$modelConfig->perPage); } $addEntity = true; if ($indexType == 'Tree' && self::$modelConfig->maxFirstLevelItems && $fullEntity::where('depth', 0)->count() >= self::$modelConfig->maxFirstLevelItems) { $addEntity = false; } $viewData = array('active' => $entity, 'modelConfig' => self::$modelConfig, 'objects' => $objects, 'addEntity' => $addEntity, 'searchParams' => $searchParams, 'searchDataWithFieldValues' => $searchDataWithFieldValues, 'ordering' => $ordering, 'ajaxRequest' => $ajaxRequest, 'loginRedirect' => $loginRedirect, 'getSearchResults' => $getSearchResults, 'searchIsOpen' => $searchIsOpen, 'indexType' => $indexType); if ($ajaxRequest) { $view = \View::make('gtcms.admin.elements.index' . $indexType . 'Content')->with($viewData); if ($loginRedirect) { $data = array('success' => true, 'setUrl' => $startUrl, 'view' => $view->render()); return \Response::json($data); } $data = array('success' => true, 'view' => $view->render(), 'setHistoryLinks' => false, 'modelConfigName' => self::$modelConfig->name, 'indexView' => true, 'setUrl' => "/admin/" . $entity . Tools::getGets(), 'getParams' => Tools::getGets(), 'entity' => $entity, 'searchDataWithFieldValues' => $searchDataWithFieldValues || $ordering ? true : false); return \Response::json($data); } else { return \View::make('gtcms.admin.elements.index')->with($viewData); } }
public static function drawObjectTable($objects, ModelConfig $modelConfig, $tableType = 'table', $parent = "", $searchDataWithFieldValues = false, $ordering = false, $quickEdit = false) { $hasPositionInParent = false; $parentModelName = false; $hidePositionControls = $modelConfig->hidePositionControls; if ($tableType == 'sideTable' && $objects->count()) { /** @var BaseModel $firstObject */ $firstObject = $objects[0]; $configuration = $firstObject->getRelatedModelConfigurationInParentModel($modelConfig); $hasPositionInParent = $configuration['position']; $hidePositionControls = $configuration['hidePositionControls']; $parentModelName = $configuration['parentModelName']; } $objectsAreMovable = ($modelConfig->position && $tableType == 'table' || $hasPositionInParent) && !$hidePositionControls && $objects->count() > 1; $tree = ""; $ctrlNum = 1; $controls = true; // -------------- EXCEPTIONS --------------- // ------------ END EXCEPTIONS ------------- if (!$quickEdit) { $tree .= ' <table class="table table-striped table-hover table-type-' . $tableType . ($objectsAreMovable ? ' hasPositioning' : '') . '"> <tbody class="' . ($searchDataWithFieldValues || $ordering ? ' searchDataPresent' : '') . '"> <tr>'; $counter = 0; foreach ($modelConfig->formFields as $field) { if ($field->restrictedToSuperadmin && !\Auth::user()->is_superadmin) { continue; } $userRole = \Auth::user()->role; if ($field->restrictedAccess && !$field->restrictedAccess->{$userRole}) { continue; } if ($field->{$tableType}) { $counter++; $tree .= ' <th class="controlButtons ' . ($objectsAreMovable && $counter == 1 ? 'sortablePadding' : '') . '" ' . ($objectsAreMovable && $counter == 1 ? 'colspan="2"' : '') . '><span>' . $field->label . '</span>'; if ($field->order && $tableType == 'table') { $tree .= ' <div class="sortLinks"> <a class="btn btn-xs" href="/admin/' . $modelConfig->name . '?&orderBy=' . $field->property . '&direction=asc&getIgnore_getSearchResults=true' . Tools::getSearchAndOrderGets(false, true, true) . '" class="sortLink" data-loadtype="fadeIn"> <i class="fa fa-caret-up"></i> </a> <a class="btn btn-mini" href="/admin/' . $modelConfig->name . '?orderBy=' . $field->property . '&direction=desc&getIgnore_getSearchResults=true' . Tools::getSearchAndOrderGets(false, true, true) . '" class="sortLink" data-loadtype="fadeIn"> <i class="fa fa-caret-down"></i> </a> </div> '; } $tree .= '</th>'; } } if ($controls) { $tree .= ' <th class="controls' . $ctrlNum . '"><span>' . trans('gtcms.controls') . '</span></th>'; } $tree .= ' </tr>'; } /** @var BaseModel $object */ foreach ($objects as $object) { $objectName = ""; $gets = $parent ? $parent . (Tools::getGets() ? "&" . Tools::getGets(array(), false, false) : '') : Tools::getGets(); $tree .= ' <tr class="depth ' . ($objectsAreMovable ? 'isSortable' : '') . ' ' . ($quickEdit ? 'rowSelectize' : '') . '" data-objectid="' . $object->id . '" data-modelname="' . $modelConfig->name . '" data-parentname="' . $parentModelName . '" >'; //regular fields $counter = 0; foreach ($modelConfig->formFields as $index => $field) { if ($field->restrictedToSuperadmin && !\Auth::user()->is_superadmin) { continue; } $userRole = \Auth::user()->role; if ($field->restrictedAccess && !$field->restrictedAccess->{$userRole}) { continue; } if ($field->{$tableType}) { $counter++; $image = false; $method = false; if ($field->displayProperty) { if ($field->displayProperty->type == 'accessor') { $property = $field->displayProperty->method; } else { if ($field->displayProperty->type == 'image') { $method = $field->displayProperty->method; $image = true; } else { if ($field->displayProperty->type == 'model') { $property = $field->displayProperty->property; $displayProperty = $field->displayProperty ? $field->displayProperty : $field->property; $method = $displayProperty->method; $relatedProperty = $displayProperty->property; if ($object->{$method}()->count()) { if ($displayProperty->multiple) { $relatedModels = $object->{$method}()->withPivot('position')->orderBy('pivot_position', 'asc')->get(); $value = ""; foreach ($relatedModels as $relModel) { $value .= $relModel->{$relatedProperty} . ", "; } $value = rtrim($value, ", "); } else { $value = $object->{$method}->{$relatedProperty}; } } else { $value = " - "; } } else { $property = $field->property; } } } } else { $property = $field->property; } if ($objectsAreMovable && $counter == 1) { $tree .= "\n\t\t\t\t\t\t\t\t<td class='sortHandle'>\n\t\t\t\t\t\t\t\t\t<div class='sortHandle'>\n\t\t\t\t\t\t\t\t\t\t<i class='fa fa-ellipsis-v'></i>\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</td>"; } $tree .= "<td>"; if (property_exists($field, $tableType . 'Link') && !$image) { if ($field->displayProperty && $field->displayProperty->type == 'model') { $tree .= '<a href="/admin/' . $modelConfig->name . '/edit/' . $object->id . $gets . '">' . ($object->{$method} ? $object->{$method}->{$property} : '- deleted -') . '</a>'; if (!$objectName) { $objectName = $object->{$method} ? $object->{$method}->{$property} : ''; } } else { $tree .= '<a href="/admin/' . $modelConfig->name . '/edit/' . $object->id . $gets . '">' . $object->{$property} . '</a>'; if (!$objectName) { $objectName = $object->{$property}; } } } else { if ($image) { if (!$method) { $method = $image; } $tree .= ' <a href="/admin/' . $modelConfig->name . '/edit/' . $object->id . $gets . '"> <img style="height: 60px;" src="' . $object->{$method}('url', 'gtcmsThumb') . '"> </a>'; if (!$objectName) { $objectName = "image"; } } else { if ($field->displayProperty && $field->displayProperty->type == 'model') { if ($field->displayProperty->multiple) { $tree .= $value; } else { $tree .= $object->{$method} ? $object->{$method}->{$property} : ' - '; } } else { if (in_array($field->type, array('date', 'dateTime'))) { $tree .= $object->formatDate($object->{$property}, $field->displayProperty->dateFormat ? $field->displayProperty->dateFormat : $field->dateFormat); } else { if ($field->type == 'checkbox') { $tree .= $object->{$property} ? self::drawCheckboxIcon(true) : self::drawCheckboxIcon(false); } else { if ($field->type == 'select' && $field->indexSelect) { $originalValue = $object->{$property}; $listMethod = $field->selectType->listMethod; $options['class'] = ""; $list = array(); if ($field->selectType->type == 'model') { $selectModel = $field->selectType->modelName; $fullModel = ModelConfig::fullEntityName($selectModel); $list = $fullModel::$listMethod(); } else { if ($field->selectType->type == 'list') { $entity = $modelConfig->myFullEntityName(); $list = $entity::$listMethod(); } } if (!is_array($list) && is_object($list)) { $reflection = new \ReflectionClass($list); if ($reflection->getShortName() == "Collection") { /** @var \Illuminate\Support\Collection $list */ $list = $list->toArray(); } } if (!$field->required) { $null = array('' => '-'); $list = $null + $list; } $options['class'] .= ' doSelectize selectizeNoCreate ajaxSelectUpdate '; $options['class'] .= $field->required ? " required " : ''; $options['data-classname'] = $modelConfig->name; $options['data-objectid'] = $object->id; $options['data-property'] = $property; $options['data-token'] = csrf_token(); if ($field->indexClass) { $options['class'] .= ' ' . $field->indexClass . ' '; } else { $options['class'] .= ' standardSelectWidth '; } $tree .= \Form::select($field->property, $list, $originalValue, $options); } else { $tree .= $object->{$property}; } } } } } } $tree .= "</td>"; } } if ($controls) { $quickEditControl = ""; if (config('gtcms.premium') && $modelConfig->getQuickEditFields('all')) { $quickEditControl = GtcmsPremium::getQuickEditControl($modelConfig, $object, $gets); } if ($object->isDeletable()) { $tree .= '<td class="controlButtons">'; $tree .= $quickEditControl; $tree .= '<a href="/admin/' . $modelConfig->name . '/delete/' . $object->id . $gets . '" class="btn btn-default btn-xs deleteButton" data-modelname="' . $modelConfig->hrName . '" data-objectname="' . $objectName . '" > <i class="fa fa-times"></i> </a> </td>'; } else { $tree .= ' <td class="controlButtons">'; $tree .= $quickEditControl; $tree .= '<a href="#" class="btn btn-default btn-xs deleteButton disabled" > <i class="fa fa-times"></i> </a> </td>'; } } $tree .= '</tr>'; } if (!$quickEdit) { $tree .= ' </tbody> </table>'; if ($tableType == 'table' && $objects->total() > $modelConfig->perPage) { $tree .= '<div class="paginationContainer">' . $objects->appends(Tools::getGets(array('page' => NULL, 'getIgnore_getSearchResults' => 'true'), TRUE))->links() . '</div>'; } } return $tree; }