Example #1
0
 /**
  * Add relation field type.
  *
  * @param Entity $entity
  * @param Collection            $collection
  * @param string                $id
  * @param string                $ref
  * @param bool                  $inline
  *
  * @return BasicRelation
  */
 public function relates($entity, $collection, $id, $ref = null, $inline = false)
 {
     if ($ref === null) {
         $ref = str_plural($id);
     }
     $ref = $entity->getEntitiesRepository()->resolve($ref);
     $model = $entity->newModel();
     if (!method_exists($model, $id)) {
         $className = get_class($model);
         throw new \RuntimeException("The target model [{$className}] doesn't have relation [{$id}] defined.");
     }
     $relation = $model->{$id}();
     if (!$relation instanceof Relation) {
         $className = get_class($model);
         throw new \RuntimeException("The method [{$id}] of model [{$className}] did not return valid relation.");
     }
     $relationClassName = class_basename($relation);
     $className = __NAMESPACE__ . '\\' . ($inline ? 'InlineTypes' : 'Types') . '\\' . $relationClassName;
     if (!class_exists($className)) {
         throw new \RuntimeException("Cruddy does not know how to handle [{$relationClassName}] relation.");
     }
     $instance = new $className($entity, $id, $ref, $relation);
     $collection->add($instance);
     return $instance;
 }
Example #2
0
 /**
  * @param Entity $entity
  * @param $id
  *
  * @return \Kalnoy\Cruddy\Contracts\Field
  */
 protected function resolveField(Entity $entity, $id)
 {
     $field = $entity->getFields()->get($id);
     if ($field === null) {
         throw new \RuntimeException("The field [{$entity->getId()}.{$id}] is not found.");
     }
     return $field;
 }
Example #3
0
 /**
  * Get a list of relations that should be eagerly loaded.
  *
  * @return array
  */
 public function eagerLoads($simplified)
 {
     if ($simplified) {
         return $this->entity->eagerLoads();
     }
     $relations = array_reduce($this->items, function ($relations, Column $item) {
         return array_merge($relations, $item->eagerLoads());
     }, $this->entity->eagerLoads());
     return array_unique($relations);
 }
Example #4
0
 /**
  * Bootstrap the application events.
  *
  * @return void
  */
 public function boot()
 {
     $this->loadViewsFrom(__DIR__ . '/../resources/views', 'cruddy');
     $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'cruddy');
     $this->publishes([__DIR__ . '/../public' => public_path('cruddy')], 'public');
     $this->publishes([__DIR__ . '/../resources/views' => base_path('resources/views/vendor/cruddy')], 'views');
     $this->publishes([__DIR__ . '/../config/cruddy.php' => config_path('cruddy.php')], 'config');
     $this->registerRoutes($this->app['router'], $this->app['config']);
     Entity::setEventDispatcher($this->app->make('events'));
 }
Example #5
0
 /**
  * @param string $action
  * @param Entity $entity
  *
  * @return bool
  */
 public function isPermitted($action, Entity $entity)
 {
     $key = "{$entity->getId()}.{$action}";
     return ($user = $this->sentry->getUser()) && $user->hasAccess($key);
 }
Example #6
0
 /**
  * Register saved event handler.
  *
  * @param string $id
  * @param mixed $callback
  *
  * @return void
  */
 public static function saved($id, $callback)
 {
     Entity::saved($id, $callback);
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function toArray()
 {
     return ['multiple' => $this->isMultiple(), 'reference' => $this->reference->getId()] + parent::toArray();
 }
Example #8
0
 /**
  * @return bool
  */
 protected function isPermitted()
 {
     return $this->form->isPermitted($this->getAction());
 }