/** * Saves messages to database * * @param array $messages * @param \yii\db\Connection $db * @param string $sourceMessageTable * @param string $messageTable * @param boolean $removeUnused * @param array $languages * @param boolean $markUnused */ protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages, $markUnused) { $q = new \Leaps\Db\Query(); $current = []; foreach ($q->select(['id', 'category', 'message'])->from($sourceMessageTable)->all() as $row) { $current[$row['category']][$row['id']] = $row['message']; } $new = []; $obsolete = []; foreach ($messages as $category => $msgs) { $msgs = array_unique($msgs); if (isset($current[$category])) { $new[$category] = array_diff($msgs, $current[$category]); $obsolete += array_diff($current[$category], $msgs); } else { $new[$category] = $msgs; } } foreach (array_diff(array_keys($current), array_keys($messages)) as $category) { $obsolete += $current[$category]; } if (!$removeUnused) { foreach ($obsolete as $pk => $m) { if (mb_substr($m, 0, 2) === '@@' && mb_substr($m, -2) === '@@') { unset($obsolete[$pk]); } } } $obsolete = array_keys($obsolete); $this->stdout("Inserting new messages..."); $savedFlag = false; foreach ($new as $category => $msgs) { foreach ($msgs as $m) { $savedFlag = true; $lastPk = $db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $m]); foreach ($languages as $language) { $db->createCommand()->insert($messageTable, ['id' => $lastPk['id'], 'language' => $language])->execute(); } } } $this->stdout($savedFlag ? "saved.\n" : "Nothing new...skipped.\n"); $this->stdout($removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages..."); if (empty($obsolete)) { $this->stdout("Nothing obsoleted...skipped.\n"); } else { if ($removeUnused) { $db->createCommand()->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute(); $this->stdout("deleted.\n"); } elseif ($markUnused) { $db->createCommand()->update($sourceMessageTable, ['message' => new \Leaps\Db\Expression("CONCAT('@@',message,'@@')")], ['in', 'id', $obsolete])->execute(); $this->stdout("updated.\n"); } } }
/** * @inheritdoc */ protected function getMigrationHistory($limit) { if ($this->db->schema->getTableSchema($this->migrationTable, true) === null) { $this->createMigrationHistoryTable(); } $query = new Query(); $rows = $query->select(['version', 'apply_time'])->from($this->migrationTable)->orderBy('apply_time DESC, version DESC')->limit($limit)->createCommand($this->db)->queryAll(); $history = ArrayHelper::map($rows, 'version', 'apply_time'); unset($history[self::BASE_MIGRATION]); return $history; }
/** * Loads the messages from database. * You may override this method to customize the message storage in the database. * @param string $category the message category. * @param string $language the target language. * @return array the messages loaded from database. */ protected function loadMessagesFromDb($category, $language) { $mainQuery = new Query(); $mainQuery->select(['t1.message message', 't2.translation translation'])->from(["{$this->sourceMessageTable} t1", "{$this->messageTable} t2"])->where('t1.id = t2.id AND t1.category = :category AND t2.language = :language')->params([':category' => $category, ':language' => $language]); $fallbackLanguage = substr($language, 0, 2); if ($fallbackLanguage != $language) { $fallbackQuery = new Query(); $fallbackQuery->select(['t1.message message', 't2.translation translation'])->from(["{$this->sourceMessageTable} t1", "{$this->messageTable} t2"])->where('t1.id = t2.id AND t1.category = :category AND t2.language = :fallbackLanguage')->andWhere("t2.id NOT IN (SELECT id FROM {$this->messageTable} WHERE language = :language)")->params([':category' => $category, ':language' => $language, ':fallbackLanguage' => $fallbackLanguage]); $mainQuery->union($fallbackQuery, true); } $messages = $mainQuery->createCommand($this->db)->queryAll(); return ArrayHelper::map($messages, 'message', 'translation'); }
/** * Retrieves multiple values from cache with the specified keys. * @param array $keys a list of keys identifying the cached values * @return array a list of cached values indexed by the keys */ protected function getValues($keys) { if (empty($keys)) { return []; } $query = new Query(); $query->select(['id', 'data'])->from($this->cacheTable)->where(['id' => $keys])->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')'); if ($this->db->enableQueryCache) { $this->db->enableQueryCache = false; $rows = $query->createCommand($this->db)->queryAll(); $this->db->enableQueryCache = true; } else { $rows = $query->createCommand($this->db)->queryAll(); } $results = []; foreach ($keys as $key) { $results[$key] = false; } foreach ($rows as $row) { $results[$row['id']] = $row['data']; } return $results; }
/** * Performs access check for the specified user. * This method is internally called by [[checkAccess()]]. * * @param string|integer $user the user ID. This should can be either an integer or a string representing * the unique identifier of a user. See [[\Leaps\Web\User::id]]. * @param string $itemName the name of the operation that need access check * @param array $params name-value pairs that would be passed to rules associated * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, * which holds the value of `$userId`. * @param Assignment[] $assignments the assignments to the specified user * @return boolean whether the operations can be performed by the user. */ protected function checkAccessRecursive($user, $itemName, $params, $assignments) { if (($item = $this->getItem($itemName)) === null) { return false; } Leaps::trace($item instanceof Role ? "Checking role: {$itemName}" : "Checking permission: {$itemName}", __METHOD__); if (!$this->executeRule($user, $item, $params)) { return false; } if (isset($assignments[$itemName]) || in_array($itemName, $this->defaultRoles)) { return true; } $query = new Query(); $parents = $query->select(['parent'])->from($this->itemChildTable)->where(['child' => $itemName])->column($this->db); foreach ($parents as $parent) { if ($this->checkAccessRecursive($user, $parent, $params, $assignments)) { return true; } } return false; }
/** * Session write handler. * Do not call this method directly. * @param string $id session ID * @param string $data session data * @return boolean whether session write is successful */ public function writeSession($id, $data) { // exception must be caught in session write handler // http://us.php.net/manual/en/function.session-set-save-handler.php try { $query = new Query(); $exists = $query->select(['id'])->from($this->sessionTable)->where(['id' => $id])->createCommand($this->db)->queryScalar(); $fields = $this->composeFields($id, $data); if ($exists === false) { $this->db->createCommand()->insert($this->sessionTable, $fields)->execute(); } else { unset($fields['id']); $this->db->createCommand()->update($this->sessionTable, $fields, ['id' => $id])->execute(); } } catch (\Exception $e) { $exception = ErrorHandler::convertExceptionToString($e); // its too late to use Leaps logging here error_log($exception); echo $exception; return false; } return true; }