/** * TASK performs with ACTION = INSERT * * @param int $limit * @return string */ public static function taskInsertElement($limit = 500) { if (($limit = (int) $limit) < 0) { return; } $iblockElement = new \CIBlockElement(); $connection = \Bitrix\Main\Application::getConnection(); $sqlHelper = $connection->getSqlHelper(); // We get a task with the `ACTION` of `INSERT` $sql = "\n SELECT t1.ID as TASK_ID, t1.PARAMS, t1.FILTER_USER_ID, t3.*\n FROM b_citfact_filter_subscribe_stack as t1\n LEFT JOIN b_citfact_filter_subscribe_user as t2 ON t2.ID = t1.FILTER_USER_ID\n LEFT JOIN b_citfact_filter_subscribe as t3 ON t3.ID = t2.FILTER_USER\n WHERE t1.ACTION = 'INSERT' AND t3.ACTIVE = 'N'\n LIMIT 1\n "; $task = (array) $connection->query($sql)->fetch(); if (!empty($task)) { $filter['IBLOCK_ID'] = $task['IBLOCK_ID']; if ($task['SECTION_ID'] > 0) { $filter['SECTION_ID'] = $task['SECTION_ID']; } $filter = array_merge($filter, unserialize($task['FILTER'])); $elementResult = $iblockElement->GetList(array('ID' => 'ASC'), $filter, false, array('nTopCount' => $limit), array('ID')); while ($element = $elementResult->fetch()) { Model\SubscribeNotifyTable::add(array('FILTER_USER_ID' => $task['FILTER_USER_ID'], 'ELEMENT_ID' => $element['ID'])); } Model\SubscribeStackTable::delete(array('ID' => $task['TASK_ID'])); } return "Citfact\\FilterSubscribe\\Agent::taskInsertElement({$limit})"; }
/** * Remove filter user and relationships * * @param int $id * @return \Bitrix\Main\Entity\DeleteResult * @throws \InvalidArgumentException */ public function removeFilterUser($id) { $filterUser = Model\SubscribeUserTable::getById($id)->fetch(); if ($this->isEmptyResult($filterUser)) { throw new \InvalidArgumentException('Invalid filter user id'); } $queryBuilder = new Entity\Query(Model\SubscribeStackTable::getEntity()); $subscribeStack = $queryBuilder->setSelect(array('ID'))->setFilter(array('FILTER_USER_ID' => $filterUser['ID']))->exec(); while ($row = $subscribeStack->fetch()) { Model\SubscribeStackTable::delete(array('ID' => $row['ID'])); } $result = Model\SubscribeUserTable::delete(array('ID' => $filterUser['ID'])); $queryBuilder = new Entity\Query(Model\SubscribeUserTable::getEntity()); $filterUserResult = $queryBuilder->registerRuntimeField('cnt', array('data_type' => 'integer', 'expression' => array('count(%s)', 'ID')))->setSelect(array('ID', 'cnt'))->setFilter(array('FILTER_ID' => $filterUser['FILTER_ID']))->exec()->fetch(); if ($filterUserResult['cnt'] <= 0) { Model\SubscribeTable::delete(array('ID' => $filterUser['FILTER_ID'])); } return $result; }