/**
  * Gets matching resources by tags. This is adapted function from miniShop1 for backward compatibility
  * @deprecated
  *
  * @param array $tags Tags for search
  * @param int $only_ids Return only ids of matched resources
  * @param int $strict 0 - goods must have at least one specified tag
  *					  1 - goods must have all specified tags, but can have more
  * 					  2 - goods must have exactly the same tags.
  * @return array $ids Or array with resources with data and tags
  */
 function getTagged($tags = array(), $strict = 0, $only_ids = 0)
 {
     if (!is_array($tags)) {
         $tags = explode(',', $tags);
     }
     $q = $this->modx->newQuery('msProductOption', array('key' => 'tags', 'value:IN' => $tags));
     $q->select('product_id');
     $ids = array();
     if ($q->prepare() && $q->stmt->execute()) {
         $ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
     }
     $ids = array_unique($ids);
     // If needed only ids of not strictly mathed items - return.
     if (!$strict && $only_ids) {
         return $ids;
     }
     // Filtering ids
     $count = count($tags);
     /* @var PDOStatement $stmt*/
     if ($strict) {
         foreach ($ids as $key => $product_id) {
             if ($strict > 1) {
                 $sql = "SELECT COUNT(*) FROM {$this->modx->getTableName('msProductOption')} WHERE `product_id` = {$product_id} AND `key` = 'tags';";
                 $stmt = $this->modx->prepare($sql);
                 $stmt->execute();
                 if ($stmt->fetch(PDO::FETCH_COLUMN) != $count) {
                     unset($ids[$key]);
                     continue;
                 }
             }
             foreach ($tags as $tag) {
                 $sql = "SELECT COUNT(`product_id`) FROM {$this->modx->getTableName('msProductOption')} WHERE `product_id` = {$product_id} AND `key` = 'tags' AND `value` = '{$tag}';";
                 $stmt = $this->modx->prepare($sql);
                 $stmt->execute();
                 if (!$stmt->fetch(PDO::FETCH_COLUMN)) {
                     unset($ids[$key]);
                     break;
                 }
             }
         }
     }
     // Return strictly ids, if needed
     $ids = array_unique($ids);
     if ($only_ids) {
         return $ids;
     }
     // Process results
     $data = array();
     foreach ($ids as $id) {
         if (!$only_ids) {
             if ($res = $this->modx->getObject('msProduct', $id)) {
                 $data[$id] = $res->toArray();
             }
         }
     }
     return $data;
 }
Beispiel #2
0
 /** Create tree */
 public static function createTree($total, $lvls = 3)
 {
     self::$modx->query("DELETE FROM " . self::$ClientTable);
     self::$modx->query("ALTER TABLE " . self::$ClientTable . " AUTO_INCREMENT=1");
     // distribution of elements on the levels of nesting
     $num = $total;
     $lvlsArr = array();
     for ($i = $lvls; $i > 0; $i--) {
         if ($i == 1) {
             $limit = $num;
         } else {
             $limit = ceil($num * 0.6);
             $num -= $limit;
         }
         $lvlsArr[$i] = $limit;
     }
     $sql = "INSERT INTO " . self::$ClientTable . " (`id`, `parent`) VALUES (:id, :parent)";
     $stmt = self::$modx->prepare($sql);
     $pArr = array();
     $k = 0;
     for ($i = 1; $i <= $lvls; $i++) {
         $limit = $lvlsArr[$i];
         echo $i . ' level: ' . $limit . ' pcs.<br>';
         while ($limit--) {
             $k = $k + 1;
             $prev = NULL;
             if ($i > 1) {
                 $c = count($pArr[$i - 1]) - 1;
                 $prev = $pArr[$i - 1][mt_rand(0, $c)];
             } else {
                 $c = 0;
                 $prev = 0;
             }
             if ($stmt instanceof PDOStatement) {
                 $stmt->bindValue(':id', $k);
                 $stmt->bindValue(':parent', $prev);
                 if ($stmt->execute()) {
                     $pArr[$i][] = $k;
                     //$this->modx->lastInsertId();
                 } else {
                     throw new Exception('Error add');
                 }
             }
         }
     }
 }