public function val_get() { if ($this->resolved_value) { $val = $this->resolved_value; } else { $val = $this->form()->input_value($this->name); } if (gettype($val) == 'string') { $val = \System\Json::decode($val); } if (is_array($val)) { if ($val['method'] == 'url') { $val = \System\File::fetch($val['url'])->temp()->unload(); $val->temp = false; $val->method = 'keep'; } else { if ($val['method'] == 'upload') { $file = $this->form()->request->post($val['upload']); $file = \System\File::from_tmp($file['tmp_name'], $val['name']); $file->temp(); $file->temp = false; $file->method = 'keep'; $file->mime = $val['mime']; $val = $file; } else { if ($val['method'] == 'save') { $file = \System\File::from_path($val['path']); $file->read_meta(); $val = $file; } else { if ($val['method'] == 'drop') { $val = null; } } } } } $this->resolved_value = $val; return $val; }
public static function get_schema() { static::check_model(); $attrs = array(); $schema = array(); $cname = get_called_class(); $list = static::get_attr_list(); foreach ($list as $name) { if ($name == static::get_id_col()) { continue; } $attr = static::get_attr($name); $attr['name'] = $name; switch ($attr['type']) { case 'bool': $attr['type'] = 'boolean'; break; case 'varchar': $attr['type'] = 'string'; break; case 'json': $attr['type'] = 'object'; break; case self::REL_HAS_ONE: $nm = $attr['model']; $attr['type'] = 'model'; $attr['subtype'] = 'has_one'; $attr['bound_to'] = static::get_rel_bound_to($name); break; case self::REL_BELONGS_TO: $attr['type'] = 'model'; break; case self::REL_HAS_MANY: $rm = $attr['model']; if (empty($attr['is_bilinear'])) { $attr['bound_to'] = static::get_rel_bound_to($name); $attr['foreign_key'] = $rm::get_belongs_to_id($attr['bound_to']); } else { $attr['foreign_key'] = static::get_id_col(); } $attr['type'] = 'collection'; break; } // Convert attribute model bindings if (isset($attr['model'])) { $attr['model'] = \System\Loader::get_model_from_class($attr['model']); } // Convert attribute value options if (isset($attr['options'])) { if (isset($attr['options'][0]) && $attr['options'][0] == 'callback') { $opts = $attr['options']; array_shift($opts); $opts = call_user_func($opts); } else { $opts = $attr['options']; } $attr['options'] = array(); foreach ($opts as $opt_value => $opt_name) { $attr['options'][] = array('name' => $opt_name, 'value' => $opt_value); } } // Word 'default' is keyword in some browsers, so pwf-models use 'def' instead if (isset($attr['default'])) { $attr['def'] = $attr['default']; unset($attr['default']); if (in_array($attr['type'], array('image'))) { $attr['def'] = \System\Image::from_path($attr['def'])->to_object(); } else { if (in_array($attr['type'], array('file', 'sound'))) { $attr['def'] = \System\File::from_path($attr['def'])->to_object(); } } } if (is_array($attr)) { unset($attr[0]); $attrs[] = $attr; } } if (any($cname::$conversions)) { $schema['conversions'] = $cname::$conversions; } $schema['attrs'] = $attrs; return $schema; }