Exemple #1
0
 public function run()
 {
     if (fnmatch('*cli*', php_sapi_name())) {
         $dir = Config::get('dir.schedules', APPLICATION_PATH . DS . 'schedules');
         if (is_dir($dir)) {
             Timer::start();
             Cli::show("Start of execution", 'COMMENT');
             $files = glob($dir . DS . '*.php');
             foreach ($files as $file) {
                 require_once $file;
                 $object = str_replace('.php', '', Arrays::last(explode(DS, $file)));
                 $class = 'Thin\\' . ucfirst(Inflector::camelize($object . '_schedule'));
                 $instance = lib('app')->make($class);
                 $methods = get_class_methods($instance);
                 Cli::show("Start schedule '{$object}'", 'COMMENT');
                 foreach ($methods as $method) {
                     $when = $this->getWhen($instance, $method);
                     $isDue = $this->isDue($object, $method, $when);
                     if (true === $isDue) {
                         Cli::show("Execution of {$object}->{$method}", 'INFO');
                         $instance->{$method}();
                     } else {
                         Cli::show("No need to execute {$object}->{$method}", 'QUESTION');
                     }
                 }
             }
             Cli::show("Time of execution [" . Timer::get() . " s.]", 'SUCCESS');
             Cli::show("end of execution", 'COMMENT');
         }
     }
 }
Exemple #2
0
 public function _related()
 {
     $fields = array_keys($this->_data);
     $obj = $this;
     foreach ($fields as $field) {
         if (fnmatch('*_id', $field)) {
             if (is_string($field)) {
                 $value = $this->{$field};
                 if (!is_callable($value)) {
                     $fk = str_replace('_id', '', $field);
                     $ns = $this->_db->db;
                     $cb = function ($object = false) use($value, $fk, $ns) {
                         $db = Db::instance($ns, $fk);
                         return $db->find($value, $object);
                     };
                     $this->_event($fk, $cb);
                     $setter = lcfirst(Inflector::camelize("link_{$fk}"));
                     $cb = function (Model $fkObject) use($obj, $field, $fk) {
                         $obj->{$field} = $fkObject->id;
                         $newCb = function () use($fkObject) {
                             return $fkObject;
                         };
                         $obj->_event($fk, $newCb);
                         return $obj;
                     };
                     $this->_event($setter, $cb);
                 }
             }
         }
     }
     return $this;
 }
Exemple #3
0
 public function __construct($type)
 {
     $this->type = $type;
     $dbFile = STORAGE_PATH . DS . Inflector::camelize('value_' . $type) . '.store';
     $this->db = new SQLite3($dbFile);
     $q = "SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'datas'";
     $res = $this->db->query($q);
     if (false === $res->fetchArray()) {
         $this->db->exec('CREATE TABLE datas (key, value, expiration)');
     }
     $this->clean();
 }
Exemple #4
0
 public function __construct(Database $model)
 {
     $this->model = $model->inCache(false);
     $fileConfig = APPLICATION_PATH . DS . 'models' . DS . 'CrudNosql' . DS . ucfirst(Inflector::camelize($model->db)) . DS . ucfirst(Inflector::camelize($model->table)) . '.php';
     if (File::exists($fileConfig)) {
         $this->config = (include $fileConfig);
     } else {
         $this->config = [];
     }
     if (!empty($this->config)) {
         $this->prepareFields();
     }
     if (get_magic_quotes_gpc()) {
         $_REQUEST = $this->stripslashes($_REQUEST);
     }
 }
Exemple #5
0
 public function __construct($type)
 {
     $this->type = $type;
     $this->fields = Arrays::exists($type, Data::$_fields) ? Data::$_fields[$type] : Data::noConfigFields($type);
     $this->settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : Data::defaultConfig($type);
     $this->session = session(Inflector::camelize('store_' . $type));
     $data = $this->session->getData();
     if (empty($data)) {
         $data = array();
         $this->session->setData($data);
     }
     $dbFile = STORAGE_PATH . DS . Inflector::camelize('store_' . $type) . '.store';
     $this->db = new SQLite3($dbFile);
     $q = "SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'datas'";
     $res = $this->db->query($q);
     if (false === $res->fetchArray()) {
         $this->db->exec('CREATE TABLE datas (id VARCHAR PRIMARY KEY, datecreate, datemodify, value)');
     }
 }
Exemple #6
0
 /**
  * Magic method for handling API methods.
  *
  * @since   PHP 5.3
  * @param   string  $method
  * @param   array   $args
  * @return  array
  */
 public static function __callStatic($method, $args)
 {
     // if production mode...
     if (Config::get('paypal.production_mode') === true) {
         // use production credentials
         $credentials = Config::get('paypal.production');
         // use production endpoint
         $endpoint = 'https://api-3t.paypal.com/nvp';
     } else {
         // use sandbox credentials
         $credentials = Config::get('paypal.sandbox');
         // use sandbox endpoint
         $endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
     }
     // build credentials
     $params = array('VERSION' => '74.0', 'USER' => $credentials['username'], 'PWD' => $credentials['password'], 'SIGNATURE' => $credentials['signature'], 'METHOD' => Inflector::camelize($method));
     // build post data
     $fields = http_build_query($params + $args[0]);
     // curl request
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $endpoint);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
     $response = curl_exec($ch);
     // if errors...
     if (curl_errno($ch)) {
         #$errors = curl_error($ch);
         curl_close($ch);
         // return false
         return false;
     } else {
         curl_close($ch);
         // return array
         parse_str($response, $result);
         return $result;
     }
 }
Exemple #7
0
 public function __construct($type, $entity = 'db')
 {
     $this->type = $type;
     $this->entity = $entity;
     $this->fields = Arrays::exists($type, Data::$_fields) ? Data::$_fields[$type] : Data::noConfigFields($type);
     $this->settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : Data::defaultConfig($type);
     $this->session = session(Inflector::camelize('storeSQL_' . $type));
     $data = $this->session->getData();
     if (empty($data)) {
         $data = array();
         $this->session->setData($data);
     }
     $this->db = $this->connect();
     if (false === $this->checkTable()) {
         $q = 'CREATE TABLE IF NOT EXISTS `datas_' . $this->type . '` (
               `id` varchar(9) NOT NULL,
               `datecreate` int(11) unsigned NOT NULL,
               `datemodify` int(11) unsigned NOT NULL,
               `value` longtext NOT NULL
             ) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
         $this->_query($q);
     }
 }
Exemple #8
0
 public static function make($db, $what)
 {
     if (is_string($what)) {
         $file = APPLICATION_PATH . DS . 'models' . DS . 'Bigdata' . DS . 'views' . DS . $db->db . DS . ucfirst(I::lower($db->table)) . DS . I::camelize($what) . '.php';
         if (files()->exists($file)) {
             require_once $file;
             $class = '\\Thin\\' . ucfirst(I::lower($db->db)) . ucfirst(I::lower($db->table)) . 'View';
             return $class::make($db);
         } else {
             return $db;
         }
     } elseif (A::is($what)) {
         $nameview = 'view_' . $db->db . '_' . $db->table . '_' . sha1(serialize($what));
         $ageDb = $db->getage();
         $viewDb = Db::instance($db->db, $nameview);
         $ageView = $db->getage();
         $exists = strlen($db->cache()->get('dbRedis.views.' . $nameview)) ? true : false;
         if ($ageView < $ageDb || !$exists) {
             $viewDb->getCollection()->remove();
             foreach ($what as $wh) {
                 $op = 'AND';
                 if (count($wh) == 4) {
                     $op = $wh[3];
                     unset($wh[3]);
                 }
                 $db = $db->where($wh);
             }
             $res = $db->exec();
             foreach ($res as $row) {
                 $viewDb->saveView($row);
             }
             $db->cache()->set('dbRedis.views.' . $nameview, 1);
         }
         return $viewDb;
     }
 }
Exemple #9
0
 function raw($db, $table)
 {
     if ($db != SITE_NAME) {
         $fn = Inflector::camelize($db . '_' . $table);
     } else {
         $fn = ucfirst(strtolower($table));
     }
     $code = 'namespace Thin; function ' . $fn . '() {return \\Thin\\Raw::' . $fn . '();}';
     if (!function_exists('\\Thin\\' . $fn)) {
         eval($code);
     } else {
         throw new Exception('The function ' . $fn . ' ever exists.');
     }
 }
Exemple #10
0
 public static function __callStatic($fn, $args)
 {
     $method = Inflector::uncamelize($fn);
     $tab = explode('_', $method);
     $table = array_shift($tab);
     $function = implode('_', $tab);
     $function = lcfirst(Inflector::camelize($function));
     $instance = self::instance(SITE_NAME, $table);
     return call_user_func_array([$instance, $function], $args);
 }
Exemple #11
0
 public static function generate($model, $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'Crud' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = model($model);
         $crud = new Crud($db);
         File::delete($file);
         $tplModel = fgc(__DIR__ . DS . 'Model.tpl');
         $tplField = fgc(__DIR__ . DS . 'Field.tpl');
         $fields = $crud->fields();
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(array('##singular##', '##plural##', '##default_order##'), array($singular, $plural, $default_order), $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(array('##field##', '##label##'), array($field, $label), $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Exemple #12
0
 public function __call($func, $args)
 {
     if (substr($func, 0, strlen('get')) == 'get') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('get'))));
         $field = Inflector::lower($uncamelizeMethod);
         $method = lcfirst(Inflector::camelize('get_' . $field . '_attribute'));
         $methods = get_class_methods($this);
         if (in_array($method, $methods)) {
             return $this->{$method}();
         }
         $default = count($args) == 1 ? current($args) : null;
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return $res;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 $db = Db::instance($this->_db->db, $field);
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 return $db->find($resFk, $object);
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $object = count($args) == 1 ? $args[0] : false;
                     if (!is_bool($object)) {
                         $object = false;
                     }
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->get();
                         $ids = [];
                         if ($pivots->count() > 0) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     array_push($ids, $id);
                                 }
                             }
                             if (!empty($ids)) {
                                 return $db->where(['id', 'IN', implode(',', $ids)])->get(null, $object);
                             } else {
                                 return [];
                             }
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         return $db->where([$idField, '=', $this->_data['id']])->get(null, $object);
                     }
                 } else {
                     return $default;
                 }
             }
         }
     } elseif (substr($func, 0, strlen('has')) == 'has' && strlen($func) > strlen('has')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('has'))));
         $field = Inflector::lower($uncamelizeMethod);
         $res = isAke($this->_data, $field, false);
         if (false !== $res) {
             return true;
         } else {
             $resFk = isAke($this->_data, $field . '_id', false);
             if (false !== $resFk) {
                 return true;
             } else {
                 if ($field[strlen($field) - 1] == 's' && isset($this->_data['id']) && $field[0] != '_') {
                     $db = Db::instance($this->_db->db, substr($field, 0, -1));
                     $hasPivot = $this->hasPivot($db);
                     if (true === $hasPivot) {
                         $model = $db->model();
                         $pivots = $this->pivots($model)->get();
                         $ids = [];
                         if ($pivots->count() > 0) {
                             foreach ($pivots as $pivot) {
                                 $id = isAke($pivot, substr($field, 0, -1) . '_id', false);
                                 if (false !== $id) {
                                     return true;
                                 }
                             }
                             return false;
                         }
                     } else {
                         $idField = $this->_db->table . '_id';
                         $count = $db->where([$idField, '=', $this->_data['id']])->count();
                         return $count > 0 ? true : false;
                     }
                 }
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('belongsTo')) == 'belongsTo' && strlen($func) > strlen('belongsTo')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         $fk = current($args);
         if (is_object($fk)) {
             $val = isAke($this->_data, $field . '_id', false);
             $fkId = isset($fk->id) ? $fk->id : false;
             if ($val && $fkId) {
                 return intval($val) == intval($fkId);
             }
         }
         return false;
     } elseif (substr($func, 0, strlen('belongsToMany')) == 'belongsToMany' && strlen($func) > strlen('belongsToMany')) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (is_object($fk)) {
             return $this->belongsToMany($field);
         }
         return false;
     } elseif (substr($func, 0, strlen('set')) == 'set') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, strlen('set'))));
         $field = Inflector::lower($uncamelizeMethod);
         if (!empty($args)) {
             $val = current($args);
         } else {
             $val = null;
         }
         if (fnmatch('*_id', $field)) {
             if (is_numeric($val)) {
                 $val = (int) $val;
             } elseif (is_object($val)) {
                 $val = (int) $val->id;
                 $this->_data[str_replace('_id', '', $field)] = $val->toArray();
             }
         } else {
             if (is_object($val)) {
                 if ($val instanceof \Thin\TimeLib) {
                     $val = $val->timestamp;
                 } else {
                     $this->_data[$field . '_id'] = $val->id;
                     $val = $val->toArray();
                 }
             }
         }
         $method = lcfirst(Inflector::camelize('set_' . $field . '_attribute'));
         $methods = get_class_methods($this);
         if (in_array($method, $methods)) {
             $val = $this->{$method}($val);
         }
         $this->_data[$field] = $val;
         $autosave = isAke($this->_data, 'autosave', false);
         return !$autosave ? $this : $this->save();
     } else {
         $cb = isAke($this->_events, $func, false);
         if (false !== $cb) {
             if ($cb instanceof Closure) {
                 return call_user_func_array($cb, $args);
             }
         } else {
             if ($func[strlen($func) - 1] == 's' && isset($this->_data['id']) && $func[0] != '_') {
                 $db = Db::instance($this->_db->db, substr($func, 0, -1));
                 $object = count($args) == 1 ? $args[0] : false;
                 if (!is_bool($object)) {
                     $object = false;
                 }
                 $hasPivot = $this->hasPivot($db);
                 if (true === $hasPivot) {
                     $model = $db->model();
                     $pivots = $this->pivots($model)->get();
                     $ids = [];
                     if ($pivots->count() > 0) {
                         foreach ($pivots as $pivot) {
                             $id = isAke($pivot, substr($func, 0, -1) . '_id', false);
                             if (false !== $id) {
                                 array_push($ids, $id);
                             }
                         }
                         if (!empty($ids)) {
                             return $db->where(['id', 'IN', implode(',', $ids)])->get(null, $object);
                         } else {
                             return [];
                         }
                     }
                 } else {
                     $idField = $this->_db->table . '_id';
                     return $object ? $db->where([$idField, '=', $this->_data['id']])->models()->toArray() : $db->where([$idField, '=', $this->_data['id']])->cursor()->toArray();
                 }
             } else {
                 if (!empty($args)) {
                     $object = count($args) == 1 ? $args[0] : false;
                     $db = Db::instance($this->_db->db, $func);
                     $field = $func . '_id';
                     if (is_bool($object) && isset($this->_data[$field])) {
                         return $db->find($value, $object);
                     } elseif (is_object($object)) {
                         $this->{$field} = (int) $object->id;
                         return $this;
                     }
                 }
                 $auth = ['checkIndices', '_hooks', 'rel', 'boot'];
                 if (in_array($func, $auth)) {
                     return true;
                 } else {
                     $scopes = $this->_db->store->get('scopes', []);
                     $scope = Inflector::uncamelize($func);
                     $closure = isAke($scopes, $scope, false);
                     if ($closure) {
                         if (is_callable($closure)) {
                             $model = $this;
                             return call_user_func_array($closure, [$model]);
                         }
                     }
                 }
                 throw new Exception("{$func} is not a model function of {$this->_db}.");
             }
         }
     }
 }
Exemple #13
0
 public function __destruct()
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db)) . DS . ucfirst(Inflector::camelize($this->model->table)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db)))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($this->model->db)));
     }
     if (!File::exists($file) || $this->replace) {
         File::delete($file);
         $tplModel = File::read(__DIR__ . DS . 'Model.tpl');
         $tplField = File::read(__DIR__ . DS . 'Field.tpl');
         $uniques = isAke($this->indices, 'unique', []);
         $foreigns = isAke($this->indices, 'foreign', []);
         $softDelete = true === $this->softDelete ? 'true' : 'false';
         $uString = '[';
         if (count($uniques)) {
             foreach ($uniques as $unique) {
                 $uString .= "'" . $unique . "'" . ',';
             }
             $uString = substr($uString, 0, -1);
         }
         $uString .= ']';
         $fString = '[';
         if (count($foreigns)) {
             foreach ($foreigns as $foreign) {
                 $fString .= "'" . $foreign . "'" . ',';
             }
             $fString = substr($fString, 0, -1);
         }
         $fString .= ']';
         $before_create = $this->getHook('before_create');
         $after_create = $this->getHook('after_create');
         $before_update = $this->getHook('before_update');
         $after_update = $this->getHook('after_update');
         $before_read = $this->getHook('before_read');
         $after_read = $this->getHook('after_read');
         $before_delete = $this->getHook('before_delete');
         $after_delete = $this->getHook('after_delete');
         $before_list = $this->getHook('before_list');
         $after_list = $this->getHook('after_list');
         $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##soft_delete##', '##foreigns##', '##uniques##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$this->singular, $this->plural, $this->order, $softDelete, $fString, $uString, $before_create, $after_create, $before_update, $after_update, $before_read, $after_read, $before_delete, $after_delete, $before_list, $after_list], $tplModel);
         $fieldsSection = '';
         foreach ($this->fields as $field => $infos) {
             if ($field != 'id') {
                 $label = $this->getSetting($field, 'label', ucfirst($field));
                 $form_type = $this->getSetting($field, 'form_type', 'text');
                 $helper = $this->getSetting($field, 'helper', 'false');
                 $required = $this->getSetting($field, 'required', 'true');
                 $form_plus = $this->getSetting($field, 'form_plus', 'false');
                 $length = $this->getSetting($field, 'length', 'false');
                 $is_listable = $this->getSetting($field, 'is_listable', 'true');
                 $is_exportable = $this->getSetting($field, 'is_exportable', 'true');
                 $is_searchable = $this->getSetting($field, 'is_searchable', 'true');
                 $is_sortable = $this->getSetting($field, 'is_sortable', 'true');
                 $is_readable = $this->getSetting($field, 'is_readable', 'true');
                 $is_creatable = $this->getSetting($field, 'is_creatable', 'true');
                 $is_updatable = $this->getSetting($field, 'is_updatable', 'true');
                 $is_deletable = $this->getSetting($field, 'is_deletable', 'true');
                 $content_view = $this->getSetting($field, 'content_view', 'false');
                 $content_list = $this->getSetting($field, 'content_list', 'false');
                 $content_search = $this->getSetting($field, 'content_search', 'false');
                 $content_create = $this->getSetting($field, 'content_create', 'false');
                 $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, $form_type, $helper, $required, $form_plus, $length, $is_listable, $is_exportable, $is_searchable, $is_sortable, $is_readable, $is_creatable, $is_updatable, $is_deletable, $content_view, $content_list, $content_search, $content_create, $label], $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Exemple #14
0
 public function __call($fn, $args)
 {
     $method = substr($fn, 0, strlen('findLastBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findLastBy'))));
     if (strlen($fn) > strlen('findLastBy')) {
         if ('findLastBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->where([$object, '=', Arrays::first($args)])->last($obj);
         }
     }
     $method = substr($fn, 0, strlen('findFirstBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findFirstBy'))));
     if (strlen($fn) > strlen('findFirstBy')) {
         if ('findFirstBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : true;
             if (!is_bool($obj)) {
                 $obj = true;
             }
             return $this->findFirstBy($object, Arrays::first($args), $obj);
         }
     }
     $method = substr($fn, 0, strlen('findOneBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('findOneBy'))));
     if (strlen($fn) > strlen('findOneBy')) {
         if ('findOneBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : false;
             if (!is_bool($obj)) {
                 $obj = false;
             }
             return $this->findOneBy($object, Arrays::first($args), $obj);
         }
     }
     $method = substr($fn, 0, strlen('orderBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('orderBy'))));
     if (strlen($fn) > strlen('orderBy')) {
         if ('orderBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields) && 'id' != $object) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = !empty($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('groupBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields)) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             return $this->groupBy($object);
         }
     }
     $method = substr($fn, 0, strlen('where'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('where'))));
     if (strlen($fn) > strlen('where')) {
         if ('where' == $method) {
             return $this->where([$object, '=', Arrays::first($args)]);
         }
     }
     $method = substr($fn, 0, strlen('sortBy'));
     $object = Inflector::uncamelize(lcfirst(substr($fn, strlen('sortBy'))));
     if (strlen($fn) > strlen('sortBy')) {
         if ('sortBy' == $method) {
             $fields = $this->fieldsRow();
             if (!Arrays::in($object, $fields) && 'id' != $object) {
                 $object = Arrays::in($object . '_id', $fields) ? $object . '_id' : $object;
             }
             $direction = !empty($args) ? Arrays::first($args) : 'ASC';
             return $this->order($object, $direction);
         } elseif ('findBy' == $method) {
             $obj = count($args) == 2 ? $args[1] : false;
             if (!is_bool($obj)) {
                 $obj = false;
             }
             return $this->findBy($object, Arrays::first($args), false, $obj);
         }
     }
     $model = $this->model();
     $scope = lcfirst(Inflector::camelize('scope_' . Inflector::uncamelize($fn)));
     if (method_exists($model, $scope)) {
         return call_user_func_array([$model, $scope], $args);
     }
     throw new Exception("Method '{$fn}' is unknown.");
 }
Exemple #15
0
 private function related($obj)
 {
     $settings = isAke(self::$config, "{$this->database}.{$this->table}");
     $relations = isAke($settings, 'relations');
     $params = $this->args;
     if (count($relations)) {
         foreach ($relations as $relation) {
             $field = $relation . '_id';
             if (is_string($field)) {
                 $value = $obj->{$field};
                 if (!is_callable($value)) {
                     $fk = $tableFk = $relation;
                     $fks = $fk . 's';
                     $cb = function ($object = true) use($value, $tableFk, $params) {
                         list($database, $table, $host, $username, $password) = $params;
                         $db = Database::instance($database, $tableFk, $host, $username, $password);
                         if ($db) {
                             return $db->find($value, $object);
                         }
                         return null;
                     };
                     $obj->event($fk, $cb);
                     $cb = function ($object = true) use($value, $tableFk, $params) {
                         list($database, $table, $host, $username, $password) = $params;
                         $db = Database::instance($database, $tableFk, $host, $username, $password);
                         if ($db) {
                             return $db->where($db->pk() . " = '" . addslashes($value) . "'")->exec($object);
                         }
                         return null;
                     };
                     $obj->event($fks, $cb);
                     $setter = lcfirst(Inflector::camelize("link_{$fk}"));
                     $cb = function (Container $fkObject) use($obj, $field, $fk) {
                         $obj->{$field} = $fkObject->id();
                         $newCb = function () use($fkObject) {
                             return $fkObject;
                         };
                         $obj->event($fk, $newCb);
                         return $obj;
                     };
                     $obj->event($setter, $cb);
                 }
             }
         }
     }
     return $obj;
 }
Exemple #16
0
 protected function _camelize($name)
 {
     return Inflector::camelize($name);
 }
Exemple #17
0
 public static function row($type, array $data, $extends = array())
 {
     if (Arrays::isAssoc($data)) {
         $obj = o(sha1(serialize($data)));
         if (count($extends)) {
             foreach ($extends as $name => $instance) {
                 $closure = function ($object) use($name, $instance) {
                     $idx = $object->is_thin_object;
                     $objects = Utils::get('thinObjects');
                     return $instance->{$name}($objects[$idx]);
                 };
                 $obj->_closures[$name] = $closure;
             }
         }
         $settings = Arrays::exists($type, static::$_settings) ? static::$_settings[$type] : array();
         if (count($settings)) {
             $db = Arrays::exists('db', $settings) ? $settings['db'] : null;
             if (!empty($db)) {
                 $methods = array('save', 'delete');
                 foreach ($methods as $method) {
                     if (!Arrays::exists($method, $obj->_closures)) {
                         $closure = function () use($type, $method, $obj, $db) {
                             $name = $method . Inflector::camelize($db);
                             return $obj->{$name}($type);
                         };
                         $obj->_closures[$method] = $closure;
                     }
                 }
             }
         }
         return $obj->populate($data);
     }
     return null;
 }
Exemple #18
0
 public static function generate($database, $model, $fields = [], $overwrite = false)
 {
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw')) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw');
     }
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudRaw' . DS . ucfirst(Inflector::camelize($database)));
     }
     if (!File::exists($file) || $overwrite) {
         $db = Db::instance($database, $model);
         $crud = Crud::instance($db);
         File::delete($file);
         $tplModel = File::read(__DIR__ . DS . 'Model.tpl');
         $tplField = File::read(__DIR__ . DS . 'Field.tpl');
         $fields = empty($fields) ? $crud->fields() : $fields;
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##foreigns##', '##uniques##', '##soft_delete##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$singular, $plural, $default_order, '[]', '[]', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false'], $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, 'text', 'false', 'true', 'false', 'false', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', $label], $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Exemple #19
0
 private function related(Container $obj)
 {
     $fields = array_keys($obj->assoc());
     foreach ($fields as $field) {
         if (endsWith($field, '_id')) {
             if (is_string($field)) {
                 $value = $obj->{$field};
                 if (!is_callable($value)) {
                     $fk = repl('_id', '', $field);
                     $ns = $this->ns;
                     $cb = function () use($value, $fk, $ns) {
                         $db = jdb($ns, $fk);
                         return $db->find($value);
                     };
                     $obj->event($fk, $cb);
                     $setter = lcfirst(Inflector::camelize("link_{$fk}"));
                     $cb = function (Container $fkObject) use($obj, $field, $fk) {
                         $obj->{$field} = $fkObject->getId();
                         $newCb = function () use($fkObject) {
                             return $fkObject;
                         };
                         $obj->event($fk, $newCb);
                         return $obj;
                     };
                     $obj->event($setter, $cb);
                 }
             }
         }
     }
     return $obj;
 }
Exemple #20
0
 public function sql($sql)
 {
     $infos = $this->parseQuery($sql);
     $method = isAke($infos, 'method', null);
     if (!empty($method)) {
         $sqlMethod = lcfirst(Inflector::camelize('sql_' . $method));
         if (method_exists($this, $sqlMethod)) {
             return $this->{$sqlMethod}($infos);
         }
     }
     return $this;
 }
Exemple #21
0
 public function render()
 {
     if (!count($this->_items)) {
         return '<div class="span4"><div class="alert alert-info"><button type="button" class="close" data-dismiss="alert">×</button>' . $this->_config['noResultMessage'] . '</div></div>';
     }
     $pagination = $this->_config['pagination'];
     $fields = $this->_config['fields'];
     $addable = $this->_config['addable'];
     $viewable = $this->_config['viewable'];
     $editable = $this->_config['editable'];
     $deletable = $this->_config['deletable'];
     $duplicable = $this->_config['duplicable'];
     if (\Thin\Arrays::exists('export', $this->_config)) {
         $export = $this->_config['export'];
         if (count($export)) {
             $this->_export = $export;
         }
     }
     $order = !strlen($this->_request->getCrudOrder()) ? $this->_config['defaultOrder'] : $this->_request->getCrudOrder();
     $orderDirection = !strlen($this->_request->getCrudOrderDirection()) ? $this->_config['defaultOrderDirection'] : $this->_request->getCrudOrderDirection();
     $sorted = true === $this->_config['order'] ? 'tablesorter' : '';
     $html = '<table class="table table-striped ' . $sorted . ' table-bordered table-condensed">' . NL;
     $html .= '<thead>' . NL;
     $html .= '<tr>' . NL;
     foreach ($fields as $field => $infosField) {
         if (true === $infosField['onList']) {
             if (true !== $infosField['sortable']) {
                 $html .= '<th class="no-sorter">' . \Thin\Html\Helper::display($infosField['label']) . '</th>' . NL;
             } else {
                 if ($field == $order) {
                     $directionJs = 'ASC' == $orderDirection ? 'DESC' : 'ASC';
                     $js = 'orderGoPage(\'' . $field . '\', \'' . $directionJs . '\');';
                     $html .= '<th>
                             <div onclick="' . $js . '" class="text-left field-sorting ' . \Thin\Inflector::lower($orderDirection) . '" rel="' . $field . '">
                             ' . \Thin\Html\Helper::display($infosField['label']) . '
                             </div>
                         </th>';
                 } else {
                     $js = 'orderGoPage(\'' . $field . '\', \'ASC\');';
                     $html .= '<th>
                             <div onclick="' . $js . '" class="text-left field-sorting" rel="' . $field . '">
                             ' . \Thin\Html\Helper::display($infosField['label']) . '
                             </div>
                         </th>';
                 }
             }
         }
     }
     $html .= '<th class="no-sorter">Actions</th>' . NL;
     $html .= '</tr>' . NL;
     $html .= '</thead>' . NL;
     $html .= '<tbody>' . NL;
     foreach ($this->_items as $item) {
         $html .= '<tr>' . NL;
         foreach ($fields as $field => $infosField) {
             if (true === $infosField['onList']) {
                 $content = $infosField['content'];
                 $options = \Thin\Arrays::exists('options', $infosField) ? $infosField['options'] : array();
                 if (empty($options)) {
                     $options = array();
                 }
                 if (!in_array('nosql', $options)) {
                     $getter = 'get' . \Thin\Inflector::camelize($field);
                     $value = $item->{$getter}();
                 } else {
                     $value = $content;
                 }
                 if (strstr($content, '##self##') || strstr($content, '##em##') || strstr($content, '##field##') || strstr($content, '##id##')) {
                     $content = repl(array('##self##', '##em##', '##field##', '##id##'), array($value, $this->_em, $field, $item->getId()), $content);
                     $value = \Thin\Crud::internalFunction($content);
                 }
                 if (empty($value)) {
                     $value = '&nbsp;';
                 }
                 $html .= '<td>' . \Thin\Html\Helper::display($value) . '</td>' . NL;
             }
         }
         $actions = '';
         if (true === $viewable) {
             $actions .= '<a href="' . \Thin\Crud::getRoute('view', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="afficher" class="icon-file"></i></a>&nbsp;&nbsp;&nbsp;';
         }
         if (true === $editable) {
             $actions .= '<a href="' . \Thin\Crud::getRoute('edit', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="éditer" class="icon-edit"></i></a>&nbsp;&nbsp;&nbsp;';
         }
         if (true === $duplicable) {
             $actions .= '<a href="' . \Thin\Crud::getRoute('duplicate', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '"><i title="dupliquer" class="icon-plus"></i></a>&nbsp;&nbsp;&nbsp;';
         }
         if (true === $deletable) {
             $actions .= '<a href="#" onclick="if (confirm(\'Confirmez-vous la suppression de cet élément ?\')) document.location.href = \'' . \Thin\Crud::getRoute('delete', array('id' => $item->getId(), 'entity' => $item->_getEntity(), 'table' => $item->_getTable())) . '\';"><i title="supprimer" class="icon-trash"></i></a>&nbsp;&nbsp;&nbsp;';
         }
         $html .= '<td class="col_plus">' . $actions . '</td>' . NL;
         $html .= '</tr>' . NL;
     }
     $html .= '</tbody>' . NL;
     $html .= '</table>' . NL;
     return $html;
 }