* @package taoItems */ abstract class ExportForm extends tao_helpers_form_FormContainer { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- // --- OPERATIONS --- /** * Short description of method initForm * * @access public * @return mixed * @author Joel Bout, */ public function initForm() { $this->form = new tao_helpers_form_xhtml_Form('export'); $this->form->setDecorators( [ 'element' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div']), 'group' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div', 'cssClass' => 'form-group']), 'error' => new tao_helpers_form_xhtml_TagWrapper( ['tag' => 'div', 'cssClass' => 'form-error ui-state-error ui-corner-all'] ), 'actions-bottom' => new tao_helpers_form_xhtml_TagWrapper( ['tag' => 'div', 'cssClass' => 'form-toolbar'] ), 'actions-top' => new tao_helpers_form_xhtml_TagWrapper( ['tag' => 'div', 'cssClass' => 'form-toolbar'] ) ] ); $exportElt = tao_helpers_form_FormFactory::getElement('export', 'Free'); $exportElt->setValue( ' ' . __('Export') . '' ); $this->form->setActions([$exportElt], 'bottom'); } /** * overriden * * @access public * @return mixed * @throws common_Exception * @author Joel Bout, */ public function initElements() { $itemService = $this->getItemsService(); $fileName = ''; $options = ['options' => []]; if (isset($this->data['items'])) { $fileName = $this->getFileName($this->data['file_name']); $options = $this->getOptions(...array_values($this->data['items'])); } elseif (isset($this->data['instance'])) { $item = $this->data['instance']; if ( $item instanceof core_kernel_classes_Resource && $itemService->hasItemModel($item, [ItemModel::MODEL_URI]) ) { $fileName = $this->getFileName($item->getLabel()); $options = $this->getOptions($item); } } else { $class = $this->data['class'] ?? $itemService->getRootClass(); if ($class instanceof core_kernel_classes_Class) { $fileName = $this->getFileName($class->getLabel()); $options = $this->getOptions(...$class->getInstances(true)); } } $nameElt = tao_helpers_form_FormFactory::getElement('filename', 'Textbox'); $nameElt->setDescription(__('File name')); $nameElt->setValue($fileName); $nameElt->setUnit('.zip'); $nameElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $this->form->addElement($nameElt); $instanceElt = tao_helpers_form_FormFactory::getElement('instances', 'Checkbox'); $instanceElt->setDescription(__('Items')); if (count($options['options']) > 1) { $instanceElt->setAttribute('checkAll', true); } $instanceElt->setOptions(tao_helpers_Uri::encodeArray($options['options'], tao_helpers_Uri::ENCODE_ARRAY_KEYS)); $instanceElt->setReadOnly( tao_helpers_Uri::encodeArray($options['disabledOptions'], tao_helpers_Uri::ENCODE_ARRAY_KEYS) ); foreach (array_keys($options['options']) as $value) { if (!isset($options['disabledOptions'][$value])) { $instanceElt->setValue($value); } } $this->form->addElement($instanceElt); $this->form->createGroup('options', '

' . $this->getFormGroupName() . '

', ['filename', 'instances']); } /** * @return taoItems_models_classes_ItemsService */ protected function getItemsService() { return taoItems_models_classes_ItemsService::singleton(); } protected function getFileName(string $name): string { return strtolower(tao_helpers_Display::textCleaner($name, '*')); } protected function getOptions(core_kernel_classes_Resource ...$resource): array { $itemService = taoItems_models_classes_ItemsService::singleton(); $options = []; $disabledOptions = []; foreach ($resource as $instance) { if (!$itemService->hasItemModel($instance, [ItemModel::MODEL_URI])) { continue; } try { $this->isInstanceValid($instance); } catch (common_exception_UserReadableException $e) { $disabledOptions[$instance->getUri()] = $e->getUserMessage(); } $options[$instance->getUri()] = $instance->getLabel(); } return [ 'options' => $options, 'disabledOptions' => $disabledOptions ]; } /** * @param $item * * @return bool * @throws ExportElementException * @throws common_Exception */ protected function isInstanceValid($item) { try { $xml = Service::singleton()->getXmlByRdfItem($item); } catch (FileNotFoundException $e) { } if (empty($xml)) { throw new ExportElementException($item, __('no item content')); } return true; } /** * Get the form group name to be display * @return string */ abstract protected function getFormGroupName(); }