public function session($k, $d = null) { $session = session('front'); if (is_null($d)) { $getter = getter($k); $val = $session->{$getter}(); return $val ? $val : $d; } else { $session->erase($k); $setter = setter($k); $session->{$setter}($d); } }
public function get($key, $default, $args = []) { $translation = $default; $locale = session('web')->getLanguage(); if (!$locale) { $locale = lng(); session('web')->setLanguage($locale); } if (fnmatch('*_*', $locale)) { list($locale, $d) = explode('_', $locale, 2); } $locale = strtolower($locale); if (false !== $this->mustTranslate) { $row = $this->db->where(['key', '=', $key])->first(); if ($row) { if (!isset($row[$locale])) { $translate = $this->db->find((int) $row['id']); $setter = setter($locale); $translate->{$setter}('')->save(); } $translation = isAke($row, $locale, $default); if (!strlen($translation)) { $translation = $default; } } else { $default = preg_replace('~[\\r\\n]+~', '', $default); $translate = $this->db->firstOrCreate(['key' => $key]); $setter = setter(DEFAULT_LANGUAGE); $translate->{$setter}($default); $setter = setter($locale); $translate->{$setter}('')->save(); } } if (!empty($args)) { foreach ($args as $k => $v) { $translation = str_replace('%' . $k . '%', $v, $translation); } } return $translation; }
private function uploadSession($field) { $session = session('upload'); $upload = isAke($_FILES, $field); if (!empty($upload)) { $setter = setter($field); $session->{$setter}(fgc($upload['tmp_name'])); return true; } return false; }
public function attach($model, $attributes = []) { $m = !is_array($model) ? $model : Arrays::first($model); if (!isset($this->_data['id']) || empty($m->id)) { throw new Exception("Attach method requires a valid model."); } $mTable = $m->db()->table; $names = [$this->_db->table, $mTable]; asort($names); $pivot = Inflector::lower('pivot' . implode('', $names)); $db = Db::instance($this->_db->db, $pivot); if (is_array($model)) { foreach ($model as $mod) { $id = (int) $mod->id; $row = $mod->db()->find($id); if ($row) { $fieldAttach = $mTable . '_id'; $fieldModel = $this->_db->table . '_id'; $attach = $db->firstOrCreate([$fieldAttach => $id, $fieldModel => $this->_data['id']]); if (!empty($attributes)) { foreach ($attributes as $k => $v) { $setter = setter($k); $attach->{$setter}($v); } $attach->save(); } } } } else { $id = (int) $model->id; $row = $model->db()->find($id); if ($row) { $fieldAttach = $mTable . '_id'; $fieldModel = $this->_db->table . '_id'; $attach = $db->firstOrCreate([$fieldAttach => $id, $fieldModel => $this->_data['id']]); if (!empty($attributes)) { foreach ($attributes as $k => $v) { $setter = setter($k); $attach->{$setter}($v); } $attach->save(); } } } return $this; }
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)); }
function kv() { $args = func_get_args(); $s = session('kv'); if (count($args) == 1) { $get = getter(Arrays::first($args)); return $s->{$get}(); } elseif (count($args) == 2) { $set = setter(Arrays::first($args)); return $s->{$set}(Arrays::last($args)); } return null; }
public function searchNearByAddress($address, $query) { $collection = lib('collection'); $coords = lib('geo')->getCoords($address); $url = 'http://search.mappy.net/search/1.0/find?extend_bbox=1&bbox=' . $coords['lat1'] . ',' . $coords['lng1'] . ',' . $coords['lat2'] . ',' . $coords['lng2'] . '&q=' . urlencode($query) . '&favorite_country=' . $coords['country_id'] . '&language=FRE&&max_results=199'; dd($url); $key = 'serach.near.' . sha1($url); $json = redis()->get($key); if (!$json) { $json = dwn($url); redis()->set($key, $json); } $data = json_decode($json, true); $pois = isAke($data, 'pois', []); foreach ($pois as $service) { $dbService = rdb('geo', 'service')->where(['code', '=', $service['rubricId']])->first(true); if ($dbService) { $id = $dbService->id; } else { if (isset($service['allRubrics'])) { if (count($service['allRubrics'])) { foreach ($service['allRubrics'] as $rubrikA) { $idr = isAke($rubrikA, 'id'); $lr = isAke($rubrikA, 'label'); $pr = isAke($rubrikA, 'rubricParentId'); if ($idr == $service['rubricId']) { $spi = rdb('geo', 'service')->firstOrCreate(['code' => $idr])->setLabel($lr)->setFamily($pr)->save(); $id = $spi->id; } } } } } $serviceproxIds = $supplements = []; $serviceproxIds[] = $id; $poi = $service['id']; $checkEtab = rdb('geo', 'etablissement')->where(['poi', '=', (string) $poi])->first(true); $add = $checkEtab ? false : true; unset($service['id']); if (false === $add) { $service['id'] = $checkEtab->id; } unset($service['offer']); unset($service['prov']); unset($service['pjBlocId']); unset($service['thematicId']); if (isset($service['tabs'])) { if (count($service['tabs'])) { foreach ($service['tabs'] as $tmp) { $tmpUrl = isAke($tmp, 'url', false); $tmpId = isAke($tmp, 'appId', false); if (false !== $tmpUrl && false !== $tmpId) { $key = 'inf.' . $poi . '.' . $tmpId; if ($tmpId == 'pj') { $inf = redis()->get($key); if (empty($inf)) { $infHtml = dwn($tmpUrl); $inf = substr($infHtml, strlen('callback('), -1); redis()->set($key, $inf); } $inf = json_decode($inf, true); $t = isAke($inf, 'tabs', []); if (!empty($t)) { foreach ($t as $tmpT) { $b = isAke($tmpT, 'blocks', []); $t = isAke($tmpT, 'tags', []); if (!empty($b)) { for ($ti = 0; $ti < count($b); $ti += 2) { $seg = $b[$ti]; $seg2 = $b[$ti + 1]; if (is_array($seg) && is_array($seg2)) { $title = isAke($seg, 'title', false); $kv = isAke($seg2, 'keyValue', false); if (false !== $title && false !== $kv) { $title = Inflector::urlize($title); foreach ($kv as $tmpRow) { $supplements[$title][] = [$tmpRow['key'] => $tmpRow['value']]; } } } } } } } } elseif ($tmpId == 'indoor') { } elseif ($tmpId == 'localbusinesspremium') { } elseif ($tmpId == 'total') { } elseif ($tmpId == 'totalaccess') { } elseif ($tmpId == 'darty') { } elseif ($tmpId == 'eleclerc') { } elseif ($tmpId == 'moteurproduitpromo') { } elseif ($tmpId == 'mappyshopping') { } elseif ($tmpId == 'booking') { $inf = redis()->get($key); if (empty($inf)) { $infHtml = dwn($tmpUrl); $inf = substr($infHtml, strlen('callback('), -1); redis()->set($key, $inf); } $inf = json_decode($inf, true); /* TODO */ } elseif ($tmpId == 'localbusinessdiscovery') { $inf = redis()->get($key); if (empty($inf)) { $infHtml = dwn($tmpUrl); $inf = substr($infHtml, strlen('callback('), -1); redis()->set($key, $inf); } $inf = json_decode($inf, true); $t = isAke($inf, 'tabs', []); if (!empty($t)) { foreach ($t as $tmpT) { $b = isAke($tmpT, 'blocks', []); if (!empty($b)) { foreach ($b as $tmpB) { $kv = isAke($tmpB, 'keyValue', false); if (false !== $kv) { foreach ($kv as $tmpRow) { $tmpK = Inflector::urlize($tmpRow['key']); $supplements['infos'][$tmpK] = $tmpRow['value']; } } } } } } } else { $inf = redis()->get($key); if (empty($inf)) { $infHtml = dwn($tmpUrl); $inf = substr($infHtml, strlen('callback('), -1); redis()->set($key, $inf); } $inf = json_decode($inf, true); } } } } } unset($service['tabs']); unset($service['contextualPoiUrl']); unset($service['providerIds']); unset($service['pjRatingId']); unset($service['hasUnclaimableProvider']); unset($service['additionalInfos']); unset($service['additionalInfo']); unset($service['coordinateProvider']); unset($service['coordinateProvider3D']); unset($service['offerType']); unset($service['rubricId']); unset($service['closestPanoramicId']); if (isset($service['allRubrics'])) { if (count($service['allRubrics'])) { foreach ($service['allRubrics'] as $rubrikA) { $idr = isAke($rubrikA, 'id'); $lr = isAke($rubrikA, 'label'); $pr = isAke($rubrikA, 'rubricParentId'); $spi = rdb('geo', 'service')->firstOrCreate(['code' => $idr])->setLabel($lr)->setFamily($pr)->save(); } } unset($service['allRubrics']); } if (isset($service['additionalRubricIds'])) { if (count($service['additionalRubricIds'])) { foreach ($service['additionalRubricIds'] as $newRubrique) { $spi = rdb('geo', 'service')->firstOrCreate(['code' => $newRubrique]); $serviceproxIds[] = $spi->id; } } } unset($service['additionalRubricIds']); unset($service['brandIconUrl']); unset($service['slat']); unset($service['slng']); unset($service['salt']); unset($service['brand']); unset($service['indoors']); unset($service['visibleIn3D']); unset($service['townCode']); if (isset($service['town'])) { $service['city'] = $service['town']; unset($service['town']); } if (isset($service['positions3D'])) { if (isset($service['positions3D']['origin'])) { if (isset($service['positions3D']['origin']['alt'])) { $service['altitude'] = $service['positions3D']['origin']['alt']; unset($service['positions3D']); } } } if (isset($service['way'])) { $service['address'] = $service['way']; unset($service['way']); } if (isset($service['pCode'])) { $service['zip'] = $service['pCode']; unset($service['pCode']); unset($service['positions3D']); } if (isset($service['lat']) && isset($service['lng'])) { $distances = distanceKmMiles($coords['lng1'], $coords['lat1'], $service['lng'], $service['lat']); $service['distance'] = (double) $distances['km']; } ksort($service); $service['poi'] = $poi; if (false === $add) { $collection[] = $service; } if (true === $add) { $distance = $service['distance']; unset($service['distance']); $etab = rdb('geo', 'etablissement')->firstOrCreate($service)->setPoi($poi)->save(); setLocation($etab, $etab->lng, $etab->lat); if (!empty($supplements)) { foreach ($supplements as $supK => $supV) { $setter = setter($supK); $etab->{$setter}($supV); } $etab->save(); } $fields = $etab->_keys(); $sFields = array_merge(array_keys($service), array_keys($supplements)); $except = ['id', 'poi', 'created_at', 'updated_at']; $resave = false; $expurge = []; foreach ($fields as $field) { if (!in_array($field, $except)) { if (!in_array($field, $sFields)) { $expurge[] = $field; $etab = $etab->expurge($field); $resave = true; } } } if (true === $resave) { $etab = $etab->save(); } foreach ($serviceproxIds as $serviceproxId) { $spTmp = rdb('geo', 'service')->find((int) $serviceproxId); $spTmp->attach($etab); } $zone = rdb('geo', 'zone')->find(1); $zone->attach($etab); $service['new'] = true; $service['id'] = $etab->id; $service['distance'] = $distance; } $collection[] = $service; } if (!empty($collection)) { $tuples = $new = []; $collection->sortBy('distance'); foreach ($collection as $row) { $hasPoi = isAke($row, 'poi', false); if (false === $hasPoi) { $new[] = $row; } else { $poi = sha1($hasPoi); if (!in_array($poi, $tuples)) { $tuples[] = $poi; $new[] = $row; } } } $collection = lib('collection', [$new]); } return $collection->toArray(); }
public function row($name, $value) { $setter = setter($name); return with(new Container())->{$setter}($value); }
public function out($data = []) { if (!$this->isOut()) { $keyFrom = $this->makeKeyLabel($this->from); $keyTo = $this->makeKeyLabel($this->to); $row = ['type' => $this->ns, 'from_id' => $this->to->id, 'from' => $keyTo, 'to_id' => $this->from->id, 'to' => $keyFrom]; $edgeRow = Model::GraphJoin()->create($row)->save(); foreach ($data as $k => $v) { $setter = setter($k); $edgeRow->{$setter}($v); } $relation->save(); $in = isset($this->from->in_graph) ? $this->from->in_graph : []; if (!is_array($in)) { $in = []; } $rowData = ['id' => $edgeRow->id, 'type' => $this->ns]; foreach ($data as $k => $v) { $rowData[$k] = $v; } $in[] = $rowData; $this->from->in_graph = $in; $this->from->save(); $out = isset($this->to->out_graph) ? $this->to->out_graph : []; if (!is_array($out)) { $out = []; } $rowData = ['id' => $edgeRow->id, 'type' => $this->ns]; foreach ($data as $k => $v) { $rowData[$k] = $v; } $out[] = $rowData; $this->to->out_graph = $out; $this->to->save(); } }
public function update(array $criteria) { $updated = 0; $items = (array) $this->getIterator(); foreach ($items as $item) { if (isset($item['id'])) { $row = $this->db->find((int) $item['id']); if ($row) { foreach ($criteria as $k => $v) { $setter = setter($k); $row->{$setter}($v); } $row->save(); $updated++; } } } return $updated; }