コード例 #1
0
ファイル: Object.php プロジェクト: schpill/thin
 public function __call($func, $argv)
 {
     $key = sha1('orm' . $this->_token);
     $orm = isAke($this->values, $key, false);
     $key = sha1('model' . $this->_token);
     $dbjson = isAke($this->values, $key, false);
     if (substr($func, 0, 4) == 'link' && false !== $orm) {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 4)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             $var = setter($var . '_id');
             $this->{$var}($value->id);
             return $this;
         }
     } elseif (substr($func, 0, 3) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
                 if (Arrays::exists('relationships', $settings)) {
                     if (Arrays::exists($var, $settings['relationships'])) {
                         return Data::getById($var, $this->{$var});
                     }
                 }
             }
             if (Arrays::is($this->{$var}) && count($argv) == 1) {
                 $o = new self();
                 $getter = getter(Arrays::first($argv));
                 $o->populate($this->{$var});
                 return $o->{$getter}();
             }
             if ($this->{$var} instanceof Closure) {
                 if (is_callable($this->{$var}) && count($argv)) {
                     return call_user_func_array($this->{$var}, $argv);
                 }
             }
             return count($argv) && is_null($this->{$var}) ? Arrays::first($argv) : $this->{$var};
         } else {
             if (isset($this->db_instance)) {
                 return $this->db_instance->getValue($this, $var);
             }
             if (isset($this->thin_type)) {
                 $type = $this->thin_type;
                 Data::getModel($type);
                 $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : [];
                 $relationships = Arrays::exists('relationships', $settings) ? $settings['relationships'] : [];
                 if (Arrays::exists($var, $relationships) && 's' == $var[strlen($var) - 1]) {
                     if (Arrays::exists($var, $relationships)) {
                         $res = dm(substr($var, 0, -1))->where("{$type} = " . $this->id)->get();
                         $collection = [];
                         if (count($res)) {
                             foreach ($res as $obj) {
                                 array_push($collection, $obj);
                             }
                         }
                         return $collection;
                     }
                 } elseif (Arrays::exists('defaultValues', $settings)) {
                     if (Arrays::is($settings['defaultValues'])) {
                         if (Arrays::exists($this->{$var}, $settings['defaultValues'])) {
                             return $settings['defaultValues'][$this->{$var}];
                         }
                     }
                 }
             }
             if (count($argv) == 1) {
                 return Arrays::first($argv);
             }
             return null;
         }
     } elseif (substr($func, 0, 3) == 'has') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (isset($this->{$var})) {
             return !empty($this->{$var});
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->hasValue($this, $var);
         }
     } elseif (substr($func, 0, 3) == 'set') {
         $value = Arrays::first($argv);
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (!empty($var)) {
             if (isset($this->thin_type)) {
                 Data::getModel($this->thin_type);
                 $fields = Arrays::exists($this->thin_type, Data::$_fields) ? Data::$_fields[$this->thin_type] : [];
                 if (!Arrays::exists($var, $fields)) {
                     throw new Exception($var . ' is not defined in the model => ' . $this->thin_type);
                 } else {
                     $settingsField = $fields[$var];
                     if (Arrays::exists('checkValue', $settingsField)) {
                         $functionCheck = $settingsField['checkValue'];
                         $value = $functionCheck($value);
                     }
                     if (is_object($value)) {
                         if (isset($value->thin_type)) {
                             if ($value->thin_type == $var) {
                                 $value = $value->id;
                             }
                         }
                     }
                 }
             }
             $this->{$var} = $value;
             if (!Arrays::in($var, $this->_fields)) {
                 $this->_fields[] = $var;
             }
             if (isset($this->is_thin_object)) {
                 $name = $this->is_thin_object;
                 $objects = Utils::get('thinObjects');
                 $this->values = null;
                 $objects[$name] = $this;
                 Utils::set('thinObjects', $objects);
             }
             if (isset($this->is_app)) {
                 if (true === $this->is_app) {
                     Utils::set('ThinAppContainer', $this);
                 }
             }
         } elseif (isset($this->db_instance)) {
             return $this->db_instance->setValue($this, $var, $value);
         }
         return $this;
     } elseif (substr($func, 0, 3) == 'add') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (!isset($this->{$var})) {
             $this->{$var} = [];
         }
         if (!Arrays::is($this->{$var})) {
             $this->{$var} = [];
         }
         array_push($this->{$var}, $value);
         return $this;
     } elseif (substr($func, 0, 6) == 'remove') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6)));
         $var = Inflector::lower($uncamelizeMethod) . 's';
         $value = Arrays::first($argv);
         if (isset($this->{$var})) {
             if (Arrays::is($this->{$var})) {
                 if (count($this->{$var})) {
                     $remove = false;
                     foreach ($this->{$var} as $key => $tmpValue) {
                         $comp = md5(serialize($value)) == md5(serialize($tmpValue));
                         if (true === $comp) {
                             $remove = true;
                             break;
                         }
                     }
                     if (true === $remove) {
                         unset($this->{$var}[$key]);
                     }
                 }
             }
         }
         return $this;
     }
     if (Arrays::in($func, $this->_fields)) {
         if ($this->{$func} instanceof Closure) {
             return call_user_func_array($this->{$func}, $argv);
         }
     }
     if (Arrays::exists($func, $this->_closures)) {
         if ($this->_closures[$func] instanceof Closure) {
             return call_user_func_array($this->_closures[$func], $argv);
         }
     }
     if (isset($this->_token)) {
         $id = sha1($func . $this->_token);
         if (Arrays::is($this->values)) {
             if (Arrays::exists($id, $this->values)) {
                 return call_user_func_array($this->values[$id], $argv);
             }
         }
     }
     if (true === hasEvent($func)) {
         array_push($argv, $this);
         return fire($func, $argv);
     }
     if (!is_callable($func) || substr($func, 0, 6) !== 'array_' || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get' || substr($func, 0, 3) !== 'has' || substr($func, 0, 3) !== 'add' || substr($func, 0, 6) !== 'remove') {
         $callable = strrev(repl('_', '', $func));
         if (!is_callable($callable)) {
             if (method_exists($this, $callable)) {
                 return call_user_func_array(array($this, $callable), $argv);
             }
         } else {
             return call_user_func_array($callable, $argv);
         }
         if (isset($this->thin_litedb)) {
             $closure = isAke($this->thin_litedb->closures, $func);
             if (!empty($closure) && $closure instanceof Closure) {
                 return $closure($this);
             }
         }
         if (isset($this->db_instance)) {
             return $this->db_instance->{$func}($this, $var, $value);
             call_user_func_array(array($this->db_instance, $func), array_merge(array($this), $argv));
         }
         if (false !== $orm) {
             $db = call_user_func_array($orm, []);
             $fields = array_keys($db->map['fields']);
             if (Arrays::in($func, $fields)) {
                 if (!count($argv)) {
                     return $this->{$func};
                 } else {
                     $setter = setter($func);
                     $this->{$setter}(Arrays::first($argv));
                     return $this;
                 }
             }
             $tab = str_split($func);
             $many = false;
             if (Arrays::last($tab) == 's') {
                 array_pop($tab);
                 $table = implode('', $tab);
                 $many = true;
             } else {
                 $table = $func;
             }
             $object = count($argv) == 1 ? Arrays::first($argv) : false;
             $model = model($table);
             return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object);
         }
         if (false !== $dbjson) {
             $db = $this->model()->db();
             $fields = $db->fields();
             $modelMethods = get_class_methods('Dbjson\\Model');
             if (Arrays::in($func, $fields)) {
                 if (!count($argv)) {
                     return $this->{$func};
                 } else {
                     $setter = setter($func);
                     $this->{$setter}(Arrays::first($argv));
                     return $this;
                 }
             }
             if (Arrays::in($func, $modelMethods)) {
                 return call_user_func_array([$this->model(), $func], $argv);
             }
             $tab = str_split($func);
             $many = false;
             if (Arrays::last($tab) == 's') {
                 array_pop($tab);
                 $table = implode('', $tab);
                 $many = true;
             } else {
                 $table = $func;
             }
             $object = count($argv) == 1 ? Arrays::first($argv) : true;
             $model = jdb($db->db, $table);
             return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object);
         }
         return null;
     }
     return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
 }
コード例 #2
0
ファイル: Cms.php プロジェクト: schpill/thin
 public static function makeForm($name)
 {
     $html = '';
     $db = dm('adminform');
     $db2 = dm('adminformfield');
     $res = $db->where('name = ' . $name)->get();
     if (count($res)) {
         $form = $db->first($res);
         $html .= '<form class="form-horizontal" action="' . URLSITE . static::__($form->getPage()->getUrl()) . '" id="form_' . Inflector::lower($name) . '" method="post">';
         $html .= Form::hidden('cms_form', $form->getId(), array('id' => 'cms_form'));
         $fields = $db2->where('adminform = ' . $form->getId())->order('hierarchy')->get();
         if (count($fields)) {
             foreach ($fields as $field) {
                 $true = getBool('true');
                 $fieldName = $field->getName();
                 $label = static::__($field->getLabel());
                 $type = $field->getAdminformfieldtype()->getValue();
                 $value = static::__($field->getDefault());
                 $required = $field->getRequired() == $true->getId();
                 if (empty($value)) {
                     $value = null;
                 }
                 switch ($type) {
                     case 'hidden':
                         $html .= Form::hidden($fieldName, $value, array('id' => $fieldName));
                         break;
                     case 'select':
                         $options = explode("\n", $value);
                         $tab = array();
                         foreach ($options as $row) {
                             list($key, $val) = explode('##', $row, 2);
                             $tab[$key] = $val;
                         }
                         $html .= Form::select($fieldName, $tab, null, array('id' => $fieldName, 'required' => $required), $label);
                         break;
                     case 'password':
                         $html .= Form::password($fieldName, array('id' => $fieldName, 'required' => $required), $label);
                         break;
                     default:
                         $html .= Form::$type($fieldName, $value, array('id' => $fieldName, 'required' => $required), $label);
                 }
             }
         }
         $html .= '<div class="control-group" id="control-group-submit"><label for="submit" class="control-label labelrequired">&nbsp;</label><div class="control-group" id="control-group-submit" style="position: relative; left: 20px;">' . Form::submit(static::__($form->getButton())) . '</div></div>';
     }
     return $html;
 }
コード例 #3
0
ファイル: Helper.php プロジェクト: noikiy/inovi
 function cms_objects($collection, $array = false)
 {
     $coll = array();
     $q = dm('collection');
     $res = $q->where('name = ' . $collection)->get();
     if (count($res)) {
         $row = $q->first($res);
         $q = dm('object');
         $objects = $q->where('collection = ' . $row->getId())->get();
         if (count($objects)) {
             foreach ($objects as $object) {
                 $objectLng = Cms::lng($object->getValue());
                 $ini = parse_ini_string($objectLng, true);
                 if (true === $array) {
                     array_push($coll, $ini);
                 } else {
                     $obj = new cmsObj();
                     $obj->populate($ini);
                     array_push($coll, $obj);
                 }
             }
         }
     }
     return $coll;
 }
コード例 #4
0
ファイル: Data.php プロジェクト: schpill/thin
 public static function store($type, $flat, $key = null)
 {
     $settings = Arrays::exists($type, static::$_settings) ? static::$_settings[$type] : static::defaultConfig($type);
     $hook = Arrays::exists('store', $settings) ? $settings['store'] : null;
     static::_hook($hook, func_get_args(), 'before');
     $checkTuple = Arrays::exists('checkTuple', $settings) ? $settings['checkTuple'] : null;
     $indexes = Arrays::exists('indexes', $settings) ? $settings['indexes'] : null;
     if (Arrays::exists('beforeStore', $settings)) {
         $settings['beforeDelete']($type, $flat);
     }
     static::event($type);
     $dir = static::checkDir($type);
     $object = new $type();
     $flat = static::_sanitize($flat);
     $object->populate($flat);
     $serialize = static::serialize($object);
     $edit = !is_null($key);
     $add = !$edit;
     if (true === $add) {
         $key = static::makeKey($type);
     }
     $versionFile = STORAGE_PATH . DS . 'data' . DS . $dir . DS . 'versions' . DS . $key . '_' . Timer::getMS() . '.data';
     if (true === $edit) {
         $oldFile = STORAGE_PATH . DS . 'data' . DS . $dir . DS . 'read' . DS . $key . '.data';
         $oldObject = static::getIt($type, $oldFile);
         if (count($indexes)) {
             foreach ($indexes as $indexField => $indexInfo) {
                 static::indexRemove($indexField, $indexInfo, static::getObject($oldFile));
             }
         }
         if (File::exists($oldFile)) {
             File::delete($oldFile);
         }
     }
     if (!empty($checkTuple)) {
         if (is_string($checkTuple)) {
             $db = dm($type);
             $res = $db->where($checkTuple . ' = ' . $flat[$checkTuple])->get();
         }
         if (Arrays::is($checkTuple)) {
             $query = '';
             foreach ($checkTuple as $ct) {
                 $query .= $ct . ' = ' . $flat[$ct] . ' && ';
             }
             $query = substr($query, 0, -4);
             $tabConditions = explode(' && ', $query);
             $init = true;
             foreach ($tabConditions as $cond) {
                 $db = dm($type);
                 $res = $db->where($cond)->sub();
                 if (true === $init) {
                     $init = false;
                     $results = $res;
                 } else {
                     $results = array_intersect($results, $res);
                 }
             }
             $db = dm($type);
             $res = $db->get($results);
         }
         if (count($res)) {
             $row = $db->first($res);
             static::_hook($hook, array("thin_type" => $type) + $flat, 'after');
             return $row;
         }
     }
     $file = repl(DS . 'versions' . DS, DS . 'write' . DS, $versionFile);
     File::delete($file);
     File::put($file, $serialize);
     $fields = Arrays::exists($type, static::$_fields) ? static::$_fields[$type] : static::noConfigFields($type);
     $versioning = false;
     if (count($fields)) {
         foreach ($fields as $field => $info) {
             $val = $object->{$field};
             if (empty($val)) {
                 if (!Arrays::exists('canBeNull', $info)) {
                     if (!Arrays::exists('default', $info)) {
                         static::_hook($hook, func_get_args(), 'after');
                         throw new Exception('The field ' . $field . ' cannot be null.');
                     } else {
                         $object->{$field} = $info['default'];
                     }
                 }
             } else {
                 if (Arrays::exists('sha1', $info)) {
                     if (!preg_match('/^[0-9a-f]{40}$/i', $val) || strlen($val) != 40) {
                         $object->{$field} = sha1($val);
                     }
                 } elseif (Arrays::exists('md5', $info)) {
                     if (!preg_match('/^[0-9a-f]{32}$/i', $val) || strlen($val) != 32) {
                         $object->{$field} = md5($val);
                     }
                 }
             }
         }
     }
     if (Arrays::exists('versioning', $settings)) {
         $versioning = $settings['versioning'];
     }
     if (false !== $versioning) {
         File::put($versionFile, $serialize);
     }
     if (count($indexes)) {
         $thisObject = static::getById($type, $object->getId());
         foreach ($indexes as $indexField => $indexInfo) {
             static::indexCreate($indexField, $indexInfo, $thisObject);
         }
         static::commit();
     }
     if (Arrays::exists('afterStore', $settings)) {
         if ($settings['afterStore'] instanceof \Closure) {
             $settings['afterStore']($type, $flat);
         }
     }
     static::_hook($hook, func_get_args(), 'after');
     static::emptyCache($type);
     return $object;
 }