Esempio n. 1
0
 public function read()
 {
     $list = cfg('resources', 'models', $this->name);
     $rels = array('collection', 'model');
     $data = array();
     $pack = array();
     foreach ($list as $model) {
         $cname = \System\Loader::get_class_from_model($model);
         $model = \System\Loader::get_model_from_class($model);
         $schema = $cname::get_visible_schema($this->request->user);
         foreach ($schema['attrs'] as $attr) {
             if (in_array($attr['type'], $rels)) {
                 $rel_cname = \System\Loader::get_class_from_model($attr['model']);
                 $rel_model = \System\Loader::get_model_from_class($attr['model']);
                 if (!array_key_exists($rel_model, $pack)) {
                     $pack[$rel_model] = $rel_cname::get_visible_schema($this->request->user);
                 }
             }
         }
         $pack[$model] = $schema;
     }
     foreach ($pack as $name => $def) {
         $data[] = array("name" => $name, "parents" => array('model'), "static" => $def);
     }
     $this->content = json_encode(array("data" => $data));
 }
Esempio n. 2
0
 /** Ask if user has right to do this
  * @param string      $method One of created, browsed
  * @param System\User $user   User to get perms for
  * @return bool
  */
 public static function can_user($method, \System\User $user)
 {
     if ($user->is_root()) {
         return true;
     }
     $cname = get_called_class();
     $conds = array();
     if (isset($cname::$access) && isset($cname::$access[$method]) && !is_null($cname::$access[$method])) {
         return !!$cname::$access[$method];
     }
     if ($user->is_guest()) {
         $conds['public'] = true;
     } else {
         $groups = $user->get_groups();
         if (any($groups)) {
             $conds[] = 'id_group IN (' . implode(',', collect_ids($groups)) . ')';
         }
     }
     $perm = \System\User\Perm::get_first()->add_filter(array('attr' => 'trigger', 'type' => 'in', 'in' => array('model-' . $method, '*')))->add_filter(array('attr' => 'name', 'type' => 'in', 'in' => array(\System\Loader::get_model_from_class($cname) . \System\Loader::SEP_MODEL . $method, '*')))->where($conds)->fetch();
     return $perm ? $perm->allow : static::get_default_for($method);
 }
Esempio n. 3
0
 /**
  * Convert model to string
  *
  * @return string
  */
 public function __toString()
 {
     return sprintf('[%s]', \System\Loader::get_model_from_class(get_class($this)));
 }
Esempio n. 4
0
 /** Get string constant for common attributes
  * @param string $model
  * @param string $attr
  * @return string
  */
 public function get_common_attr_trans_name($model, $attr)
 {
     $key = 'attr-' . $attr;
     $model = \System\Loader::get_model_from_class($model);
     $full = 'model-' . $model . '-' . $key;
     return $this->has_msg($full) ? $full : $key;
 }
Esempio n. 5
0
 /** Convert model to string
  * @return string
  */
 public function __toString()
 {
     return sprintf('[%s#%s]', \System\Loader::get_model_from_class(get_class($this)), $this->is_new() ? 'new' : $this->id);
 }
Esempio n. 6
0
 private static function seed_data($data_set_name, $data_set_models)
 {
     \System\Init::full();
     foreach ($data_set_models as $model => $data_set) {
         $extra = array("model" => $model);
         \Helper\Cli::do_over($data_set, function ($key, $tdata, $extra) {
             $model = $extra['model'];
             $obj = null;
             $idc = $model::get_id_col($model);
             if (isset($tdata[$col = 'id']) || isset($tdata[$col = $idc])) {
                 $tdata[$idc] = $tdata[$col];
                 $obj = $model::find($tdata[$col]);
             }
             if ($obj) {
                 $obj->update_attrs($tdata);
             } else {
                 $obj = new $model($tdata);
                 $obj->is_new_object = true;
             }
             $obj->save();
             foreach ($tdata as $attr => $val) {
                 if (is_array($val) && $model::is_rel($attr)) {
                     if ($model::get_attr_type($attr) == $model::REL_HAS_MANY) {
                         $def = $model::get_attr($attr);
                         if (any($def['is_bilinear']) && any($def['is_master'])) {
                             unset($obj->{$attr});
                             $obj->{$attr} = $val;
                         }
                     }
                 }
             }
         }, $data_set_name . ': ' . \System\Loader::get_model_from_class($model), $extra);
     }
 }