• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/volume/emailattachment.php
  • Класс: Bitrix\Crm\Volume\EmailAttachment
  • Вызов: EmailAttachment::clearFiles
public function clearFiles()
{
	if (!$this->canClearEntity())
	{
		return -1;
	}

	$query = $this->prepareQuery();

	$dropped = -1;

	if ($this->prepareFilter($query))
	{
		$userPermissions = \CCrmPerms::GetUserPermissions($this->getOwner());
		$diskAvailable = \Bitrix\Main\Loader::includeModule('disk');
		$exceptMailTemplateAttachment = Volume\MailTemplate::getAttachmentList();

		$query
			//->where('TYPE_ID', '=', \CCrmActivityType::Email)
			->setSelect(array('ID', 'OWNER_TYPE_ID', 'OWNER_ID'))
			->setLimit(self::MAX_ENTITY_PER_INTERACTION)
			->setOrder(array('ID' => 'ASC'));

		if ($this->getProcessOffset() > 0)
		{
			$query->where('ID', '>', $this->getProcessOffset());
		}

		$res = $query->exec();

		$dropped = 0;
		while ($activity = $res->fetch())
		{
			$this->setProcessOffset($activity['ID']);

			if (\CCrmActivity::CheckItemDeletePermission($activity, $userPermissions))
			{
				$activityElementList = Crm\ActivityElementTable::getList(array(
					'filter' => array('=ACTIVITY_ID' => $activity['ID']),
					'select' => array('STORAGE_TYPE_ID', 'ELEMENT_ID'),
				));

				// check existence and force removing
				while ($row = $activityElementList->fetch())
				{
					// ignore mail template attachments
					if (in_array((int)$row['ELEMENT_ID'], $exceptMailTemplateAttachment))
					{
						$this->incrementFailCount();
						continue;
					}
					$elementIds = array($row['ELEMENT_ID']);
					if (\CCrmActivity::DeleteStorageElements($activity['ID'], array('STORAGE_ELEMENT_IDS' => $elementIds)))
					{
						if ($row['STORAGE_TYPE_ID'] == Crm\Integration\StorageType::File)
						{
							\CFile::Delete($row['ELEMENT_ID']);
						}
						elseif ($row['STORAGE_TYPE_ID'] == Crm\Integration\StorageType::Disk && $diskAvailable)
						{
							$file = \Bitrix\Disk\File::getById($row['ELEMENT_ID']);
							if ($file instanceof \Bitrix\Disk\File)
							{
								if (!$file->delete(\Bitrix\Disk\SystemUser::SYSTEM_USER_ID))
								{
									$this->collectError($file->getErrors());
								}
							}
						}
						$this->incrementDroppedFileCount();
						$dropped ++;
					}
					else
					{
						$error = \CCrmActivity::GetLastErrorMessage();
						if (empty($error))
						{
							$error = 'Deletion failed with activity #'.$activity['ID'];
						}
						$this->collectError(new Main\Error($error,self::ERROR_DELETION_FAILED));
						$this->incrementFailCount();
					}
				}
			}
			else
			{
				$this->collectError(new Main\Error('Access denied to activity #'.$activity['ID'], self::ERROR_PERMISSION_DENIED));
				$this->incrementFailCount();
			}

			if ($this->hasTimeLimitReached())
			{
				break;
			}
		}
	}
	else
	{
		$this->collectError(new Main\Error('Filter error', self::ERROR_DELETION_FAILED));
	}

	return $dropped;
}