PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/bitrix/modules/iblock/lib/bizproctype/usertypepropertydiskfile.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 197 lines | 138 code | 37 blank | 22 comment | 25 complexity | ae1dfef58e7cd60d9465caef5f66a7f1 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Iblock\BizprocType;
  3. use Bitrix\Bizproc\FieldType;
  4. use Bitrix\Main\Loader;
  5. class UserTypePropertyDiskFile extends UserTypeProperty
  6. {
  7. /**
  8. * @param FieldType $fieldType
  9. * @param $value
  10. * @return string
  11. */
  12. protected static function formatValuePrintable(FieldType $fieldType, $value)
  13. {
  14. if(!Loader::includeModule('disk'))
  15. {
  16. return '';
  17. }
  18. $attachedId = (int)$value;
  19. $attachedModel = \Bitrix\Disk\AttachedObject::loadById($attachedId, array('OBJECT'));
  20. if(!$attachedModel)
  21. {
  22. return '';
  23. }
  24. global $USER;
  25. $userId = $USER->getID();
  26. if($userId)
  27. {
  28. if(!$attachedModel->canRead($userId))
  29. {
  30. return '';
  31. }
  32. }
  33. $file = $attachedModel->getFile();
  34. if(!$file)
  35. {
  36. return '';
  37. }
  38. $driver = \Bitrix\Disk\Driver::getInstance();
  39. $urlManager = $driver->getUrlManager();
  40. return '[url='.$urlManager->getUrlUfController('download', array('attachedId' => $attachedModel->getId())
  41. ).']'.htmlspecialcharsbx($file->getName()).'[/url]';
  42. }
  43. /**
  44. * @param FieldType $fieldType Document field object.
  45. * @param array $field Form field information.
  46. * @param mixed $value Field value.
  47. * @param bool $allowSelection Allow selection flag.
  48. * @param int $renderMode Control render mode.
  49. * @return string
  50. */
  51. public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
  52. {
  53. return static::renderControlMultiple($fieldType, $field, $value, $allowSelection, $renderMode);
  54. }
  55. /**
  56. * @param FieldType $fieldType Document field object.
  57. * @param array $field Form field information.
  58. * @param mixed $value Field value.
  59. * @param bool $allowSelection Allow selection flag.
  60. * @param int $renderMode Control render mode.
  61. * @return string
  62. */
  63. public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
  64. {
  65. if ($renderMode & FieldType::RENDER_MODE_DESIGNER)
  66. return '';
  67. $userType = static::getUserType($fieldType);
  68. $documentType = $fieldType->getDocumentType();
  69. $iblockId = $documentType[2];
  70. $iblockId = str_replace('iblock_', '', $iblockId);
  71. if (!empty($userType['GetPublicEditHTML']))
  72. {
  73. if (!isset($value['VALUE']))
  74. $value = array('VALUE' => $value);
  75. $fieldName = static::generateControlName($field);
  76. $fieldName .= $fieldType->isMultiple() ? '' : '[]';
  77. $renderResult = call_user_func_array(
  78. $userType['GetPublicEditHTML'],
  79. array(
  80. array(
  81. 'IBLOCK_ID' => $iblockId,
  82. 'IS_REQUIRED' => $fieldType->isRequired()? 'Y' : 'N',
  83. 'PROPERTY_USER_TYPE' => $userType
  84. ),
  85. $value,
  86. array(
  87. 'FORM_NAME' => $field['Form'],
  88. 'VALUE' => $fieldName,
  89. 'DESCRIPTION' => '',
  90. ),
  91. true
  92. )
  93. );
  94. }
  95. else
  96. $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
  97. return $renderResult;
  98. }
  99. public static function extractValue(FieldType $fieldType, array $field, array $request)
  100. {
  101. if(!Loader::includeModule('disk'))
  102. return null;
  103. $value = parent::extractValue($fieldType, $field, $request);
  104. if (isset($value['VALUE']))
  105. $value = $value['VALUE'];
  106. if(!$value)
  107. return null;
  108. // Attach file disk
  109. $userFieldManager = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager();
  110. list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType('iblock_workflow');
  111. list($type, $realId) = \Bitrix\Disk\Uf\FileUserType::detectType($value);
  112. if($type != \Bitrix\Disk\Uf\FileUserType::TYPE_NEW_OBJECT)
  113. return null;
  114. $errorCollection = new \Bitrix\Disk\Internals\Error\ErrorCollection();
  115. $fileModel = \Bitrix\Disk\File::loadById($realId, array('STORAGE'));
  116. if(!$fileModel)
  117. return null;
  118. $securityContext = $fileModel->getStorage()->getCurrentUserSecurityContext();
  119. if(!$fileModel->canRead($securityContext))
  120. return null;
  121. $documentType = $fieldType->getDocumentType();
  122. $iblockId = intval(substr($documentType[2], strlen("iblock_")));
  123. $canUpdate = $fileModel->canUpdate($securityContext);
  124. global $USER;
  125. $attachedModel = \Bitrix\Disk\AttachedObject::add(array(
  126. 'MODULE_ID' => $moduleId,
  127. 'OBJECT_ID' => $fileModel->getId(),
  128. 'ENTITY_ID' => $iblockId,
  129. 'ENTITY_TYPE' => $connectorClass,
  130. 'IS_EDITABLE' => (int)$canUpdate,
  131. 'ALLOW_EDIT' => (int) ($canUpdate && (int)\Bitrix\Main\Application::getInstance()->getContext()->getRequest()->getPost('DISK_FILE_'.$iblockId.'_DISK_ATTACHED_OBJECT_ALLOW_EDIT')),
  132. 'CREATED_BY' => $USER->getId(),
  133. ), $errorCollection);
  134. if(!$attachedModel || $errorCollection->hasErrors())
  135. return null;
  136. return $attachedModel->getId();
  137. }
  138. public static function clearValueSingle(FieldType $fieldType, $value)
  139. {
  140. if(!Loader::includeModule('disk'))
  141. return;
  142. $value = (int)$value;
  143. if(!$value)
  144. return;
  145. $documentType = $fieldType->getDocumentType();
  146. $iblockId = intval(substr($documentType[2], strlen("iblock_")));
  147. if(!$iblockId)
  148. return;
  149. $userFieldManager = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager();
  150. list($type, $realId) = \Bitrix\Disk\Uf\FileUserType::detectType($value);
  151. if($type == \Bitrix\Disk\Uf\FileUserType::TYPE_ALREADY_ATTACHED)
  152. {
  153. $attachedModel = \Bitrix\Disk\AttachedObject::loadById($realId);
  154. if(!$attachedModel)
  155. return;
  156. if($userFieldManager->belongsToEntity($attachedModel, "iblock_workflow", $iblockId))
  157. \Bitrix\Disk\AttachedObject::detachByFilter(array('ID' => $realId));
  158. }
  159. }
  160. }