/**
  * Method searches for a custom filter that, needs updating (notifications of new elements).
  * Custom filters are added to the stack of tasks.
  *
  * @param int $limitFilter
  * @return string
  */
 public static function findUserFilterNotify($limitFilter = 1, $limitFilterUser = 100)
 {
     $iblockElement = new \CIBlockElement();
     $connection = \Bitrix\Main\Application::getConnection();
     $sqlHelper = $connection->getSqlHelper();
     $limitFilter = (int) $limitFilter;
     $limitFilterUser = (int) $limitFilterUser;
     if ($limitFilterUser <= 0 || $limitFilterUser <= 0) {
         return;
     }
     $queryBuilder = new Entity\Query(Model\SubscribeTable::getEntity());
     $filterResult = $queryBuilder->setSelect(array('*'))->setFilter(array('ACTIVE' => 'Y'))->setLimit($limitFilter)->exec();
     while ($filterRow = $filterResult->fetch()) {
         $filter['IBLOCK_ID'] = $filterRow['IBLOCK_ID'];
         if ($filterRow['SECTION_ID'] > 0) {
             $filter['SECTION_ID'] = $filterRow['SECTION_ID'];
         }
         $filter = array_merge($filter, unserialize($filterRow['FILTER']));
         $elementResult = $iblockElement->GetList(false, $filter, array('IBLOCK_ID'))->fetch();
         if ($elementResult['CNT'] <= 0) {
             continue;
         }
         // Choose filters which notification less,
         // than the number of elements with filter
         $sql = "\n                SELECT DISTINCT t1.ID\n                FROM b_citfact_filter_subscribe_user as t1\n                LEFT OUTER JOIN b_citfact_filter_subscribe_notify as t2 ON (\n                    t1.ID = t2.FILTER_USER_ID AND\n                    t1.FILTER_ID = '" . $sqlHelper->forSql($filterRow['ID']) . "'\n                )\n                WHERE (SELECT count(ID) FROM b_citfact_filter_subscribe_notify as t3 WHERE t3.FILTER_USER_ID = t1.ID) < '" . $sqlHelper->forSql($elementResult['CNT']) . "'\n                GROUP BY t1.ID\n                LIMIT 0," . $limitFilterUser . "\n            ";
         // Adding custom filters to stack table
         $notifyList = (array) $connection->query($sql)->fetchAll();
         foreach ($notifyList as $key => $notify) {
             Model\SubscribeStackTable::add(array('FILTER_USER_ID' => $notify['ID'], 'ACTION' => 'UPDATE'));
         }
         // If list for stack table is not empty, then deactivate filter
         if (sizeof($notifyList) > 0) {
             Model\SubscribeTable::update($filterRow['ID'], array('ACTIVE' => 'N'));
         }
     }
     return "Citfact\\FilterSubscribe\\Agent::findUserFilterNotify({$limitFilter})";
 }
 /**
  * Save link user filter with filter
  * Requirement fields: USER_ID, FILTER_ID
  *
  * @param array $fields
  * @return \Bitrix\Main\Entity\AddResult
  * @throws \InvalidArgumentException
  */
 public function addFilterUser(array $fields)
 {
     $userId = $fields['USER_ID'];
     $userData = \CUser::GetById($userId)->GetNext();
     if ($this->isEmptyResult($userData)) {
         throw new \InvalidArgumentException('Invalid USER_ID');
     }
     $queryBuilder = new Entity\Query(Model\SubscribeTable::getEntity());
     $subscribe = $queryBuilder->setSelect(array('ID'))->setFilter(array('ID' => $fields['FILTER_ID']))->exec()->fetch();
     if ($this->isEmptyResult($subscribe)) {
         throw new \InvalidArgumentException('Invalid FILTER_ID');
     }
     $filterUserData = Model\SubscribeUserTable::getList(array('select' => array('ID'), 'filter' => array('USER_ID' => $userId, 'FILTER_ID' => $subscribe['ID'])))->fetch();
     if (!$this->isEmptyResult($filterUserData)) {
         $addResult = new \Bitrix\Main\Entity\AddResult();
         $addResult->setId($filterUserData['ID']);
         return $addResult;
     }
     $filterUserResult = Model\SubscribeUserTable::add(array('USER_ID' => $userData['ID'], 'FILTER_ID' => $subscribe['ID']));
     if ($filterUserResult->isSuccess()) {
         Model\SubscribeStackTable::add(array('FILTER_USER_ID' => $filterUserResult->getId(), 'ACTION' => 'INSERT'));
     }
     return $filterUserResult;
 }