| Total Complexity | 118 |
| Total Lines | 966 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProductsHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductsHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ProductsHandler extends OledrionPersistableObjectHandler |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * ProductsHandler constructor. |
||
| 38 | * @param \XoopsDatabase|null $db |
||
| 39 | */ |
||
| 40 | public function __construct(\XoopsDatabase $db = null) |
||
|
|
|||
| 41 | { |
||
| 42 | // Table Classe Id Libellé |
||
| 43 | parent::__construct($db, 'oledrion_products', Products::class, 'product_id', 'product_title'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Retourne la liste des x produits les plus vus par les visiteurs |
||
| 48 | * |
||
| 49 | * @param Parameters $parameters |
||
| 50 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 51 | * @internal param int $start Début des données |
||
| 52 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 53 | * @internal param int $category Identifiant de la catégorie (évenutellement) |
||
| 54 | * @internal param string $sort Champ sur lequel trier |
||
| 55 | * @internal param string $order Sens du tri |
||
| 56 | */ |
||
| 57 | public function getMostViewedProducts(Parameters $parameters) |
||
| 58 | { |
||
| 59 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 60 | 'start' => 0, |
||
| 61 | 'limit' => 0, |
||
| 62 | 'category' => 0, |
||
| 63 | 'sort' => 'product_hits', |
||
| 64 | 'order' => 'DESC', |
||
| 65 | ])); |
||
| 66 | $data = []; |
||
| 67 | $criteria = new \CriteriaCompo(); |
||
| 68 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 69 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 70 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 71 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 72 | } |
||
| 73 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 74 | // Se limiter aux seuls produits encore en stock |
||
| 75 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 76 | } |
||
| 77 | if (is_array($parameters['category']) && count($parameters['category']) > 0) { |
||
| 78 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 79 | } elseif (0 != $parameters['category']) { |
||
| 80 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 81 | } |
||
| 82 | $criteria->add(new \Criteria('product_hits', 0, '>')); |
||
| 83 | |||
| 84 | $criteria->setLimit($parameters['limit']); |
||
| 85 | $criteria->setStart($parameters['start']); |
||
| 86 | $criteria->setSort($parameters['sort']); |
||
| 87 | $criteria->setOrder($parameters['order']); |
||
| 88 | $data = $this->getObjects($criteria, true); |
||
| 89 | |||
| 90 | return $data; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Retourne la liste des x produits les mieux notés par les visiteurs |
||
| 95 | * |
||
| 96 | * @param Parameters $parameters |
||
| 97 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 98 | * @internal param int $start Début des données |
||
| 99 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 100 | * @internal param int $category Identifiant de la catégorie (évenutellement) |
||
| 101 | */ |
||
| 102 | public function getBestRatedProducts(Parameters $parameters) |
||
| 103 | { |
||
| 104 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 105 | 'start' => 0, |
||
| 106 | 'limit' => 0, |
||
| 107 | 'category' => 0, |
||
| 108 | 'sort' => 'product_rating', |
||
| 109 | 'order' => 'DESC', |
||
| 110 | ])); |
||
| 111 | $data = []; |
||
| 112 | $criteria = new \CriteriaCompo(); |
||
| 113 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 114 | $criteria->add(new \Criteria('product_rating', 0, '>')); // Se limiter aux seuls produits qui ont été vraiment notés |
||
| 115 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 116 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 117 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 118 | } |
||
| 119 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 120 | // Se limiter aux seuls produits encore en stock |
||
| 121 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 122 | } |
||
| 123 | if (is_array($parameters['category']) && count($parameters['category']) > 0) { |
||
| 124 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 125 | } elseif (0 != $parameters['category']) { |
||
| 126 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 127 | } |
||
| 128 | $criteria->setLimit($parameters['limit']); |
||
| 129 | $criteria->setStart($parameters['start']); |
||
| 130 | $criteria->setSort($parameters['sort']); |
||
| 131 | $criteria->setOrder($parameters['order']); |
||
| 132 | $data = $this->getObjects($criteria, true); |
||
| 133 | |||
| 134 | return $data; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Retourne la liste des x derniers produits recommandés |
||
| 139 | * |
||
| 140 | * @param Parameters $parameters |
||
| 141 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 142 | * @internal param int $start Indice de départ |
||
| 143 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 144 | * @internal param int $category Identifiant de la catégorie (évenutellement) |
||
| 145 | */ |
||
| 146 | public function getRecentRecommended(Parameters $parameters) |
||
| 147 | { |
||
| 148 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 149 | 'start' => 0, |
||
| 150 | 'limit' => 0, |
||
| 151 | 'category' => 0, |
||
| 152 | 'sort' => 'product_recommended', |
||
| 153 | 'order' => 'DESC', |
||
| 154 | ])); |
||
| 155 | $data = []; |
||
| 156 | $criteria = new \CriteriaCompo(); |
||
| 157 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 158 | $criteria->add(new \Criteria('product_recommended', '0000-00-00', '<>')); |
||
| 159 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 160 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 161 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 162 | } |
||
| 163 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 164 | // Se limiter aux seuls produits encore en stock |
||
| 165 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 166 | } |
||
| 167 | if (is_array($parameters['category'])) { |
||
| 168 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 169 | } elseif (0 != $parameters['category']) { |
||
| 170 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 171 | } |
||
| 172 | $criteria->setLimit($parameters['limit']); |
||
| 173 | $criteria->setStart($parameters['start']); |
||
| 174 | $criteria->setSort($parameters['sort']); |
||
| 175 | $criteria->setOrder($parameters['order']); |
||
| 176 | $data = $this->getObjects($criteria, true); |
||
| 177 | |||
| 178 | return $data; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Retourne le nombre total de produits recommandés |
||
| 183 | * |
||
| 184 | * @return int Le nombre total de produits recommandés |
||
| 185 | */ |
||
| 186 | public function getRecommendedCount() |
||
| 187 | { |
||
| 188 | $criteria = new \CriteriaCompo(); |
||
| 189 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 190 | $criteria->add(new \Criteria('product_recommended', '0000-00-00', '<>')); |
||
| 191 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 192 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 193 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 194 | } |
||
| 195 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 196 | // Se limiter aux seuls produits encore en stock |
||
| 197 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->getCount($criteria); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Retourne la liste des x derniers produits parus toutes catégories confondues ou dans une catégorie spécifique |
||
| 205 | * |
||
| 206 | * @param Parameters $parameters |
||
| 207 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 208 | * @internal param int $start Début des données |
||
| 209 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 210 | * @internal param mixed $category Identifiant de la catégorie (évenutellement) ou tableau d'ID ou rien du tout |
||
| 211 | * @internal param string $sort Champ(s) à utiliser pour le tri |
||
| 212 | * @internal param string $order Ordre de tri |
||
| 213 | * @internal param int $excluded Produit à exclure de la liste (éventuellement) |
||
| 214 | * @internal param bool $thisMonthOnly Indique s'il ne faut prendre que les produits du mois |
||
| 215 | */ |
||
| 216 | public function getRecentProducts(Parameters $parameters) |
||
| 217 | { |
||
| 218 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 219 | 'start' => 0, |
||
| 220 | 'limit' => 0, |
||
| 221 | 'category' => 0, |
||
| 222 | 'sort' => 'product_submitted DESC, product_title', |
||
| 223 | 'order' => '', |
||
| 224 | 'excluded' => 0, |
||
| 225 | 'thisMonthOnly' => false, |
||
| 226 | ])); |
||
| 227 | $data = []; |
||
| 228 | $criteria = new \CriteriaCompo(); |
||
| 229 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 230 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 231 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 232 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 233 | } |
||
| 234 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 235 | // Se limiter aux seuls produits encore en stock |
||
| 236 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 237 | } |
||
| 238 | if (is_array($parameters['category']) && count($parameters['category']) > 0) { |
||
| 239 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 240 | } elseif ($parameters['category'] > 0) { |
||
| 241 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 242 | } |
||
| 243 | if ($parameters['excluded'] > 0) { |
||
| 244 | $criteria->add(new \Criteria('product_id', $parameters['excluded'], '<>')); |
||
| 245 | } |
||
| 246 | |||
| 247 | if ($parameters['thisMonthOnly']) { |
||
| 248 | $criteria->add(Oledrion\Utility::getThisMonthCriteria()); |
||
| 249 | } |
||
| 250 | |||
| 251 | $criteria->setLimit($parameters['limit']); |
||
| 252 | $criteria->setStart($parameters['start']); |
||
| 253 | $criteria->setSort($parameters['sort']); |
||
| 254 | if ('' !== xoops_trim($parameters['order'])) { |
||
| 255 | $criteria->setOrder($parameters['order']); |
||
| 256 | } |
||
| 257 | $data = $this->getObjects($criteria, true); |
||
| 258 | |||
| 259 | return $data; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Retourne le nombre total de produits récents (éventuellement dans une catégorie) |
||
| 264 | * |
||
| 265 | * @param mixed $category Array ou Integer |
||
| 266 | * @param int $excludedProduct ID d'un produit à exclure |
||
| 267 | * @return int |
||
| 268 | */ |
||
| 269 | public function getRecentProductsCount($category = 0, $excludedProduct = 0) |
||
| 270 | { |
||
| 271 | $criteria = new \CriteriaCompo(); |
||
| 272 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 273 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 274 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 275 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 276 | } |
||
| 277 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 278 | // Se limiter aux seuls produits encore en stock |
||
| 279 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 280 | } |
||
| 281 | if (is_array($category)) { |
||
| 282 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $category) . ')', 'IN')); |
||
| 283 | } elseif ($category > 0) { |
||
| 284 | $criteria->add(new \Criteria('product_cid', (int)$category, '=')); |
||
| 285 | } |
||
| 286 | if ($excludedProduct > 0) { |
||
| 287 | $criteria->add(new \Criteria('product_id', $excludedProduct, '<>')); |
||
| 288 | } |
||
| 289 | |||
| 290 | return $this->getCount($criteria); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Retourne la liste des produits qui correspondent à des "critères" de manière à les utiliser pour la newsletter |
||
| 295 | * |
||
| 296 | * @param Parameters $parameters |
||
| 297 | * @return array Des objects de type produits |
||
| 298 | * @internal param int $startingDate Date de soumission du produit à prendre comme borne inférieure |
||
| 299 | * @internal param int $endingDate Date de soumission du produit à prendre comme borne supérieure |
||
| 300 | * @internal param mixed $category Soit un tableau d'ID de catégories soit un ID unique de catégorie |
||
| 301 | * @internal param int $start Position de départ |
||
| 302 | * @internal param int $limit Nombre d'enregistrements à retourner |
||
| 303 | */ |
||
| 304 | public function getProductsForNewsletter(Parameters $parameters) |
||
| 305 | { |
||
| 306 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 307 | 'startingDate' => 0, |
||
| 308 | 'endingDate' => 0, |
||
| 309 | 'category' => 0, |
||
| 310 | 'start' => 0, |
||
| 311 | 'limit' => 0, |
||
| 312 | ])); |
||
| 313 | $data = []; |
||
| 314 | $criteria = new \CriteriaCompo(); |
||
| 315 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 316 | $criteria->add(new \Criteria('product_submitted', $parameters['startingDate'], '>=')); |
||
| 317 | $criteria->add(new \Criteria('product_submitted', $parameters['endingDate'], '<=')); |
||
| 318 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 319 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 320 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 321 | } |
||
| 322 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 323 | // Se limiter aux seuls produits encore en stock |
||
| 324 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 325 | } |
||
| 326 | if (is_array($parameters['category'])) { |
||
| 327 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 328 | } elseif ($parameters['category'] > 0) { |
||
| 329 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 330 | } |
||
| 331 | $criteria->setLimit($parameters['limit']); |
||
| 332 | $criteria->setStart($parameters['start']); |
||
| 333 | $criteria->setSort('product_title'); |
||
| 334 | $data = $this->getObjects($criteria, true); |
||
| 335 | |||
| 336 | return $data; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Retourne le nombre total de produits publiés dans la base en tenant compte des préférences du module |
||
| 341 | * |
||
| 342 | * @param int|int $product_cid Catégorie du produit |
||
| 343 | * @return int Le nombre de produits publiés |
||
| 344 | */ |
||
| 345 | public function getTotalPublishedProductsCount($product_cid = 0) |
||
| 346 | { |
||
| 347 | $criteria = new \CriteriaCompo(); |
||
| 348 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 349 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 350 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 351 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 352 | } |
||
| 353 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 354 | // Se limiter aux seuls produits encore en stock |
||
| 355 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 356 | } |
||
| 357 | if ($product_cid > 0) { |
||
| 358 | $criteria->add(new \Criteria('product_cid', (int)$product_cid, '=')); |
||
| 359 | } |
||
| 360 | |||
| 361 | return $this->getCount($criteria); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Récupération de l'ID et du titre d'une série de produits répondants à un critère |
||
| 366 | * |
||
| 367 | * @param null|\CriteriaElement|\CriteriaCompo $criteria critère de sélection |
||
| 368 | * @return array Tableau dont la clé = ID produit et la valeur le titre du produit |
||
| 369 | */ |
||
| 370 | public function getIdTitle($criteria = null) |
||
| 371 | { |
||
| 372 | global $myts; |
||
| 373 | $ret = []; |
||
| 374 | $sql = 'SELECT product_id, product_title FROM ' . $this->table; |
||
| 375 | if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 376 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 377 | if ('' !== $criteria->getSort()) { |
||
| 378 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 379 | } |
||
| 380 | $limit = $criteria->getLimit(); |
||
| 381 | $start = $criteria->getStart(); |
||
| 382 | } |
||
| 383 | $result = $this->db->query($sql, $limit, $start); |
||
| 384 | if (!$result) { |
||
| 385 | return $ret; |
||
| 386 | } |
||
| 387 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 388 | $ret[$myrow['product_id']] = $myts->htmlSpecialChars($myrow['product_title']); |
||
| 389 | } |
||
| 390 | |||
| 391 | return $ret; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Mise à jour du compteur de lectures du produit |
||
| 396 | * |
||
| 397 | * @param int $product_id L'identifiant du produit dont il faut mettre à jour le compteur de lectures |
||
| 398 | * @return bool Le résultat de la mise à jour |
||
| 399 | */ |
||
| 400 | public function addCounter($product_id) |
||
| 401 | { |
||
| 402 | $sql = 'UPDATE ' . $this->table . ' SET product_hits = product_hits + 1 WHERE product_id= ' . (int)$product_id; |
||
| 403 | |||
| 404 | // Note, pas de mise à jour du cache ! |
||
| 405 | return $this->db->queryF($sql); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Mise à jour de la notation d'un produit |
||
| 410 | * |
||
| 411 | * @param int $product_id Identifiant du produit |
||
| 412 | * @param float $rating la notation |
||
| 413 | * @param int $votes Le nombre de votes du produit |
||
| 414 | * @return bool Le résultat de la mise à jour |
||
| 415 | */ |
||
| 416 | public function updateRating($product_id, $rating, $votes) |
||
| 417 | { |
||
| 418 | $sql = 'UPDATE ' . $this->table . ' SET product_rating = ' . (int)$rating . ', product_votes = ' . (int)$votes . ' WHERE product_id = ' . (int)$product_id; |
||
| 419 | |||
| 420 | return $this->db->queryF($sql); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Mise à jour du nombre de commentaires d'un produit |
||
| 425 | * |
||
| 426 | * @param int $product_id Identifiant du produit |
||
| 427 | * @param int $commentsCount Nombre total de commentaires |
||
| 428 | */ |
||
| 429 | public function updateCommentsCount($product_id, $commentsCount) |
||
| 430 | { |
||
| 431 | $product = null; |
||
| 432 | $product = $this->get($product_id); |
||
| 433 | if (is_object($product)) { |
||
| 434 | $criteria = new \Criteria('product_id', $product_id, '='); |
||
| 435 | $this->updateAll('product_comments', $commentsCount, $criteria, true); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Retourne x produits au hasard |
||
| 441 | * |
||
| 442 | * @param Parameters $parameters |
||
| 443 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 444 | * @internal param int $start Début des données |
||
| 445 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 446 | * @internal param int $category Identifiant de la catégorie (évenutellement) |
||
| 447 | * @internal param string $sort Zone sur laquelle faire le tri |
||
| 448 | * @internal param string $order Ordre de tri |
||
| 449 | * @internal param bool $thisMonthOnly Uniquement les produits du mois en cours ? |
||
| 450 | */ |
||
| 451 | public function getRandomProducts(Parameters $parameters) |
||
| 452 | { |
||
| 453 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 454 | 'start' => 0, |
||
| 455 | 'limit' => 0, |
||
| 456 | 'category' => 0, |
||
| 457 | 'sort' => 'RAND()', |
||
| 458 | 'order' => 'ASC', |
||
| 459 | 'thisMonthOnly' => false, |
||
| 460 | ])); |
||
| 461 | $data = []; |
||
| 462 | $criteria = new \CriteriaCompo(); |
||
| 463 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 464 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 465 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 466 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 467 | } |
||
| 468 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 469 | // Se limiter aux seuls produits encore en stock |
||
| 470 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 471 | } |
||
| 472 | if (is_array($parameters['category'])) { |
||
| 473 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 474 | } elseif (0 != $parameters['category']) { |
||
| 475 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 476 | } |
||
| 477 | |||
| 478 | if ($parameters['thisMonthOnly']) { |
||
| 479 | $criteria->add(Oledrion\Utility::getThisMonthCriteria()); |
||
| 480 | } |
||
| 481 | |||
| 482 | $criteria->setLimit($parameters['limit']); |
||
| 483 | $criteria->setStart($parameters['start']); |
||
| 484 | $criteria->setSort($parameters['sort']); |
||
| 485 | $criteria->setOrder($parameters['order']); |
||
| 486 | $data = $this->getObjects($criteria, true); |
||
| 487 | |||
| 488 | return $data; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Retourne x produits en promo |
||
| 493 | * |
||
| 494 | * @param Parameters $parameters |
||
| 495 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 496 | * @internal param int $start Début des données |
||
| 497 | * @internal param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 498 | * @internal param int $category Identifiant de la catégorie (évenutellement) |
||
| 499 | */ |
||
| 500 | public function getPromotionalProducts(Parameters $parameters) |
||
| 501 | { |
||
| 502 | $parameters = $parameters->extend(new Oledrion\Parameters([ |
||
| 503 | 'start' => 0, |
||
| 504 | 'limit' => 0, |
||
| 505 | 'category' => 0, |
||
| 506 | 'sort' => 'product_title', |
||
| 507 | 'order' => 'DESC', |
||
| 508 | ])); |
||
| 509 | $data = []; |
||
| 510 | $criteria = new \CriteriaCompo(); |
||
| 511 | $criteria->add(new \Criteria('product_online', 1, '=')); |
||
| 512 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 513 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 514 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 515 | } |
||
| 516 | if (0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 517 | // Se limiter aux seuls produits encore en stock |
||
| 518 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 519 | } |
||
| 520 | if (is_array($parameters['category'])) { |
||
| 521 | $criteria->add(new \Criteria('product_cid', '(' . implode(',', $parameters['category']) . ')', 'IN')); |
||
| 522 | } elseif (0 != $parameters['category']) { |
||
| 523 | $criteria->add(new \Criteria('product_cid', (int)$parameters['category'], '=')); |
||
| 524 | } |
||
| 525 | $criteria->add(new \Criteria('product_discount_price', 0, '>')); |
||
| 526 | $criteria->setLimit($parameters['limit']); |
||
| 527 | $criteria->setStart($parameters['start']); |
||
| 528 | $criteria->setSort($parameters['sort']); |
||
| 529 | $criteria->setOrder($parameters['order']); |
||
| 530 | $data = $this->getObjects($criteria, true); |
||
| 531 | |||
| 532 | return $data; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Retourne les produits dont les stocks sont bas |
||
| 537 | * |
||
| 538 | * @param int $start Début des données |
||
| 539 | * @param int $limit Nombre maximum d'enregistrements à renvoyer |
||
| 540 | * @return array Tableau de produits (sous la forme d'objets) |
||
| 541 | */ |
||
| 542 | public function getLowStocks($start = 0, $limit = 0) |
||
| 543 | { |
||
| 544 | $ret = []; |
||
| 545 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE product_online = 1'; |
||
| 546 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 547 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 548 | $sql .= ' AND product_submitted <= ' . time(); |
||
| 549 | } |
||
| 550 | $sql .= ' AND product_stock <= product_alert_stock '; |
||
| 551 | $sql .= ' AND product_alert_stock > 0'; |
||
| 552 | $sql .= ' ORDER BY product_stock'; |
||
| 553 | $result = $this->db->query($sql, $limit, $start); |
||
| 554 | if (!$result) { |
||
| 555 | return $ret; |
||
| 556 | } |
||
| 557 | |||
| 558 | $ret = $this->convertResultSet($result, true, true); |
||
| 559 | |||
| 560 | return $ret; |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Retourne le nombre de produits dont la quantité en stock est inférieure ou égale à la quantité d'alerte |
||
| 565 | * |
||
| 566 | * @return array|int Le nombre de produits concernés |
||
| 567 | */ |
||
| 568 | public function getLowStocksCount() |
||
| 569 | { |
||
| 570 | $ret = []; |
||
| 571 | $sql = 'SELECT Count(*) AS cpt FROM ' . $this->table . ' WHERE product_online = 1'; |
||
| 572 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 573 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 574 | $sql .= ' AND product_submitted <= ' . time(); |
||
| 575 | } |
||
| 576 | $sql .= ' AND product_stock <= product_alert_stock '; |
||
| 577 | $sql .= ' AND product_alert_stock > 0'; |
||
| 578 | $result = $this->db->query($sql); |
||
| 579 | if (!$result) { |
||
| 580 | return $ret; |
||
| 581 | } |
||
| 582 | $count = 0; |
||
| 583 | list($count) = $this->db->fetchRow($result); |
||
| 584 | |||
| 585 | return $count; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Augmente les quantités en stock d'un produit |
||
| 590 | * |
||
| 591 | * @param Products $product Objet produit |
||
| 592 | * @param int $quantity $quantity Quantité à rajouter |
||
| 593 | * @return bool |
||
| 594 | */ |
||
| 595 | public function increaseStock($product, $quantity = 1) |
||
| 596 | { |
||
| 597 | $product->setVar('product_stock', $product->getVar('product_stock') + $quantity); |
||
| 598 | $this->insert($product, true); |
||
| 599 | |||
| 600 | return true; |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Diminue les quantités en stock d'un produit |
||
| 605 | * |
||
| 606 | * @param Products $product Objet produit |
||
| 607 | * @param int $quantity $quantity Quantité à soustraire |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | public function decreaseStock(&$product, $quantity = 1) |
||
| 611 | { |
||
| 612 | if ($product->getVar('product_stock') - $quantity > 0) { |
||
| 613 | $product->setVar('product_stock', $product->getVar('product_stock') - $quantity); |
||
| 614 | $this->insert($product, true); |
||
| 615 | } else { |
||
| 616 | $product->setVar('product_stock', 0); |
||
| 617 | } |
||
| 618 | |||
| 619 | return true; |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Indique si la quantité d'alerte d'un produit est atteinte |
||
| 624 | * |
||
| 625 | * @param Products $product |
||
| 626 | * @return bool Vrai si la quantité d'alerte est atteinte, sinon faux |
||
| 627 | * @internal param object $products L'objet produit concerné |
||
| 628 | */ |
||
| 629 | public function isAlertStockReached(Products $product) |
||
| 630 | { |
||
| 631 | return $product->getVar('product_stock') < $product->getVar('product_alert_stock'); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Méthode chargée de vérifier si le stock d'alerte est atteint et si c'est le cas, d'envoyer une alerte |
||
| 636 | * |
||
| 637 | * @param Products $product Produit dont il faut faire la vérification |
||
| 638 | * @return bool vrai si l'alerte à du être générée sinon faux |
||
| 639 | */ |
||
| 640 | public function verifyLowStock(&$product) |
||
| 641 | { |
||
| 642 | if ($this->isAlertStockReached($product)) { |
||
| 643 | $msg = []; |
||
| 644 | $msg['PRODUCT_NAME'] = $product->getVar('product_title'); |
||
| 645 | $msg['ACTUAL_QUANTITY'] = $product->getVar('product_stock'); |
||
| 646 | $msg['ALERT_QUANTITY'] = $product->getVar('product_alert_stock'); |
||
| 647 | $msg['PUBLIC_URL'] = $product->getLink(); |
||
| 648 | $msg['ADMIN_URL'] = OLEDRION_URL . 'admin/index.php?op=editproduct&id=' . $product->getVar('product_id'); |
||
| 649 | Oledrion\Utility::sendEmailFromTpl('shop_lowstock.tpl', Oledrion\Utility::getEmailsFromGroup(Oledrion\Utility::getModuleOption('stock_alert_email')), _OLEDRION_STOCK_ALERT, $msg); |
||
| 650 | |||
| 651 | return true; |
||
| 652 | } |
||
| 653 | |||
| 654 | return false; |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Retourne la plus petite date de création d'un produit ainsi que la "plus grande" date de création d'un produit |
||
| 659 | * |
||
| 660 | * @param int $minDate Date mini (parmètre de sortie) |
||
| 661 | * @param int $maxDate Date maxi (paramètre de sortie) |
||
| 662 | * @return bool Vrai si on a pu récupérer ces valeurs, faux sinon |
||
| 663 | */ |
||
| 664 | public function getMinMaxPublishedDate(&$minDate, &$maxDate) |
||
| 665 | { |
||
| 666 | $sql = 'SELECT Min(product_submitted) AS minDate, Max(product_submitted) AS maxDate FROM ' . $this->table . ' WHERE product_online = 1 '; |
||
| 667 | $result = $this->db->query($sql); |
||
| 668 | if (!$result) { |
||
| 669 | return false; |
||
| 670 | } |
||
| 671 | $myrow = $this->db->fetchArray($result); |
||
| 672 | $minDate = $myrow['minDate']; |
||
| 673 | $maxDate = $myrow['maxDate']; |
||
| 674 | |||
| 675 | return true; |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Retourne des produits en fonction de leur IDs tout en tenant compte du fait qu'ils sont en ligne et payés ! |
||
| 680 | * |
||
| 681 | * @param array $ids Les identifiants des produits |
||
| 682 | * @param bool $showAll Afficher les produits même s'ils ne sont plus en stock ? |
||
| 683 | * @return array Tableau d'objets de type Products, Clé = Id Produit |
||
| 684 | */ |
||
| 685 | public function getProductsFromIDs($ids, $showAll = false) |
||
| 686 | { |
||
| 687 | $ret = []; |
||
| 688 | if (is_array($ids)) { |
||
| 689 | $criteria = new \CriteriaCompo(); |
||
| 690 | if (0 == Oledrion\Utility::getModuleOption('show_unpublished')) { |
||
| 691 | // Ne pas afficher les produits qui ne sont pas publiés |
||
| 692 | $criteria->add(new \Criteria('product_submitted', time(), '<=')); |
||
| 693 | } |
||
| 694 | if (!$showAll && 0 == Oledrion\Utility::getModuleOption('nostock_display')) { |
||
| 695 | // Se limiter aux seuls produits encore en stock |
||
| 696 | $criteria->add(new \Criteria('product_stock', 0, '>')); |
||
| 697 | } |
||
| 698 | $criteria->add(new \Criteria('product_id', '(' . implode(',', $ids) . ')', 'IN')); |
||
| 699 | $ret = $this->getObjects($criteria, true, true, '*', false); |
||
| 700 | } |
||
| 701 | |||
| 702 | return $ret; |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Retourne le nombre de produits d'une ou de plusieurs catégories |
||
| 707 | * |
||
| 708 | * @param mixed $cat_cid Soit un ID de catégorie unique soit un tableau d'ID de catégories |
||
| 709 | * @return int Le nombre de produits associés à cette catégorie |
||
| 710 | */ |
||
| 711 | public function getCategoryProductsCount($cat_cid) |
||
| 712 | { |
||
| 713 | if (is_array($cat_cid)) { |
||
| 714 | $lst_ids = implode(',', $cat_cid); |
||
| 715 | $criteria = new \Criteria('product_cid', '(' . $lst_ids . ')', 'IN'); |
||
| 716 | } else { |
||
| 717 | $criteria = new \Criteria('product_cid', $cat_cid, '='); |
||
| 718 | } |
||
| 719 | |||
| 720 | return $this->getCount($criteria); |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Retourne le nombre de produits associés à un vendeur |
||
| 725 | * |
||
| 726 | * @param int $product_vendor_id L'ID du vendeur |
||
| 727 | * @return int Le nombre de produits |
||
| 728 | */ |
||
| 729 | public function getVendorProductsCount($product_vendor_id) |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Retourne le nombre de produits associés à une TVA |
||
| 738 | * |
||
| 739 | * @param int $product_vat_id L'identifiant de la TVA |
||
| 740 | * @return int Le nombre de produits |
||
| 741 | */ |
||
| 742 | public function getVatProductsCount($product_vat_id) |
||
| 743 | { |
||
| 744 | $criteria = new \Criteria('product_vat_id', $product_vat_id, '='); |
||
| 745 | |||
| 746 | return $this->getCount($criteria); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Clone d'un produit |
||
| 751 | * |
||
| 752 | * @param Products $originalProduct Le produit à cloner |
||
| 753 | * @return mixed Soit l'objet représentant le nouveau produit soit false |
||
| 754 | */ |
||
| 755 | public function cloneProduct(Products $originalProduct) |
||
| 756 | { |
||
| 757 | global $productsmanuHandler, $filesHandler, $productsmanuHandler, $relatedHandler, $oledrionHandlers; |
||
| 758 | $newProduct = $originalProduct->xoopsClone(); |
||
| 759 | if (OLEDRION_DUPLICATED_PLACE === 'right') { |
||
| 760 | $newProduct->setVar('product_title', $originalProduct->getVar('product_title') . ' ' . _AM_OLEDRION_DUPLICATED); |
||
| 761 | } else { |
||
| 762 | $newProduct->setVar('product_title', _AM_OLEDRION_DUPLICATED . ' ' . $originalProduct->getVar('product_title')); |
||
| 763 | } |
||
| 764 | $newProduct->setVar('product_id', 0); |
||
| 765 | $newProduct->setNew(); |
||
| 766 | |||
| 767 | // Copie des 2 images |
||
| 768 | if ('' !== xoops_trim($originalProduct->getVar('product_image_url'))) { |
||
| 769 | $resCopy = Oledrion\Utility::duplicateFile(OLEDRION_PICTURES_PATH, $originalProduct->getVar('product_image_url')); |
||
| 770 | if (false !== $resCopy) { |
||
| 771 | $newProduct->setVar('product_image_url', $resCopy); |
||
| 772 | } |
||
| 773 | } |
||
| 774 | if ('' !== xoops_trim($originalProduct->getVar('product_thumb_url'))) { |
||
| 775 | $resCopy = Oledrion\Utility::duplicateFile(OLEDRION_PICTURES_PATH, $originalProduct->getVar('product_thumb_url')); |
||
| 776 | if (false !== $resCopy) { |
||
| 777 | $newProduct->setVar('product_thumb_url', $resCopy); |
||
| 778 | } |
||
| 779 | } |
||
| 780 | |||
| 781 | // Copie du fichier attaché |
||
| 782 | if ('' !== xoops_trim($originalProduct->getVar('product_attachment'))) { |
||
| 783 | $resCopy = Oledrion\Utility::duplicateFile(OLEDRION_ATTACHED_FILES_PATH, $originalProduct->getVar('product_attachment')); |
||
| 784 | if (false !== $resCopy) { |
||
| 785 | $newProduct->setVar('product_attachment', $resCopy); |
||
| 786 | } |
||
| 787 | } |
||
| 788 | |||
| 789 | $res = $this->insert($newProduct, true); |
||
| 790 | if ($res) { |
||
| 791 | $newProductId = $newProduct->getVar('product_id'); |
||
| 792 | // Copie des fichiers liés |
||
| 793 | if ($filesHandler->getProductFilesCount($originalProduct->product_id) > 0) { |
||
| 794 | $attachedFiles = []; |
||
| 795 | $attachedFiles = $filesHandler->getProductFiles($originalProduct->product_id); |
||
| 796 | if (count($attachedFiles) > 0) { |
||
| 797 | foreach ($attachedFiles as $oneFile) { |
||
| 798 | $newAttachedFile = $oneFile->xoopsClone(); |
||
| 799 | $newAttachedFile->setVar('file_product_id', $newProductId); |
||
| 800 | $resCopy = Oledrion\Utility::duplicateFile(OLEDRION_ATTACHED_FILES_PATH, $oneFile->getVar('file_filename')); |
||
| 801 | if (false !== $resCopy) { |
||
| 802 | $newAttachedFile->setVar('file_filename', $resCopy); |
||
| 803 | } |
||
| 804 | $newAttachedFile->setNew(); |
||
| 805 | $filesHandler->insert($newAttachedFile, true); |
||
| 806 | } |
||
| 807 | } |
||
| 808 | } |
||
| 809 | |||
| 810 | // Copie des fabricants |
||
| 811 | $tblTmp = []; |
||
| 812 | $criteria = new \Criteria('pm_product_id', $originalProduct->getVar('product_id'), '='); |
||
| 813 | $tblTmp = $productsmanuHandler->getObjects($criteria); |
||
| 814 | foreach ($tblTmp as $productAuthor) { |
||
| 815 | $newProductAuthor = $productAuthor->xoopsClone(); |
||
| 816 | $newProductAuthor->setVar('pm_product_id', $newProductId); |
||
| 817 | $newProductAuthor->setVar('pm_id', 0); |
||
| 818 | $newProductAuthor->setNew(); |
||
| 819 | $productsmanuHandler->insert($newProductAuthor, true); |
||
| 820 | } |
||
| 821 | |||
| 822 | // Copie des produits relatifs |
||
| 823 | $tblTmp = []; |
||
| 824 | $criteria = new \Criteria('related_product_id', $originalProduct->getVar('product_id'), '='); |
||
| 825 | $tblTmp = $relatedHandler->getObjects($criteria); |
||
| 826 | foreach ($tblTmp as $related) { |
||
| 827 | $newRelated = $related->xoopsClone(); |
||
| 828 | $newRelated->setVar('related_product_id', $newProductId); |
||
| 829 | $newRelated->setVar('related_id', 0); |
||
| 830 | $newRelated->setNew(); |
||
| 831 | $relatedHandler->insert($newRelated, true); |
||
| 832 | } |
||
| 833 | |||
| 834 | // Copie des attributs |
||
| 835 | if ($attributesHandler->getProductAttributesCount($originalProduct->product_id) > 0) { |
||
| 836 | $criteria = new \Criteria('attribute_product_id', $originalProduct->product_id, '='); |
||
| 837 | $tblTmp = $attributesHandler->getObjects($criteria); |
||
| 838 | foreach ($tblTmp as $attribute) { |
||
| 839 | $newAttribute = $attribute->xoopsClone(); |
||
| 840 | $newAttribute->setVar('attribute_product_id', $newProductId); |
||
| 841 | $newAttribute->setVar('attribute_id', 0); |
||
| 842 | $newAttribute->setNew(); |
||
| 843 | $attributesHandler->insert($newAttribute, true); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | |||
| 847 | return $newProduct; |
||
| 848 | } |
||
| 849 | |||
| 850 | return false; |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Construit un sélecteur de produit(s) en fonction des paramètres et en tenant compte du nombre total de produits dans la base |
||
| 855 | * |
||
| 856 | * @todo : Remplacer les paramètres par un objet paramètre et/ou un tableau |
||
| 857 | * @param Parameters $parameters |
||
| 858 | * @return \XoopsFormElementTray|\XoopsFormSelect Retourne soit un objet de type tray <a href='psi_element://XoopsFormElementTray'>XoopsFormElementTray</a> soit un select <a href='psi_element://XoopsFormSelect'>XoopsFormSelect</a> |
||
| 859 | * @internal param string $caption Le titre du sélecteur |
||
| 860 | * @internal param string $name Le nom du champ qui receuille les produits |
||
| 861 | * @internal param mixed $value La valeur sélectionnées |
||
| 862 | * @internal param int $size Le nombre d'éléments visibles dans le sélecteur |
||
| 863 | * @internal param bool $multiple Indique si c'est un sélecteur multiple ou pas |
||
| 864 | * @internal param array $values Les valeurs sélectionnées ou les valeurs qui font le sélecteur |
||
| 865 | * @internal param bool $showAll Indique s'il faut voir tous les produits ou pas (pas publiés et en stock) |
||
| 866 | * @internal param string $sort Zone de tri |
||
| 867 | * @internal param string $order Ordre de tri |
||
| 868 | * @internal param string $formName Nom du formulaire |
||
| 869 | * @internal param string $description Description à rajouter à la zone |
||
| 870 | * @internal param mixed $withNull Option à rajouter en premier |
||
| 871 | */ |
||
| 872 | public function productSelector(Parameters $parameters) |
||
| 1000 | } |
||
| 1001 | } |
||
| 1002 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths