示例#1
0
文件: Front.php 项目: gtcrais/gtcms
    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;
    }