• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/item/task/template/field/basetemplate.php
  • Класс: BitrixTasksItemTaskTemplateFieldBaseTemplate
  • Вызов: BaseTemplate::saveValueToDataBase
public function saveValueToDataBase($value, $key, $item)
{
	$result = new Result();

	$itemId = $item->getId();
	$state = $item->getTransitionState();
	$isAdd = $state->isModeCreate();
	$isUpdate = $state->isModeUpdate();
	$isDelete = $state->isModeDelete();

	if($itemId)
	{
		// todo: use BitrixTasksInternalsHelperTaskTemplateDependence here, because
		// todo: tree functional of BitrixTasksInternalsTaskTemplateDependenceTable is deprecated, it is non-extensible, buggy and
		// todo: throws exceptions, which is also not good

		try
		{
			if($isAdd || $isUpdate)
			{
				if($isAdd && $value)
				{
					// add link
					DependenceTable::createLink($itemId, $value);
				}

				if($isUpdate)
				{
					// relocate or remove link
					DependenceTable::link($itemId, $value);
				}
			}
			elseif($isDelete)
			{
				// delete the entire sub-tree
				$subRes = DependenceTable::getSubTree($itemId, array('select' => array('ID' => 'TEMPLATE_ID')), array('INCLUDE_SELF' => false));
				while($subItem = $subRes->fetch())
				{
					$subTemplate = new Template($subItem['ID']);
					if($subTemplate->canDelete())
					{
						$subDeleteResult = $subTemplate->delete(array(
							'IGNORE' => array(
								'BASE_TEMPLATE_ID'
							)
						));
						if(!$subDeleteResult->isSuccess())
						{
							$result->adoptErrors($subDeleteResult, array(
								'CODE' => 'SUB_TEMPLATE_DELETE.#CODE#',
								//'MESSAGE' => Loc::getMessage('TASKS_ITEM_SUBITEM_DELETE_ERROR').': #MESSAGE#',
							));
						}
					}
					// todo: else we could add some warning here...
				}

				// todo: actually, we must break links according to the rights...
				try
				{
					DependenceTable::deleteSubtree($itemId);
				}
				catch(TreeTargetNodeNotFoundException $e) // had no children actually
				{
					// nobody cares
				}
			}
		}
		catch(TreeLinkExistsException $e)
		{
			// do nothing, we are fine
		}
		catch(TreeLinkNotExistException $e)
		{
			// do nothing, we are fine
		}
		catch(TreeException $e)
		{
			$result->addException($e, 'Error managing links');
		}
	}

	return $result;
}