PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/export/Export.class.php

https://github.com/bgerp/bgerp
PHP | 289 lines | 166 code | 81 blank | 42 comment | 40 complexity | 59e8628a07f4c477674765b20c531739 MD5 | raw file
  1. <?php
  2. /**
  3. * Експортиране на документи
  4. *
  5. * @category bgerp
  6. * @package export
  7. *
  8. * @author Yusein Yuseinov <yyuseinov@gmail.com>
  9. * @copyright 2006 - 2018 Experta OOD
  10. * @license GPL 3
  11. *
  12. * @since v 0.1
  13. */
  14. class export_Export extends core_Mvc
  15. {
  16. /**
  17. * Заглавие на таблицата
  18. */
  19. public $title = 'Експортиране на документ';
  20. /**
  21. * Връща масив с възможните формати за експорт
  22. *
  23. * @param int $clsId
  24. * @param int $objectId
  25. * @param NULL|int $limit
  26. *
  27. * @return array
  28. */
  29. public static function getPossibleExports($clsId, $objectId, $limit = null)
  30. {
  31. $clsArr = core_Classes::getOptionsByInterface('export_ExportTypeIntf');
  32. $res = array();
  33. foreach ($clsArr as $cls => $clsName) {
  34. if (!cls::load($clsName, true)) {
  35. continue;
  36. }
  37. $clsInst = cls::getInterface('export_ExportTypeIntf', $cls);
  38. if (!$clsInst->canUseExport($clsId, $objectId)) {
  39. continue;
  40. }
  41. $res[$cls] = $clsInst->getExportTitle($clsId, $objectId);
  42. if (isset($limit)) {
  43. if (!--$limit) {
  44. break;
  45. }
  46. }
  47. }
  48. return $res;
  49. }
  50. /**
  51. * Помощна функция за проверка на права към документа
  52. *
  53. * @param int $clsId
  54. * @param int $objId
  55. *
  56. * @return bool
  57. */
  58. public static function canUseExport($clsId, $objId)
  59. {
  60. static $resArr = array();
  61. $key = $clsId . '|' . $objId . '|' . core_Users::getCurrent();
  62. if (isset($resArr[$key])) {
  63. return $resArr[$key];
  64. }
  65. if (!$clsId || !$objId) {
  66. $resArr[$key] = false;
  67. return $resArr[$key];
  68. }
  69. if (!cls::load($clsId, true)) {
  70. $resArr[$key] = false;
  71. return $resArr[$key];
  72. }
  73. $dInst = cls::get($clsId);
  74. $dRec = $dInst->fetch($objId);
  75. if (($dRec->state == 'rejected') || ($dRec->state == 'draft')) {
  76. $resArr[$key] = false;
  77. return $resArr[$key];
  78. }
  79. if (!$dInst->haveRightFor('single', $objId)) {
  80. $resArr[$key] = false;
  81. return $resArr[$key];
  82. }
  83. $resArr[$key] = true;
  84. return $resArr[$key];
  85. }
  86. /**
  87. * Екшън за експортиране
  88. */
  89. public function act_Export()
  90. {
  91. Request::setProtected(array('classId', 'docId'));
  92. $classId = Request::get('classId', 'class(interface=doc_DocumentIntf)');
  93. $docId = Request::get('docId', 'int');
  94. expect($classId && $docId);
  95. $inst = cls::get($classId);
  96. $dRec = $inst->fetch($docId);
  97. expect($dRec);
  98. $inst->requireRightFor('exportdoc', $dRec);
  99. core_App::setTimeLimit(300);
  100. $form = $this->getForm();
  101. $form->title = 'Експортиране на|* ' . $inst->getFormTitleLink($docId);
  102. $retUrl = getRetUrl();
  103. if (empty($retUrl)) {
  104. $retUrl = array($inst, 'single', $docId);
  105. }
  106. $exportFormats = $this->getPossibleExports($classId, $docId);
  107. if (!empty($exportFormats)) {
  108. ksort($exportFormats);
  109. }
  110. $suggestions = '';
  111. foreach ($exportFormats as $clsId => $typeTitle) {
  112. $suggestions .= "{$clsId}={$typeTitle},";
  113. }
  114. $suggestions = rtrim($suggestions, ',');
  115. $form->FNC('type', "enum({$suggestions})", 'maxRadio=10, caption=Вид, input, mandatory,silent,removeAndRefreshForm');
  116. $pKey = 'docExportType_' . core_Users::getCurrent();
  117. if (($docExportType = core_Permanent::get($pKey)) && (isset($exportFormats[$docExportType]))) {
  118. $form->setDefault('type', $docExportType);
  119. }
  120. $form->input(null, 'silent');
  121. // $form->input();
  122. // Ако е избран драйвер, той може да добавя полета за параметри на формата
  123. if($type = $form->rec->type){
  124. $intfCls = cls::getInterface('export_ExportTypeIntf', $type);
  125. $intfCls->addParamFields($form, $classId, $docId);
  126. }
  127. $form->input(null, 'silent');
  128. $form->input();
  129. if ($form->isSubmitted()) {
  130. $exportFormatsArr = $this->getPossibleExports($classId, $docId);
  131. expect($exportFormatsArr[$form->rec->type]);
  132. Mode::set('exporting', true);
  133. $intfCls = cls::getInterface('export_ExportTypeIntf', $form->rec->type);
  134. $eRes = $intfCls->makeExport($form, $classId, $docId);
  135. if ($form->rec->type) {
  136. core_Permanent::set($pKey, $form->rec->type, 43200);
  137. }
  138. if (is_object($eRes) && $eRes instanceof core_Redirect) {
  139. return $eRes;
  140. }
  141. $form->setReadOnly('type');
  142. $form->toolbar->addBtn('Затваряне', $retUrl, 'ef_icon = img/16/close-red.png, title=' . tr('Връщане към документа') . ', class=fright');
  143. // Добавяме необходимите бутони от интерфейсите
  144. $intfArr = core_Classes::getOptionsByInterface('export_FileActionIntf');
  145. foreach ($intfArr as $cls) {
  146. $intfCls = cls::getInterface('export_FileActionIntf', $cls);
  147. $intfCls->addActionBtn($form, $eRes);
  148. }
  149. if (!empty($intfArr) && $eRes) {
  150. if ((strlen($eRes) == FILEMAN_HANDLER_LEN) || !defined('FILEMAN_HANDLER_LEN')) {
  151. if ($fRec = fileman::fetchByFh($eRes)) {
  152. if ($dRec->containerId && $fRec->id) {
  153. doc_Linked::add($dRec->containerId, $fRec->id, 'doc', 'file', tr('Експортиране'));
  154. }
  155. }
  156. }
  157. }
  158. } else {
  159. $form->toolbar->addSbBtn('Генериране', 'save', 'ef_icon = img/16/world_link.png, title = ' . tr('Генериране на линк за сваляне'));
  160. $form->toolbar->addBtn('Отказ', $retUrl, 'ef_icon = img/16/close-red.png, title= ' . tr('Прекратяване на действията'));
  161. }
  162. $tpl = $form->renderHtml();
  163. $inst->currentTab = 'Нишка';
  164. if (core_Packs::isInstalled('colab')) {
  165. if (core_Users::haveRole('partner')) {
  166. plg_ProtoWrapper::changeWrapper($inst, 'cms_ExternalWrapper');
  167. }
  168. }
  169. $tpl = $inst->renderWrapping($tpl);
  170. return $tpl;
  171. }
  172. /**
  173. * Помощен екшън за ескпорт със съответния интерфейс
  174. */
  175. public function act_ExportInExternal()
  176. {
  177. Request::setProtected(array('objId', 'clsId', 'mid', 'typeCls'));
  178. $objId = Request::get('objId', 'int');
  179. $clsId = Request::get('clsId', 'int');
  180. expect($objId && $clsId);
  181. $mid = Request::get('mid');
  182. expect($clsInst = cls::get($clsId));
  183. $mRec = $clsInst->fetch($objId);
  184. expect($mRec && $mRec->containerId);
  185. expect($mRec->state != 'rejected');
  186. expect($action = doclog_Documents::opened($mRec->containerId, $mid));
  187. doclog_Documents::popAction();
  188. $typeCls = Request::get('typeCls');
  189. $typeClsInst = cls::get($typeCls);
  190. // Ако е избран друг шаблон за отпечатване
  191. if ($action->data->tplManagerId) {
  192. $mRec->template = $action->data->tplManagerId;
  193. }
  194. $form = $clsInst->getForm();
  195. $mRec->__mid = $mid;
  196. if ($action->createdBy) {
  197. $su = core_Users::sudo($action->createdBy);
  198. }
  199. Mode::set('exporting', true);
  200. $fileHnd = $typeClsInst->makeExport($form, $clsId, $mRec);
  201. core_Users::exitSudo($su);
  202. if ($fileHnd) {
  203. $typeClsInst->logInfo('Експортиран документ');
  204. return Request::forward(array('fileman_Download', 'download', 'fh' => $fileHnd, 'forceDownload' => true));
  205. }
  206. followRetUrl(null, '|Няма данни за експорт', 'error');
  207. }
  208. }