/** * */ public function search($modelCode, $query = null, $filters = null, $order = null, $group = null, $page = null, $pageSize = null) { // Store the search $searchId = $this->storeSearch(func_get_args()); // http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html // http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-query-string-query.html // Fix arguments $query = \z\service('helper/index')->fixQuery($modelCode, $query); $order = \z\service('helper/index')->fixOrder($modelCode, $order); // Fix the page size if ($pageSize <= 0) { $pageSize = \z\pref('page/size'); } // Build the search input $input = ['index' => $modelCode, 'body' => ['query' => ['query_string' => ['default_operator' => 'and', 'query' => $query]], 'sort' => $order], 'from' => $page * $pageSize, 'size' => $pageSize]; // Perform the search $output = \z\service('driver/db/es')->search($input); // Parse hits to build data $data = []; foreach ($output['hits']['hits'] as $hit) { $data[$hit['_id']] = $hit['_source']; } // Build the result $result = ['id' => $searchId, 'size' => $output['hits']['total'], 'range' => [$input['from'], $input['size']], 'data' => $data]; return $result; }
/** * */ public function initialize() { // Define locale (first from session, then from HTTP header) $this->_localeCode = \z\request()->session('culture/locale'); if (empty($this->_localeCode) === true) { $this->_localeCode = locale_accept_from_http(\z\request()->header('Accept-Language')); } if (empty($this->_localeCode) === true) { $this->_localeCode = \z\pref('culture/locale'); } // Define fallback $this->_fallbackCode = \z\pref('culture/fallback'); // Canonicalize locales $this->_localeCode = locale_canonicalize($this->_localeCode); $this->_fallbackCode = locale_canonicalize($this->_fallbackCode); // List locales available $locales = \z\service('helper/file')->listFiles(PATH_APP . '/Config/Strings', null, false, true, false); // Is the locale available? if (in_array($this->_localeCode, $locales) === false) { // Grab the parent code (e.g. en for en_US) $primaryCode = locale_get_primary_language($this->_localeCode); // Find locales belonging to this parent $locales = array_filter($locales, function ($key) use($primaryCode) { return strpos($key, $primaryCode) === 0; }); // Sort locales A-Z sort($locales); // Grab the very first locale $this->_localeCode = array_shift($locales); } // Load strings $this->loadStrings(); }
/** * */ public function getConnection($connectionCode = null) { // Grab definitions $definitions = \z\pref('db/connections'); if (is_array($definitions) === false) { $definitions = []; } // Define the connection code if (empty($connectionCode) === true) { $definitions = array_splice($definitions, 0, 1); $keys = array_keys($definitions); $connectionCode = array_shift($keys); } // Get the definition if (array_key_exists($connectionCode, $definitions) === true) { $definition = $definitions[$connectionCode]; } // Check whether there's a definition if (empty($definition) === true) { \z\e(EXCEPTION_DB_CONNECTION_NOT_FOUND, ['connectionCode' => $connectionCode, 'definitions' => $definitions]); } // Has the connection been retrieved already? if (array_key_exists($connectionCode, $this->_connections) === false) { // Build the connection $connection = \z\service('factory/connection')->buildConnection($definition); // Connect the driver $connection->driver->connect($connection); // Store the connection $this->_connections[$connectionCode] = $connection; } // Get the connection $connection = $this->_connections[$connectionCode]; return $connection; }
/** * */ private function loadPreferences() { // Get the cache $cacheCode = 'preferences_' . \z\boot()->environment; $cache = \z\cache()->getCache($cacheCode); if ($cache !== false) { $this->_preferences = unserialize($cache); return; } // Define the number of passes $dependencies = \z\boot()->dependencies; $nbPasses = count($dependencies) - 1; // Perform n passes for ($i = 0; $i < $nbPasses; $i++) { foreach ($dependencies as $dependency) { // List preferences $paths = array_merge(\z\service('helper/file')->listFiles($dependency . '/Config/Preferences', 'json'), \z\service('helper/file')->listFiles($dependency . '/Config/Preferences/' . \z\boot()->environment, 'json')); // Parse each path foreach ($paths as $path) { // Build the preference code $parentPreferenceCode = strtolower(basename($path, '.json')); // Load preferences $preferences = \z\service('factory/json')->loadJson($path); // Store preferences foreach ($preferences as $preferenceCode => $preferenceValue) { // Build the new preference code $preferenceCode = $parentPreferenceCode . '/' . $preferenceCode; // Get the previous preference value $oldPreferenceValue = \z\pref($preferenceCode); // Is it an array? if (is_array($oldPreferenceValue) === true) { // Merge the old + the new $preferenceValue = array_merge($oldPreferenceValue, $preferenceValue); } // Set the preference \z\pref($preferenceCode, $preferenceValue); } } } // End of pass, remove the first extension array_shift($dependencies); } // Set the cache \z\cache()->setCache($cacheCode, serialize($this->_preferences)); }
/** * */ public function scrollModel($modelCode, $page = null, $pageSize = null) { // Define the page size if (empty($pageSize) === true) { $pageSize = \z\pref('page/size'); } // Select inputs within the page boundary $inputs = \z\service('factory/query')->select()->from($modelCode)->offset($page * $pageSize)->limit($pageSize)->execute(); // Parse each input $result = []; foreach ($inputs as $input) { // Build an object $object = \z\service('factory/object')->buildObject($modelCode); // Import the input $object->import($input); // Store the object $result[] = $object; } return $result; }
/** * */ public function fixOrder($modelCode, $order) { // Make sure $order is an array if (is_array($order) === false) { $order = []; } // Use default ordering if none defined if (empty($order) === true) { // Get order for the model $order = \z\pref('search/' . $modelCode . '/order'); if (is_array($order) === false) { $order = ['date_created' => 'desc']; } } // Build the final sort array $result = []; foreach ($order as $key => $direction) { $result[] = [$key => ['order' => $direction]]; } return $result; }