| Total Complexity | 126 |
| Total Lines | 809 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ItemHandler 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 ItemHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ItemHandler extends \XoopsPersistableObjectHandler |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var Publisher\Helper |
||
| 45 | */ |
||
| 46 | public $helper; |
||
| 47 | |||
| 48 | protected $resultCatCounts = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param null|\XoopsDatabase $db |
||
| 52 | */ |
||
| 53 | public function __construct(\XoopsDatabase $db = null) |
||
| 54 | { |
||
| 55 | /** @var Publisher\Helper $this->helper */ |
||
| 56 | parent::__construct($db, 'publisher_items', Item::class, 'itemid', 'title'); |
||
| 57 | $this->helper = Publisher\Helper::getInstance(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param bool $isNew |
||
| 62 | * |
||
| 63 | * @return \XoopsObject |
||
| 64 | */ |
||
| 65 | public function create($isNew = true) |
||
| 66 | { |
||
| 67 | $obj = parent::create($isNew); |
||
| 68 | if ($isNew) { |
||
| 69 | $obj->setDefaultPermissions(); |
||
|
|
|||
| 70 | } |
||
| 71 | |||
| 72 | return $obj; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * retrieve an item |
||
| 77 | * |
||
| 78 | * @param int $id itemid of the user |
||
| 79 | * |
||
| 80 | * @param null $fields |
||
| 81 | * @return mixed reference to the <a href='psi_element://Item'>Item</a> object, FALSE if failed |
||
| 82 | * object, FALSE if failed |
||
| 83 | */ |
||
| 84 | public function get($id = null, $fields = null) |
||
| 85 | { |
||
| 86 | $obj = parent::get($id); |
||
| 87 | if (is_object($obj)) { |
||
| 88 | $obj->assignOtherProperties(); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $obj; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * insert a new item in the database |
||
| 96 | * |
||
| 97 | * @param \XoopsObject $item reference to the {@link Item} |
||
| 98 | * object |
||
| 99 | * @param bool $force |
||
| 100 | * |
||
| 101 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 102 | */ |
||
| 103 | public function insert(\XoopsObject $item, $force = false) //insert(&$item, $force = false) |
||
| 104 | { |
||
| 105 | if (!$item->meta_keywords() || !$item->meta_description() || !$item->short_url()) { |
||
| 106 | $publisherMetagen = new Publisher\Metagen($item->getTitle(), $item->getVar('meta_keywords'), $item->getVar('summary')); |
||
| 107 | // Auto create meta tags if empty |
||
| 108 | if (!$item->meta_keywords()) { |
||
| 109 | $item->setVar('meta_keywords', $publisherMetagen->keywords); |
||
| 110 | } |
||
| 111 | if (!$item->meta_description()) { |
||
| 112 | $item->setVar('meta_description', $publisherMetagen->description); |
||
| 113 | } |
||
| 114 | // Auto create short_url if empty |
||
| 115 | if (!$item->short_url()) { |
||
| 116 | $item->setVar('short_url', mb_substr(Metagen::generateSeoTitle($item->getVar('title', 'n'), false), 0, 254)); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | if (!parent::insert($item, $force)) { |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | if (xoops_isActiveModule('tag')) { |
||
| 123 | // Storing tags information |
||
| 124 | $tagHandler = \XoopsModules\Tag\Helper::getInstance()->getHandler('Tag'); // xoops_getModuleHandler('tag', 'tag'); |
||
| 125 | $tagHandler->updateByItem($item->getVar('item_tag'), $item->getVar('itemid'), PUBLISHER_DIRNAME, 0); |
||
| 126 | } |
||
| 127 | |||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * delete an item from the database |
||
| 133 | * |
||
| 134 | * @param \XoopsObject $item reference to the ITEM to delete |
||
| 135 | * @param bool $force |
||
| 136 | * |
||
| 137 | * @return bool FALSE if failed. |
||
| 138 | */ |
||
| 139 | public function delete(\XoopsObject $item, $force = false) |
||
| 140 | { |
||
| 141 | // Deleting the files |
||
| 142 | if (!$this->helper->getHandler('File')->deleteItemFiles($item)) { |
||
| 143 | $item->setErrors(_AM_PUBLISHER_FILE_DELETE_ERROR); |
||
| 144 | } |
||
| 145 | if (!parent::delete($item, $force)) { |
||
| 146 | $item->setErrors(_AM_PUBLISHER_ITEM_DELETE_ERROR); |
||
| 147 | |||
| 148 | return false; |
||
| 149 | } |
||
| 150 | // Removing tags information |
||
| 151 | if (xoops_isActiveModule('tag')) { |
||
| 152 | $tagHandler = \XoopsModules\Tag\Helper::getInstance()->getHandler('Tag'); // xoops_getModuleHandler('tag', 'tag'); |
||
| 153 | $tagHandler->updateByItem('', $item->getVar('itemid'), PUBLISHER_DIRNAME, 0); |
||
| 154 | } |
||
| 155 | |||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * retrieve items from the database |
||
| 161 | * |
||
| 162 | * @param \CriteriaElement $criteria {@link CriteriaElement} |
||
| 163 | * conditions to be met |
||
| 164 | * @param bool|string $idKey what shall we use as array key ? none, itemid, categoryid |
||
| 165 | * @param bool $as_object |
||
| 166 | * @param string $notNullFields |
||
| 167 | * @return array array of <a href='psi_element://Item'>Item</a> objects |
||
| 168 | * objects |
||
| 169 | */ |
||
| 170 | |||
| 171 | public function &getObjects(\CriteriaElement $criteria = null, $idKey = 'none', $as_object = true, $notNullFields = null) |
||
| 172 | { |
||
| 173 | $limit = $start = 0; |
||
| 174 | $ret = []; |
||
| 175 | $notNullFields = (null !== $notNullFields) ?: ''; |
||
| 176 | |||
| 177 | $sql = 'SELECT * FROM ' . $this->db->prefix($this->helper->getDirname() . '_items'); |
||
| 178 | if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 179 | $whereClause = $criteria->renderWhere(); |
||
| 180 | if ('WHERE ()' !== $whereClause) { |
||
| 181 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 182 | if (!empty($notNullFields)) { |
||
| 183 | $sql .= $this->notNullFieldClause($notNullFields, true); |
||
| 184 | } |
||
| 185 | } elseif (!empty($notNullFields)) { |
||
| 186 | $sql .= ' WHERE ' . $this->notNullFieldClause($notNullFields); |
||
| 187 | } |
||
| 188 | if ('' != $criteria->getSort()) { |
||
| 189 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 190 | } |
||
| 191 | $limit = $criteria->getLimit(); |
||
| 192 | $start = $criteria->getStart(); |
||
| 193 | } elseif (!empty($notNullFields)) { |
||
| 194 | $sql .= $sql .= ' WHERE ' . $this->notNullFieldClause($notNullFields); |
||
| 195 | } |
||
| 196 | $result = $this->db->query($sql, $limit, $start); |
||
| 197 | if (!$result || 0 === $GLOBALS['xoopsDB']->getRowsNum($result)) { |
||
| 198 | return $ret; |
||
| 199 | } |
||
| 200 | $theObjects = []; |
||
| 201 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 202 | $item = new Item(); |
||
| 203 | $item->assignVars($myrow); |
||
| 204 | $theObjects[$myrow['itemid']] = $item; |
||
| 205 | unset($item); |
||
| 206 | } |
||
| 207 | foreach ($theObjects as $theObject) { |
||
| 208 | if ('none' === $idKey) { |
||
| 209 | $ret[] = $theObject; |
||
| 210 | } elseif ('itemid' === $idKey) { |
||
| 211 | $ret[$theObject->itemid()] = $theObject; |
||
| 212 | } else { |
||
| 213 | $ret[$theObject->getVar($idKey)][$theObject->itemid()] = $theObject; |
||
| 214 | } |
||
| 215 | unset($theObject); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $ret; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * count items matching a condition |
||
| 223 | * |
||
| 224 | * @param \CriteriaElement $criteria {@link CriteriaElement} |
||
| 225 | * to match |
||
| 226 | * @param string $notNullFields |
||
| 227 | * |
||
| 228 | * @return int count of items |
||
| 229 | */ |
||
| 230 | public function getCount(\CriteriaElement $criteria = null, $notNullFields = '') |
||
| 231 | { |
||
| 232 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($this->helper->getDirname() . '_items'); |
||
| 233 | if (null !== $criteria && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 234 | $whereClause = $criteria->renderWhere(); |
||
| 235 | if ('WHERE ()' !== $whereClause) { |
||
| 236 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 237 | if (!empty($notNullFields)) { |
||
| 238 | $sql .= $this->notNullFieldClause($notNullFields, true); |
||
| 239 | } |
||
| 240 | } elseif (!empty($notNullFields)) { |
||
| 241 | $sql .= ' WHERE ' . $this->notNullFieldClause($notNullFields); |
||
| 242 | } |
||
| 243 | } elseif (!empty($notNullFields)) { |
||
| 244 | $sql .= ' WHERE ' . $this->notNullFieldClause($notNullFields); |
||
| 245 | } |
||
| 246 | $result = $this->db->query($sql); |
||
| 247 | if (!$result) { |
||
| 248 | return 0; |
||
| 249 | } |
||
| 250 | list($count) = $this->db->fetchRow($result); |
||
| 251 | |||
| 252 | return $count; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param int $categoryid |
||
| 257 | * @param string|array $status |
||
| 258 | * @param string $notNullFields |
||
| 259 | * @param $criteriaPermissions |
||
| 260 | * @return \CriteriaCompo |
||
| 261 | */ |
||
| 262 | private function getItemsCriteria($categoryid = -1, $status = '', $notNullFields = '', $criteriaPermissions) |
||
| 263 | { |
||
| 264 | // global $publisherIsAdmin; |
||
| 265 | // $ret = 0; |
||
| 266 | // if (!$publisherIsAdmin) { |
||
| 267 | // $criteriaPermissions = new \CriteriaCompo(); |
||
| 268 | // // Categories for which user has access |
||
| 269 | // $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read'); |
||
| 270 | // if (!empty($categoriesGranted)) { |
||
| 271 | // $grantedCategories = new \Criteria('categoryid', "(" . implode(',', $categoriesGranted) . ")", 'IN'); |
||
| 272 | // $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 273 | // } else { |
||
| 274 | // return $ret; |
||
| 275 | // } |
||
| 276 | // } |
||
| 277 | if (null !== $categoryid && -1 != $categoryid) { |
||
| 278 | $criteriaCategory = new \Criteria('categoryid', $categoryid); |
||
| 279 | } |
||
| 280 | $criteriaStatus = new \CriteriaCompo(); |
||
| 281 | if (!empty($status) && is_array($status)) { |
||
| 282 | foreach ($status as $v) { |
||
| 283 | $criteriaStatus->add(new \Criteria('status', $v), 'OR'); |
||
| 284 | } |
||
| 285 | } elseif (!empty($status) && -1 != $status) { |
||
| 286 | $criteriaStatus->add(new \Criteria('status', $status), 'OR'); |
||
| 287 | } |
||
| 288 | $criteria = new \CriteriaCompo(); |
||
| 289 | if (!empty($criteriaCategory)) { |
||
| 290 | $criteria->add($criteriaCategory); |
||
| 291 | } |
||
| 292 | if (!empty($criteriaPermissions)) { |
||
| 293 | $criteria->add($criteriaPermissions); |
||
| 294 | } |
||
| 295 | if (!empty($criteriaStatus)) { |
||
| 296 | $criteria->add($criteriaStatus); |
||
| 297 | } |
||
| 298 | |||
| 299 | return $criteria; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $categoryid |
||
| 304 | * @param string $status |
||
| 305 | * @param string $notNullFields |
||
| 306 | * |
||
| 307 | * @return int |
||
| 308 | */ |
||
| 309 | public function getItemsCount($categoryid = -1, $status = '', $notNullFields = '') |
||
| 310 | { |
||
| 311 | |||
| 312 | // global $publisherIsAdmin; |
||
| 313 | $criteriaPermissions = ''; |
||
| 314 | if (!$GLOBALS['publisherIsAdmin']) { |
||
| 315 | $criteriaPermissions = new \CriteriaCompo(); |
||
| 316 | // Categories for which user has access |
||
| 317 | $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read'); |
||
| 318 | if (!empty($categoriesGranted)) { |
||
| 319 | $grantedCategories = new \Criteria('categoryid', '(' . implode(',', $categoriesGranted) . ')', 'IN'); |
||
| 320 | $criteriaPermissions->add($grantedCategories, 'AND'); |
||
| 321 | } else { |
||
| 322 | return 0; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | // $ret = array(); |
||
| 326 | $criteria = $this->getItemsCriteria($categoryid, $status, $notNullFields, $criteriaPermissions); |
||
| 327 | /* |
||
| 328 | if (isset($categoryid) && $categoryid != -1) { |
||
| 329 | $criteriaCategory = new \Criteria('categoryid', $categoryid); |
||
| 330 | } |
||
| 331 | $criteriaStatus = new \CriteriaCompo(); |
||
| 332 | if (!empty($status) && is_array($status)) { |
||
| 333 | foreach ($status as $v) { |
||
| 334 | $criteriaStatus->add(new \Criteria('status', $v), 'OR'); |
||
| 335 | } |
||
| 336 | } elseif (!empty($status) && $status != -1) { |
||
| 337 | $criteriaStatus->add(new \Criteria('status', $status), 'OR'); |
||
| 338 | } |
||
| 339 | $criteria = new \CriteriaCompo(); |
||
| 340 | if (!empty($criteriaCategory)) { |
||
| 341 | $criteria->add($criteriaCategory); |
||
| 342 | } |
||
| 343 | if (!empty($criteriaPermissions)) { |
||
| 344 | $criteria->add($criteriaPermissions); |
||
| 345 | } |
||
| 346 | if (!empty($criteriaStatus)) { |
||
| 347 | $criteria->add($criteriaStatus); |
||
| 348 | } |
||
| 349 | */ |
||
| 350 | $ret = $this->getCount($criteria, $notNullFields); |
||
| 351 | |||
| 352 | return $ret; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param int $limit |
||
| 357 | * @param int $start |
||
| 358 | * @param int $categoryid |
||
| 359 | * @param string $sort |
||
| 360 | * @param string $order |
||
| 361 | * @param string $notNullFields |
||
| 362 | * @param bool $asObject |
||
| 363 | * @param string $idKey |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | public function getAllPublished($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asObject = true, $idKey = 'none') |
||
| 368 | { |
||
| 369 | $otherCriteria = new \Criteria('datesub', time(), '<='); |
||
| 370 | |||
| 371 | return $this->getItems($limit, $start, [Constants::PUBLISHER_STATUS_PUBLISHED], $categoryid, $sort, $order, $notNullFields, $asObject, $otherCriteria, $idKey); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param Item $obj |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function getPreviousPublished($obj) |
||
| 380 | { |
||
| 381 | $ret = false; |
||
| 382 | $otherCriteria = new \CriteriaCompo(); |
||
| 383 | $otherCriteria->add(new \Criteria('datesub', $obj->getVar('datesub'), '<')); |
||
| 384 | $objs = $this->getItems(1, 0, [Constants::PUBLISHER_STATUS_PUBLISHED], $obj->getVar('categoryid'), 'datesub', 'DESC', '', true, $otherCriteria, 'none'); |
||
| 385 | if (count($objs) > 0) { |
||
| 386 | $ret = $objs[0]; |
||
| 387 | } |
||
| 388 | |||
| 389 | return $ret; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Item $obj |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function getNextPublished($obj) |
||
| 398 | { |
||
| 399 | $ret = false; |
||
| 400 | $otherCriteria = new \CriteriaCompo(); |
||
| 401 | $otherCriteria->add(new \Criteria('datesub', $obj->getVar('datesub'), '>')); |
||
| 402 | $otherCriteria->add(new \Criteria('datesub', time(), '<=')); |
||
| 403 | $objs = $this->getItems(1, 0, [Constants::PUBLISHER_STATUS_PUBLISHED], $obj->getVar('categoryid'), 'datesub', 'ASC', '', true, $otherCriteria, 'none'); |
||
| 404 | if (count($objs) > 0) { |
||
| 405 | $ret = $objs[0]; |
||
| 406 | } |
||
| 407 | |||
| 408 | return $ret; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param int $limit |
||
| 413 | * @param int $start |
||
| 414 | * @param int $categoryid |
||
| 415 | * @param string $sort |
||
| 416 | * @param string $order |
||
| 417 | * @param string $notNullFields |
||
| 418 | * @param bool $asObject |
||
| 419 | * @param string $idKey |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getAllSubmitted($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asObject = true, $idKey = 'none') |
||
| 424 | { |
||
| 425 | return $this->getItems($limit, $start, [Constants::PUBLISHER_STATUS_SUBMITTED], $categoryid, $sort, $order, $notNullFields, $asObject, null, $idKey); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param int $limit |
||
| 430 | * @param int $start |
||
| 431 | * @param int $categoryid |
||
| 432 | * @param string $sort |
||
| 433 | * @param string $order |
||
| 434 | * @param string $notNullFields |
||
| 435 | * @param bool $asObject |
||
| 436 | * @param string $idKey |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | public function getAllOffline($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asObject = true, $idKey = 'none') |
||
| 441 | { |
||
| 442 | return $this->getItems($limit, $start, [Constants::PUBLISHER_STATUS_OFFLINE], $categoryid, $sort, $order, $notNullFields, $asObject, null, $idKey); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param int $limit |
||
| 447 | * @param int $start |
||
| 448 | * @param int $categoryid |
||
| 449 | * @param string $sort |
||
| 450 | * @param string $order |
||
| 451 | * @param string $notNullFields |
||
| 452 | * @param bool $asObject |
||
| 453 | * @param string $idKey |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | public function getAllRejected($limit = 0, $start = 0, $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asObject = true, $idKey = 'none') |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param int $limit |
||
| 464 | * @param int $start |
||
| 465 | * @param array|string $status |
||
| 466 | * @param int $categoryid |
||
| 467 | * @param string $sort |
||
| 468 | * @param string $order |
||
| 469 | * @param string $notNullFields |
||
| 470 | * @param bool $asObject |
||
| 471 | * @param null $otherCriteria |
||
| 472 | * @param bool|string $idKey |
||
| 473 | * @return array |
||
| 474 | * @internal param bool $asObject |
||
| 475 | */ |
||
| 476 | public function getItems($limit = 0, $start = 0, $status = '', $categoryid = -1, $sort = 'datesub', $order = 'DESC', $notNullFields = '', $asObject = true, $otherCriteria = null, $idKey = 'none') |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $field |
||
| 532 | * @param string $status |
||
| 533 | * @param int $categoryId |
||
| 534 | * |
||
| 535 | * @return bool |
||
| 536 | */ |
||
| 537 | public function getRandomItem($field = '', $status = '', $categoryId = -1) |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * delete Items matching a set of conditions |
||
| 558 | * |
||
| 559 | * @param \CriteriaElement $criteria {@link CriteriaElement} |
||
| 560 | * |
||
| 561 | * @param bool $force |
||
| 562 | * @param bool $asObject |
||
| 563 | * @return bool FALSE if deletion failed |
||
| 564 | */ |
||
| 565 | public function deleteAll(\CriteriaElement $criteria = null, $force = true, $asObject = false) //deleteAll($criteria = null) |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param $itemid |
||
| 578 | * |
||
| 579 | * @return bool |
||
| 580 | */ |
||
| 581 | public function updateCounter($itemid) |
||
| 582 | { |
||
| 583 | $sql = 'UPDATE ' . $this->db->prefix($this->helper->getDirname() . '_items') . ' SET counter=counter+1 WHERE itemid = ' . $itemid; |
||
| 584 | if ($this->db->queryF($sql)) { |
||
| 585 | return true; |
||
| 586 | } |
||
| 587 | return false; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param string|array $notNullFields |
||
| 592 | * @param bool $withAnd |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function notNullFieldClause($notNullFields = '', $withAnd = false) |
||
| 597 | { |
||
| 598 | $ret = ''; |
||
| 599 | if ($withAnd) { |
||
| 600 | $ret .= ' AND '; |
||
| 601 | } |
||
| 602 | if (!empty($notNullFields) && is_array($notNullFields)) { |
||
| 603 | foreach ($notNullFields as $v) { |
||
| 604 | $ret .= " ($v IS NOT NULL AND $v <> ' ' )"; |
||
| 605 | } |
||
| 606 | } elseif (!empty($notNullFields)) { |
||
| 607 | $ret .= " ($notNullFields IS NOT NULL AND $notNullFields <> ' ' )"; |
||
| 608 | } |
||
| 609 | |||
| 610 | return $ret; |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param array $queryArray |
||
| 615 | * @param string $andor |
||
| 616 | * @param int $limit |
||
| 617 | * @param int $offset |
||
| 618 | * @param int $userid |
||
| 619 | * @param array $categories |
||
| 620 | * @param int $sortby |
||
| 621 | * @param string|array $searchin |
||
| 622 | * @param string $extra |
||
| 623 | * |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | public function getItemsFromSearch($queryArray = [], $andor = 'AND', $limit = 0, $offset = 0, $userid = 0, $categories = [], $sortby = 0, $searchin = '', $extra = '') |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @param array $categoriesObj |
||
| 723 | * @param array $status |
||
| 724 | * |
||
| 725 | * @return array |
||
| 726 | */ |
||
| 727 | public function getLastPublishedByCat($categoriesObj, $status = [Constants::PUBLISHER_STATUS_PUBLISHED]) |
||
| 728 | { |
||
| 729 | $ret = []; |
||
| 730 | $catIds = []; |
||
| 731 | foreach ($categoriesObj as $parentid) { |
||
| 732 | foreach ($parentid as $category) { |
||
| 733 | $catId = $category->getVar('categoryid'); |
||
| 734 | $catIds[$catId] = $catId; |
||
| 735 | } |
||
| 736 | } |
||
| 737 | if (empty($catIds)) { |
||
| 738 | return $ret; |
||
| 739 | } |
||
| 740 | /*$cat = array(); |
||
| 741 | |||
| 742 | $sql = "SELECT categoryid, MAX(datesub) as date FROM " . $this->db->prefix($this->helper->getDirname() . '_items') . " WHERE status IN (" . implode(',', $status) . ") GROUP BY categoryid"; |
||
| 743 | $result = $this->db->query($sql); |
||
| 744 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 745 | $cat[$row['categoryid']] = $row['date']; |
||
| 746 | } |
||
| 747 | if (count($cat) == 0) return $ret; |
||
| 748 | $sql = "SELECT categoryid, itemid, title, short_url, uid, datesub FROM " . $this->db->prefix($this->helper->getDirname() . '_items'); |
||
| 749 | $criteriaBig = new \CriteriaCompo(); |
||
| 750 | foreach ($cat as $id => $date) { |
||
| 751 | $criteria = new \CriteriaCompo(new \Criteria('categoryid', $id)); |
||
| 752 | $criteria->add(new \Criteria('datesub', $date)); |
||
| 753 | $criteriaBig->add($criteria, 'OR'); |
||
| 754 | unset($criteria); |
||
| 755 | } |
||
| 756 | $sql .= " " . $criteriaBig->renderWhere(); |
||
| 757 | $result = $this->db->query($sql); |
||
| 758 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 759 | $item = new Item(); |
||
| 760 | $item->assignVars($row); |
||
| 761 | $ret[$row['categoryid']] = $item; |
||
| 762 | unset($item); |
||
| 763 | } |
||
| 764 | */ |
||
| 765 | $sql = 'SELECT mi.categoryid, mi.itemid, mi.title, mi.short_url, mi.uid, mi.datesub'; |
||
| 766 | $sql .= ' FROM (SELECT categoryid, MAX(datesub) AS date FROM ' . $this->db->prefix($this->helper->getDirname() . '_items'); |
||
| 767 | $sql .= ' WHERE status IN (' . implode(',', $status) . ')'; |
||
| 768 | $sql .= ' AND categoryid IN (' . implode(',', $catIds) . ')'; |
||
| 769 | $sql .= ' GROUP BY categoryid)mo'; |
||
| 770 | $sql .= ' JOIN ' . $this->db->prefix($this->helper->getDirname() . '_items') . ' mi ON mi.datesub = mo.date'; |
||
| 771 | $result = $this->db->query($sql); |
||
| 772 | while (false !== ($row = $this->db->fetchArray($result))) { |
||
| 773 | $item = new Item(); |
||
| 774 | $item->assignVars($row); |
||
| 775 | $ret[$row['categoryid']] = $item; |
||
| 776 | unset($item); |
||
| 777 | } |
||
| 778 | |||
| 779 | return $ret; |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @param $parentid |
||
| 784 | * @param $catsCount |
||
| 785 | * @param string $spaces |
||
| 786 | * @return int |
||
| 787 | */ |
||
| 788 | public function countArticlesByCat($parentid, $catsCount, $spaces = '') |
||
| 789 | { |
||
| 790 | // global $resultCatCounts; |
||
| 791 | $newspaces = $spaces . '--'; |
||
| 792 | $thecount = 0; |
||
| 793 | foreach ($catsCount[$parentid] as $subCatId => $count) { |
||
| 794 | $thecount += $count; |
||
| 795 | $this->resultCatCounts[$subCatId] = $count; |
||
| 796 | if (isset($catsCount[$subCatId])) { |
||
| 797 | $thecount += $this->countArticlesByCat($subCatId, $catsCount, $newspaces); |
||
| 798 | $this->resultCatCounts[$subCatId] = $thecount; |
||
| 799 | } |
||
| 800 | } |
||
| 801 | |||
| 802 | return $thecount; |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @param int $catId |
||
| 807 | * @param array $status |
||
| 808 | * @param bool $inSubCat |
||
| 809 | * |
||
| 810 | * @return array |
||
| 811 | */ |
||
| 812 | public function getCountsByCat($catId = 0, $status, $inSubCat = false) |
||
| 850 | } |
||
| 851 | } |
||
| 852 |