Ejemplo n.º 1
0
 /**
  * @param int $sectionId
  * @param bool $forPublic
  * @return static[]
  */
 public static function findAllBySection($sectionId, $forPublic = false)
 {
     if (!$forPublic) {
         return self::findAllByAttributes(['section_id' => $sectionId], ['order' => '`order` ASC']);
     }
     if (!UserAccess::get()->canRead($sectionId)) {
         return [];
     }
     $condition = new ModelCondition(['model' => __CLASS__]);
     $condition->join = "LEFT JOIN forum_groups2categories ON (category_id = id AND group_id = :group)";
     $condition->addCondition("canread IS NULL OR canread = 1");
     $condition->setParam(":group", UserAccess::get()->getUserGroup($sectionId, true));
     $condition->order = "`order` ASC";
     $condition->with = ['subcategories', 'subcategories.lastActiveThread', 'subcategories.lastActiveUser'];
     $condition->compareColumn("section_id", $sectionId);
     $categories = self::findAll($condition);
     if (WebApp::get()->user()->isConnected()) {
         $subcategories = self::getDb()->table('forum_userhiddensubcategories')->where("user_id = :user")->setParam(":user", WebApp::get()->user()->id)->get();
         $subcategories = ArrayHelper::get()->transform($subcategories, "subcategory_id");
         foreach ($categories as $category) {
             if ($category->subcategories) {
                 foreach ($category->subcategories as $subcategory) {
                     if (in_array($subcategory->id, $subcategories)) {
                         $subcategory->hidden = true;
                     }
                 }
             }
         }
     }
     return $categories;
 }
Ejemplo n.º 2
0
 /**
  * @param $user
  * @param $section
  * @return static
  */
 public static function findByUserAndSection($user, $section)
 {
     $condition = new ModelCondition();
     $condition->join = "INNER JOIN `forum_users2groups` ON group_id = `t`.`id` AND user_id = :user";
     $condition->compareColumn("section_id", $section);
     $condition->setParam(":user", $user);
     $condition->fields = "`t`.*";
     return self::find($condition);
 }
Ejemplo n.º 3
0
 /**
  * @param DbModel[] $models
  * @param string|array $fields
  * @return ModelCondition
  */
 public function getConditionForModels($models, $fields)
 {
     $key = '__parentRelationKey';
     $column = "(CASE ";
     $mCondition = new ModelCondition(['model' => $this->model]);
     if ($this->joins) {
         foreach ($this->joins as $join) {
             if (is_array($join[0])) {
                 $t = '`' . key($join[0]) . '` as `' . current($join[0]) . '`';
             } else {
                 $t = '`' . $join[0] . '`';
             }
             $c = [];
             // columns;
             foreach ($join[1] as $col1 => $col2) {
                 if (is_array($col2)) {
                 } else {
                     if ('!=' == substr($col2, 0, 2)) {
                     } elseif ('>=' == substr($col2, 0, 2)) {
                     } elseif ('<=' == substr($col2, 0, 2)) {
                     } elseif ('>' == $col2[0]) {
                     } elseif ('<' == $col2[0]) {
                     } else {
                     }
                 }
             }
             // values;
             foreach ($join[2] as $col => $value) {
                 if (is_array($value)) {
                 } else {
                     if ('!=' == substr($value, 0, 2)) {
                     } elseif ('>=' == substr($value, 0, 2)) {
                     } elseif ('<=' == substr($value, 0, 2)) {
                     } elseif ('>' == $value[0]) {
                     } elseif ('<' == $value[0]) {
                     } else {
                     }
                 }
             }
             // type;
             $mCondition->join .= $join[3] . $t . " ON (" . implode(') AND (', $c) . ")";
         }
     }
     foreach ($this->conditions as $ck => $condition) {
         switch ($condition[0]) {
             case "=":
             case "!=":
                 $parentColumn = $condition[1];
                 // for now it only supports single column for this type of relations;
                 $relationColumn = $condition[2];
                 // same for relation column
                 if (is_string($parentColumn) && (false !== strpos($parentColumn, '.') && false !== strpos($parentColumn, '_'))) {
                     $r = $relationColumn;
                     $relationColumn = $parentColumn;
                     $parentColumn = $r;
                 }
                 if (is_array($relationColumn)) {
                     $relationColumnListForIn = [];
                     foreach ($relationColumn as $c) {
                         $relationColumnListForIn[] = $this->_column($c, true);
                     }
                     $relationColumnListForIn = implode(', ', $relationColumnListForIn);
                 } else {
                     $relationColumnListForIn = $relationColumn;
                 }
                 $conditionParts = [];
                 foreach ($models as $mk => $model) {
                     // generates CASE column WHEN parentv1 THEN parentk1 WHEN parentv2 THEN parentk2 ELSE -1 AS __parentRelationKey  + the condition itself
                     $paramKey = ':' . $ck . '_' . $mk . '_';
                     if (is_array($parentColumn) && is_array($relationColumn)) {
                         $column .= "WHEN (";
                         $columnList = [];
                         foreach ($parentColumn as $col) {
                             $mCondition->setParam($paramKey . $col, $model->{$col});
                             $conditionParts[] = $paramKey . $col . ('!=' == $condition[0] ? 'NOT' : '') . ' IN ( ' . $relationColumnListForIn . ' )';
                             $columnList[] = $paramKey . $col . ('!=' == $condition[0] ? 'NOT' : '') . ' IN ( ' . $relationColumnListForIn . ' )';
                         }
                         $column .= implode(' OR ', $columnList);
                         $column .= ") THEN {$mk} ";
                     } elseif (is_array($parentColumn)) {
                         $column .= "WHEN (" . $this->_column($relationColumn, true) . ('!=' == $condition[0] ? 'NOT' : '') . ' IN ';
                         $paramList = [];
                         foreach ($parentColumn as $col) {
                             $mCondition->setParam($paramKey . $col, $model->{$col});
                             $paramList[] = $paramKey . $col;
                         }
                         $conditionParts[] = $this->_column($relationColumn, true) . ('!=' == $condition[0] ? 'NOT' : '') . ' IN (' . implode(', ', $paramList) . ')';
                         $column .= "(" . implode(", ", $paramList) . ")";
                         $column .= ") THEN {$mk} ";
                     } elseif (is_array($relationColumn)) {
                         $mCondition->setParam($paramKey . $parentColumn, $model->{$parentColumn});
                         $conditionParts[] = $paramKey . $parentColumn . ('!=' == $condition[0] ? 'NOT' : '') . " IN ({$relationColumnListForIn})";
                         $column .= "WHEN {$paramKey} . {$parentColumn} " . ('!=' == $condition[0] ? 'NOT' : '') . " IN ({$relationColumnListForIn}) THEN {$mk} ";
                     } else {
                         if ("(CASE " == $column) {
                             $column .= $this->_column($relationColumn, true);
                         }
                         $p = str_replace('.', '__', $paramKey . $relationColumn);
                         $mCondition->setParam($p, $model->{$parentColumn});
                         $conditionParts[] = $p;
                         $column .= " WHEN " . $p . " THEN {$mk} ";
                     }
                 }
                 if (is_string($parentColumn) && is_string($relationColumn)) {
                     $mCondition->addCondition($this->_column($relationColumn, true) . ('!=' == $condition[0] ? 'NOT' : '') . " IN (" . implode(', ', $conditionParts) . ")");
                 } else {
                     $mCondition->addCondition('(' . implode(") OR (", $conditionParts) . ')');
                 }
                 break;
             case "==":
             case "=attribute":
                 if ($condition[0] == '=attribute') {
                     // for attribute read the value
                     $parentModel = get_class(current($models));
                     $c = $condition[2];
                     if (false === strpos($c, '(')) {
                         $condition[2] = $parentModel::$c;
                     } else {
                         // for method call it and get the value;
                         $c = substr($c, 0, strlen($c) - 2);
                         $condition[2] = call_user_func("{$parentModel}::{$c}");
                     }
                 }
                 $mCondition->compareColumn($condition[1], $condition[2]);
                 break;
         }
     }
     $column .= "ELSE -1 END) AS `{$key}`";
     //die($column);
     if (is_array($fields)) {
         $fields[] = $column;
     } else {
         $fields .= ', ' . $column;
     }
     $mCondition->fields = $fields;
     return $mCondition;
 }
Ejemplo n.º 4
0
 /**
  * Used on search page
  * @param string $text
  * @param int $section
  * @param int[] $categories
  * @param int[] $subcategories
  * @param int[] $authors
  * @param int $page
  * @return array|static[]
  */
 public static function findAllByKeyWords($text, $section, $categories = null, $subcategories = null, $authors = null, $page = 1)
 {
     if (!trim($text)) {
         return [];
     }
     $words = explode(" ", $text);
     $condition = new ModelCondition(['model' => __CLASS__]);
     $condition->with = ['lastActiveUser', 'owner', 'subcategory', 'category'];
     $condition->join = "LEFT JOIN `forum_thread_tags` ON forum_thread_tags.thread_id = `t`.id";
     $wordsList = [];
     foreach ($words as $k => $word) {
         if (!trim($word)) {
             continue;
         }
         $condition->setParam(":word_{$k}", $word);
         $wordsList[] = ":word_{$k}";
     }
     $words = implode(", ", $wordsList);
     $condition->addCondition("`t`.`title` LIKE :searchConcat OR (`forum_thread_tags`.`word` IN ({$words}))");
     $condition->setParam(":searchConcat", "%{$text}%");
     $condition->compareColumn("section_id", $section);
     if ($categories) {
         $condition->compareColumn("category_id", $categories);
     }
     if ($subcategories) {
         $condition->compareColumn("subcategory_id", $subcategories);
     }
     if ($authors) {
         $condition->compareColumn("user_id", $authors);
     }
     $condition->limit = Config::value('FORUM_THREADS_PER_PAGE');
     $condition->order = '`t`.`id` DESC';
     $condition->offset = ($page - 1) * Config::value('FORUM_THREADS_PER_PAGE');
     return self::findAll($condition);
 }