function traitEntityItemDelete() { $this->traitEntity_assertReason($this->model, 'delete'); $data = ['vendor' => (string) static::getVendor(), 'entity' => \Str::snake($this->getEntity())]; if ($this->model->deleted_at) { // $this->model->restore(); // dd(); $data['state'] = -1; $data['message'] = static::translateAction('item|delete.success.hard', ['model' => $this->model]); $data['models'][] = ['id' => $this->model->id]; $this->model->forceDelete(); } else { $this->model->delete(); $data['state'] = 0; $data['message'] = static::translateAction('item|delete.success.soft', ['model' => $this->model]); $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($this->model)->row($row_type); } $data['models'][] = ['id' => $this->model->id, 'model' => $this->model->toArray(), 'rows' => $rows]; } $this->traitAjax_set($data); Event::notify('larakit::model_delete-admin', $data); return $this->traitAjax_response(); }
function traitEntityRestore() { $this->traitEntity_assertReason($this->model, 'restore'); $this->model->restore(); $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($this->model)->row($row_type); } $data = ['models' => [['model' => $this->model->toArray(), 'rows' => $rows, 'id' => $this->model->id]], 'vendor' => (string) static::getVendor(), 'entity' => \Str::snake($this->getEntity()), 'message' => sprintf($this->traitEntityItemRestore_success(), $this->model), 'state' => 1]; Event::notify('larakit::model_restore-admin', $data); $this->traitAjax_set($data); return $this->traitAjax_response(); }
function setRelations($relations) { if (false === $relations) { return $this; } $ret = []; foreach ($relations as $relation) { Arr::set($ret, $relation, []); } $relations = $ret; $accessor = Accessor::factory($this->model); foreach ($relations as $relation => $subrelation) { $method = \Str::camel('hasmany_' . $relation); if (method_exists($accessor, $method)) { $data = call_user_func([$accessor, $method]); $this->addRelation($relation, $data, $subrelation); } } return $this; }
function get() { try { $args = func_get_args(); $name = array_shift($args); if (!$name) { return (string) \HtmlSpan::addClass('label label-danger')->setContent('Accessor:get()'); } $field = $this->model->{$name}; if (isset($field) && is_object($field) && $field instanceof Model) { return Accessor::factory($field); } $method = 'get' . studly_case($name) . 'Attribute'; if ('getAttribute' == $method) { return null; } if (method_exists($this, $method)) { return call_user_func_array([$this, $method], $args); } return $this->model->{$name}; } catch (\Exception $e) { return $e->getMessage(); } }
function traitEntityHasMany_item() { if (\Request::method() == 'POST') { $this->traitEntityHasMany_save(); //переоткроем модель $model_name = $this->getClassModel(); if ($model_name::isSoftDelete()) { $model = $model_name::withTrashed()->with($this->relation)->find(\Route::input('id')); } else { $model = $model_name::with($this->relation)->find(\Route::input('id')); } $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($model)->row($row_type); } $data = ['models' => [['rows' => $rows, 'model' => $model->toArray(), 'id' => $model->id]], 'vendor' => (string) static::getVendor(), 'entity' => \Str::snake(static::getEntity()), 'message' => sprintf($this->traitEntityHasMany_successMessage(), $model)]; Event::notify('larakit::model_edit-admin', $data); $this->traitAjax_set($data); return $this->traitAjax_response(); } $this->traitAjax_set('id', $this->model->id)->traitAjax_set('result', 'curtain')->traitAjax_set('vendor', (string) \Str::snake($this->getVendor()))->traitAjax_set('entity', (string) \Str::snake($this->getEntity()))->traitAjax_set('model', $this->model->toArray())->traitAjax_set('header', (string) $this->traitEntityHasMany_header())->traitAjax_set('body', (string) $this->traitEntityHasMany_body())->traitAjax_set('footer', (string) $this->traitEntityHasMany_footer()); return $this->traitAjax_response(); }
function traitEntityBelongsTo_item() { if (\Request::method() == 'POST') { $id = (int) \Input::get('belongs_to'); $this->model->{$this->getRelationKey()} = $id; $this->model->save(); // ->sync($ids); //переоткроем модель $model_name = $this->getClassModel(); /** @var Model $model */ if ($model_name::isSoftDelete()) { $model = $model_name::withTrashed(); } else { $model = $model_name::select(); } //откроем со всеми связями, которые могут потребоваться для шаблона ROW foreach ($model->getModel()->getRelations() as $relation) { $model->with($relation); } $model = $model->find(\Route::input('id')); $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($model)->row($row_type); } $data = ['models' => [['rows' => $rows, 'model' => $model->toArray(), 'id' => $model->id]], 'vendor' => (string) static::getVendor(), 'entity' => \Str::snake(static::getEntity()), 'message' => sprintf($this->traitEntityBelongsTo_successMessage(), $model)]; Event::notify('larakit::model_edit-admin', $data); $this->traitAjax_set($data); return $this->traitAjax_response(); } $this->traitAjax_set('id', $this->model->id)->traitAjax_set('result', 'curtain')->traitAjax_set('vendor', static::getVendor())->traitAjax_set('entity', $this->getEntity())->traitAjax_set('model', $this->model->toArray())->traitAjax_set('header', (string) $this->traitEntityBelongsTo_header())->traitAjax_set('body', (string) $this->traitEntityBelongsTo_body())->traitAjax_set('footer', (string) $this->traitEntityBelongsTo_footer()); return $this->traitAjax_response(); }
function traitEntityItemToggle() { $field = \Input::get('field'); $this->traitEntity_assertReason($this->model, 'toggle_' . $field); $method = \Str::camel('switch_' . $field); if (!is_callable([$this, $method])) { throw new Exception('Поле не является тумблером'); } $message = call_user_func([$this, $method]); $this->model->save(); $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($this->model)->row($row_type); } $data = ['models' => [['rows' => $rows, 'model' => $this->model->toArray(), 'id' => $this->model->id]], 'vendor' => (string) static::getVendor(), 'entity' => \Str::snake(static::getEntity()), 'message' => $message ? sprintf($message, $this->model) : 'Значение поля изменено на противоположное']; if (in_array($field, $this->traitEntityItemToggle_mass())) { $rows = []; $model_name = get_class($this->model); foreach ($model_name::get() as $model) { $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($model)->row($row_type); } $data['models'][] = ['rows' => $rows, 'model' => $model->toArray(), 'id' => $model->id]; } } $this->traitAjax_set($data); return $this->traitAjax_response(); }
function traitEntityItem_card() { return Accessor::factory($this->model)->card($this->traitEntityItem_fields(), static::getSection()); }
function traitEntityEditJson_success() { $class_model = get_class($this->model); $model = $class_model::findOrFail($this->model->id); $row_types = ManagerRowType::get(ManagerRowType::makeKey(static::getVendor(), static::getEntity())); $rows = []; foreach ($row_types as $row_type) { $rows[$row_type] = (string) Accessor::factory($model)->row($row_type); } $data = ['models' => [['rows' => $rows, 'model' => $model->toArray(), 'id' => $model->id]], 'vendor' => (string) static::getVendor(), 'entity' => \Str::snake(static::getEntity()), 'message' => sprintf($this->traitEntityEdit_successMessage(), $model)]; Event::notify('larakit::model_edit-admin', $data); $this->traitAjax_set($data); return $this->traitAjax_response(); }