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 traitEntityBelongsTo_successMessage()
 {
     $method = \Str::camel('success_message_' . $this->relation);
     $success_message = '';
     if (method_exists($this, $method)) {
         $success_message = call_user_func([$this, $method]);
     }
     return $success_message ? $success_message : 'Связи для записи "%s" успешно установлены';
 }
Пример #3
0
 protected function listen($event, $method = null)
 {
     if (!$method) {
         $segments = explode('.', $event);
         if (2 == count($segments)) {
             $method = \Str::camel($segments[0] . '_' . $segments[1]);
         }
     }
     Event::listen($event, $this->key . '@' . $method);
 }
Пример #4
0
 /**
  * @param \HTML_QuickForm2_Node $element
  *
  * @return \HTML_QuickForm2_Node
  */
 function initElement(\HTML_QuickForm2_Node $element)
 {
     \Debugbar::addMessage(($this->namespace ? $this->namespace . '::' : '') . 'models/' . $this->model->getEntity() . '.' . $element->getName());
     $config = \Config::get(($this->namespace ? $this->namespace . '::' : '') . 'models/' . $this->model->getEntity() . '.' . $element->getName());
     if (is_array($config)) {
         foreach ($config as $k => $v) {
             $method = \Str::camel('set_' . $k);
             if (is_callable([$element, $method])) {
                 call_user_func([$element, $method], $v);
             }
         }
     }
     return $element;
 }
Пример #5
0
 public function hasManyThrough($targetClassName, $joinTableName = null, $baseKey = null, $targetKey = null)
 {
     $model = Model::make($targetClassName);
     if (is_null($joinTableName)) {
         $tables = array($this->orm->tableName, $model->orm->tableName);
         sort($tables, SORT_STRING);
         $joinTableName = join("_", $tables);
     }
     if (is_null($baseKey)) {
         $baseKey = Str::camel($this->orm->tableName) . "ID";
     }
     if (is_null($targetKey)) {
         $targetKey = Str::camel($model->orm->tableName) . "ID";
     }
     return $model->select($model->orm->tableName . ".*")->join($joinTableName, array($model->orm->tableName . ".id", "=", $joinTableName . "." . $targetKey))->whereEqual($joinTableName . "." . $baseKey, $this->id);
 }
Пример #6
0
 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;
 }
Пример #7
0
 /**
  * Generate Model file from template
  * @param  Packettide\Sire\Sire $sire
  */
 public function run($sire)
 {
     $fields = $sire->fields;
     $tempRelationships = $sire->fields;
     $relationships = array();
     foreach ($tempRelationships as $key => $value) {
         if (isset($value['relationshipType']) && isset($value['relatedModel'])) {
             $temp = array();
             $temp['_name'] = \Str::camel($value['_name']);
             $temp['type'] = $value['relationshipType'];
             $temp['model'] = $value['relatedModel'];
             array_push($relationships, $temp);
         }
     }
     $fields = array_map(function ($el) {
         if (isset($el['fieldTypeOptions'])) {
             $el['breeAttrs'] = array_map(function ($v, $k) {
                 if (!($v === true || $v === false)) {
                     $v = "'{$v}'";
                 }
                 return sprintf("'%s' => %s", $k, $v);
             }, $el['fieldTypeOptions'], array_keys($el['fieldTypeOptions']));
         }
         $el['breeAttrs'] = isset($el['breeAttrs']) ? $el['breeAttrs'] : array();
         if (isset($el['relatedModel'])) {
             array_push($el['breeAttrs'], "'related' => '{$el['relatedModel']}'");
         }
         $el['breeAttrs'] = implode(", ", $el['breeAttrs']);
         return $el;
     }, $fields);
     $fields = $sire->assocToNumeric($fields);
     $path = app_path() . '/models/';
     $toTemplate = array("rules" => $sire->getRules(), "relationships" => $relationships, "breeFields" => $fields);
     foreach ($this->templates as $name => $template) {
         if (!$this->safe[$sire->name->upper() . '.php'] || !is_file($path . $name)) {
             $sire->templater->template($template, $toTemplate, $path . $name);
         }
     }
 }
Пример #8
0
 /* Dynamic route
  *
  *  controller must be same as controller class without 'Controller' string.
  *  action must be same as method, and should be slug string.
  *   EX: 'pages/show-list' will call PagesController and showList method of PagesController
  *
  */
 Route::match(['GET', 'POST'], '{controller}/{action?}/{args?}', function ($controller, $action = 'index', $args = '') {
     $controller = str_replace('-', ' ', strtolower(preg_replace('/[^A-Za-z0-9\\-]/', '', $controller)));
     $controller = str_replace(' ', '', Str::title($controller));
     $controller = '\\' . $controller . 'Controller';
     if (!class_exists($controller)) {
         return App::abort(404, "Controller '{$controller}' was not existed.");
     }
     $action = str_replace('-', ' ', preg_replace('/[^A-Za-z0-9\\-]/', '', $action));
     $method = Str::camel($action);
     if (!method_exists($controller, $method)) {
         return App::abort(404, "Method '{$method}' was not existed.");
     }
     $params = explode("/", $args);
     /*
      * Check permission
      */
     if (!Permission::checkPermission($controller, $method, $params)) {
         return App::abort(403, 'Need permission to access this page.');
     }
     /*
      * End check permission
      */
     $app = app();
     $controller = $app->make($controller);
Пример #9
0
 /**
  * @param LaraForm $class_name
  *
  * @return array
  */
 protected static function deNormalizeFormFilter($class_name)
 {
     preg_match('|^([A-Za-z\\\\]+)FormFilter\\\\FormFilter([A-Za-z]+)$|Umsi', $class_name, $matches);
     $vendor = trim(Arr::get($matches, 1, ''), '\\');
     $entity = trim(Arr::get($matches, 2, ''), '\\');
     $reflection = new \ReflectionClass($class_name);
     $extends = $reflection->getParentClass();
     if ($extends->getName() != FormFilter::class) {
         $parent_entity = self::getEntity($extends->getName());
         $parent_studly_entity = \Str::camel($parent_entity);
         $section = mb_substr($entity, mb_strlen($parent_studly_entity));
         $entity = $parent_entity;
     } else {
         $section = '';
     }
     $ret = ['vendor' => str_replace('\\', '', \Str::snake($vendor, '-')), 'entity' => \Str::snake($entity), 'section' => \Str::snake($section)];
     return $ret;
 }
Пример #10
0
 function traitModelAttach__createNotExistsFields()
 {
     /** @var Model $this */
     if (count($this->_attach_not_exists_columns)) {
         \Schema::table($this->getTable(), function (Blueprint $table) {
             foreach ($this->_attach_not_exists_columns as $column) {
                 $method = \Str::camel('add_attach_field_' . $column);
                 if (method_exists($this, $method)) {
                     call_user_func([$this, $method], $table);
                 }
             }
         });
     }
 }
Пример #11
0
 /**
  * Compile the current seedingStub with the seed template
  * @return mixed
  */
 protected function compile()
 {
     // Grab the template
     $template = File::get(__DIR__ . '/templates/seed.txt');
     // Replace the classname
     $template = str_replace('{{className}}', \Str::camel($this->database) . "TableSeeder", $template);
     $template = str_replace('{{run}}', $this->seedingStub, $template);
     return $template;
 }
 /**
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function getPropertiesFromMethods($model)
 {
     $methods = get_class_methods($model);
     if ($methods) {
         foreach ($methods as $method) {
             if (\Str::startsWith($method, 'get') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') {
                 //Magic get<name>Attribute
                 $name = \Str::snake(substr($method, 3, -9));
                 if (!empty($name)) {
                     $this->setProperty($name, null, true, null);
                 }
             } elseif (\Str::startsWith($method, 'set') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') {
                 //Magic set<name>Attribute
                 $name = \Str::snake(substr($method, 3, -9));
                 if (!empty($name)) {
                     $this->setProperty($name, null, null, true);
                 }
             } elseif (\Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
                 //Magic set<name>Attribute
                 $name = \Str::camel(substr($method, 5));
                 if (!empty($name)) {
                     $reflection = new \ReflectionMethod($model, $method);
                     $args = $this->getParameters($reflection);
                     //Remove the first ($query) argument
                     array_shift($args);
                     $this->setMethod($name, $reflection->class, $args);
                 }
             } elseif (!method_exists('Eloquent', $method) && !\Str::startsWith($method, 'get')) {
                 //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
                 $reflection = new \ReflectionMethod($model, $method);
                 $file = new \SplFileObject($reflection->getFileName());
                 $file->seek($reflection->getStartLine() - 1);
                 $code = '';
                 while ($file->key() < $reflection->getEndLine()) {
                     $code .= $file->current();
                     $file->next();
                 }
                 $begin = strpos($code, 'function(');
                 $code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
                 $begin = stripos($code, 'return $this->');
                 $parts = explode("'", substr($code, $begin + 14), 3);
                 //"return $this->" is 14 chars
                 if (isset($parts[2])) {
                     list($relation, $returnModel, $rest) = $parts;
                     $returnModel = "\\" . ltrim($returnModel, "\\");
                     $relation = trim($relation, ' (');
                     if ($relation === "belongsTo" or $relation === 'hasOne') {
                         //Single model is returned
                         $this->setProperty($method, $returnModel, true, null);
                     } elseif ($relation === "belongsToMany" or $relation === 'hasMany') {
                         //Collection or array of models (because Collection is Arrayable)
                         $this->setProperty($method, '\\Illuminate\\Database\\Eloquent\\Collection|' . $returnModel . '[]', true, null);
                     }
                 } else {
                     //Not a relation
                 }
             }
         }
     }
 }
Пример #13
0
 /**
  * Creates a camel plural
  */
 public function transformPluralCamel($key, $value)
 {
     return array(Str::plural(Str::camel($key)), Str::plural(Str::camel($value)));
 }
 function traitEntityHasMany_save()
 {
     $method = \Str::camel('save_' . $this->relation);
     if (method_exists($this, $method)) {
         call_user_func([$this, $method]);
         return;
     }
     $ids = array_keys((array) \Input::get('has_many'));
     $model_class = $this->getRelationModel();
     $entity = explode('\\', $this->getClassModel());
     $key = \Str::snake(end($entity) . 'Id');
     $model_class::where($key, $this->model->id)->update([$key => 0]);
     foreach ($ids as $id) {
         $model = $model_class::find($id);
         if ($model) {
             $model->{$key} = $this->model->id;
             $model->save();
         }
     }
 }
 function traitEntityBelongsToMany_successMessage()
 {
     $method = \Str::camel('success_message_' . $this->relation);
     $success_message = '';
     if (method_exists($this, $method)) {
         $success_message = call_user_func([$this, $method]);
     }
     return $success_message ? $success_message : \Lang::get('larakit::relations.success.belongs_to_many');
 }
Пример #16
0
 /**
  * Convert a value to camel case.
  *
  * @param  string  $value
  * @return string
  */
 function camel_case($value)
 {
     return Str::camel($value);
 }
Пример #17
0
 private function facilities()
 {
     $class = get_class(new Facility());
     $this->log($this->verb . ' Facilities');
     $url = $this->baseUrl . "map_region?c:limit=1000";
     $data = $this->getCensusData($url);
     if (!$data) {
         return false;
     }
     // Collection of Eloquent objects from API
     $apiCollection = new Collection();
     foreach ($data->map_region_list as $facility) {
         $values = [];
         $values['id'] = isset($facility->facility_id) ? $facility->facility_id : null;
         $values['name'] = isset($facility->facility_name) ? $facility->facility_name : null;
         $values['slug'] = isset($facility->facility_name) ? Str::camel($facility->facility_name) : null;
         $values['continent_id'] = isset($facility->zone_id) ? $facility->zone_id : null;
         $values['facility_type_id'] = isset($facility->facility_type_id) ? $facility->facility_type_id : null;
         $values['region_id'] = isset($facility->map_region_id) ? $facility->map_region_id : null;
         $values['currency_amount'] = isset($facility->reward_amount) ? $facility->reward_amount : null;
         $values['currency_id'] = isset($facility->reward_currency_id) ? $facility->reward_currency_id : null;
         $values['lat'] = isset($facility->location_z) ? $facility->location_z * $this->latScale + $this->latOffset : null;
         $values['lng'] = isset($facility->location_x) ? $facility->location_x * $this->lngScale + $this->lngOffset : null;
         $values['elevation'] = isset($facility->location_z) ? $facility->location_y * $this->elevationScale + $this->elevationOffset : null;
         $facility = new Facility($values);
         // Add facility type names to facilities
         if (in_array($facility->facilityType->name, ['Amp Station', 'Bio Lab', 'Interlink Facility', 'Tech Plant'])) {
             $facility->name .= " " . $facility->facilityType->name;
             $facility->slug = Str::camel($facility->name);
         }
         $apiCollection->add($facility);
     }
     $facilities = Facility::all();
     $this->addModels($class, $facilities, $apiCollection);
     $this->deleteModels($class, $facilities, $apiCollection);
     $compare = ['name', 'slug', 'continent_id', 'facility_type_id', 'region_id', 'lat', 'lng', 'elevation', 'currency_amount', 'currency_id'];
     $this->updateModels($class, Facility::all(), $apiCollection, $compare);
 }