• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/manager/task/checklist.php
  • Класс: Bitrix\Tasks\Manager\Task\CheckList
  • Вызов: CheckList::manageSet
static function manageSet($userId, $taskId, array $items = array(), array $parameters = array('PUBLIC_MODE' => false, 'MODE' => self::MODE_ADD))
{
	$errors = static::ensureHaveErrorCollection($parameters);
	$result = array(
		'DATA' => array(),
		'CAN' => array(),
		'ERRORS' => $errors
	);

	if(!static::checkSetPassed($items, $parameters['MODE']))
	{
		return $result;
	}

	$task = static::getTask($userId, $taskId);

	$data = array();

	// todo: when we edit task with rights loose, this condition will make troubles. temporary commented out
	/*
	if($parameters['MODE'] != self::MODE_ADD && !$task->isActionAllowed(\CTaskItem::ACTION_EDIT))
	{
		throw new \Bitrix\Tasks\ActionNotAllowedException();
	}
	*/

	// if you can edit the task, then you can move checklist items or add new ones.
	// optionally you can rename\toggle\delete if allowed by certain item`s permissions

	$currentItems = array('DATA' => array());
	if($parameters['MODE'] == static::MODE_UPDATE) // update existing
	{
		$currentItems = static::getListByParentEntity($userId, $taskId, $parameters);
	}

	$items = 					static::indexItemSets($items);
	$currentItems['DATA'] = 	static::indexItemSets($currentItems['DATA']);

	list($toAdd, $toUpdate, $toDelete) = static::makeDeltaSets($items, $currentItems['DATA']);
	if(empty($toAdd) && empty($toUpdate) && empty($toDelete))
	{
		return $result;
	}

	$toToggle = static::makeToggleSet($items, $currentItems['DATA']);

	// ensure we can do all the operations
	$itemName = Loc::getMessage('TASKS_MANAGER_TASK_CHECKLIST_ITEM_NAME');

	static::ensureCanToggle($toToggle, $currentItems, $errors);
	static::ensureCanUpdate($toUpdate, $currentItems, $errors, $itemName);
	static::ensureCanDelete($toDelete, $currentItems, $errors, $itemName);

	if($errors->checkNoFatals())
	{
		$items = static::reArrangeBySortIndex($items); // do not rely on request item order, it may be broken

		foreach($toDelete as $index)
		{
			$itemInst = new \CTaskCheckListItem($task, $index);
			$itemInst->delete();
		}

		// also re-order items here
		$sortIndex = 0;

		$toAdd = array_flip($toAdd);
		$toUpdate = array_flip($toUpdate);

		foreach($items as $index => $item)
		{
			unset($item['ACTION']); // todo: move ACTION cutoff above

			if(isset($toAdd[$index]))
			{
				$item['SORT_INDEX'] = $sortIndex;
				unset($item['ID']); // do not pass ID to add()

				$item['TASK_ID'] = $taskId;

				$opResult = static::add($userId, $item, $parameters);

				$data[$opResult['DATA']['ID']] = $opResult['DATA'];
			}
			else
			{
				if(isset($toUpdate[$index]))
				{
					// complete update
					$item['SORT_INDEX'] = $sortIndex;
					unset($item['ID']); // do not pass ID to update()

					static::update($userId, $index, $item, array_merge(array('TASK_ID' => $taskId), $parameters));
				}
				else
				{
					// update sort index only
					$itemInst = new \CTaskCheckListItem($task, $index);
					$itemInst->setSortIndex($sortIndex);
				}

				// todo: pass all errors from $opResult to $errors

				$data[$index] = array('ID' => $index); // todo: extended data here later ?
			}

			$sortIndex++;
		}
	}

	$result['DATA'] = $data;

	return $result;
}