• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/item.php
  • Класс: BitrixTasksItem
  • Вызов: Item::find
static function find(array $parameters = array(), $settings = null)
{
	if(!is_array($settings))
	{
		$settings = array();
	}
	if(!(int)($settings['USER_ID'] ?? null))
	{
		$settings['USER_ID'] = User::getId();
	}

	$dc = static::getDataSourceClass();
	$dcParams = array_intersect_key(
		$parameters,
		array('filter' => 1, 'select' => 1, 'order' => 1, 'limit' => 1, 'offset' => 1, 'count_total' => 1)
	);

	// todo: filter select key carefully here!!! DO NOT query fields that have DB_READABLE == false, and also
	// todo: care about SOURCE == Scalar::SOURCE_CUSTOM here!

	// todo: this is the default access controller, we could specify our own in $settings and use here
	$ac = static::getAccessControllerDefault();

	$items = array();
	$result = static::getCollectionInstance();

	$parameters = $ac->addDataBaseAccessCheck(
		$dcParams,
		array(
			'USER_ID' => $settings['USER_ID'],
		)
	);

	// catch some exceptions came from orm, and wrap it into a error
	try
	{
		$res = $dc::getList($parameters);
	}
	catch(SystemException $e) // orm throws common SystemException, which is not good, but we cant do anything
	{
		if($e->getCode() == 100) // errors like "Unknown field"
		{
			$message = $e->getMessage();
			$found = array();
			$data = array();
			if(preg_match('#Unknown field definition `([a-zA-Z0-9_]+)`#', $message, $found) && $found[1] && mb_strlen($found[1]))
			{
				$message = Loc::getMessage('TASKS_ITEM_UNKNOWN_FIELD', array('FIELD_NAME' => $found[1]));
				$data = array('FIELD_NAME' => $found[1]);
			}

			$result->addError('UNKNOWN_FIELD', $message, Error::TYPE_FATAL, $data);
			$res = null;
		}
		else
		{
			throw $e;
		}
	}

	if($res)
	{
		while($item = $res->fetch())
		{
			// todo: in $settings we could have RETURN_TYPE field: return item collection or array-of-array

			$items[] = static::makeInstanceFromSource($item, $settings['USER_ID']);
		}
		$result->set($items);
	}

	return $result;
}