layout)) $this->layout = $this->prop('layout'); return $this->layout == $layout; } /** * @param mixed[]|string $prop * @param mixed $value */ protected function onSetProp($prop, $value) { if($prop === 'id' && $value === '$AUTO') { global $app; $value = "form-{$app->rawModule}-{$app->rawMethod}"; } parent::onSetProp($prop, $value); } protected function created() { parent::created(); global $app, $lang; $module = $app->getModuleName(); $method = $app->getMethodName(); if(isAjaxRequest('modal')) { $text = !empty($lang->$module->$method) ? $lang->$module->$method : zget($lang, $method, ''); $defaultProps = array(); $defaultProps['submitBtnText'] = $text; $this->setDefaultProps($defaultProps); } if(!$this->hasProp('data')) $this->setProp('data', data($module)); if(!$this->hasProp('labelData')) $this->setProp('labelData', $lang->$module); if($this->prop('customBtn')) { global $config; $key = $method . 'Fields'; $app->loadLang($module); $app->loadModuleConfig($module); if($this->prop('foldableItems') === true) { $listFieldsKey = 'custom' . ucfirst($key); $fieldList = empty($config->{$module}->{$listFieldsKey}) ? (!empty($config->{$module}->list->{$listFieldsKey}) ? $config->{$module}->list->{$listFieldsKey} : array()) : $config->{$module}->{$listFieldsKey}; $foldableItems = empty($fieldList) ? array() : explode(',', $fieldList); if(!empty($foldableItems)) { $this->setProp('foldableItems', $foldableItems); } } if(!$this->hasProp('pinnedItems') && isset($config->{$module}->custom->{$key})) { $this->setProp('pinnedItems', explode(',', $config->$module->custom->$key)); } } } protected function beforeBuild() { $fields = $this->prop('fields'); if(is_string($fields)) $fields = explode(',', $fields); if(is_array($fields)) $fields = useFields($fields); if($fields instanceof fieldList) { $items = $fields->toList(); $this->setProp('items', $items); if(!is_null($fields->labelData)) $this->setProp('labelData', $fields->labelData); if(!is_null($fields->valueData)) $this->setProp('data', $fields->valueData); if(!is_null($fields->ordersForFull)) $this->setProp('fullModeOrders', $fields->ordersForFull); if(!is_null($fields->autoLoadRule)) { $autoLoad = $this->prop('autoLoad'); if(is_null($autoLoad)) $autoLoad = array(); $autoLoad = array_merge($autoLoad, $fields->autoLoadRule); $this->setProp('autoLoad', $autoLoad); } } } /** * @param string $name */ protected function getItemLabel($name) { $labelData = $this->prop('labelData'); $lblName = 'lbl' . ucfirst($name); if(is_object($labelData)) return isset($labelData->$lblName) ? $labelData->$lblName : (isset($labelData->$name) ? $labelData->$name : null); if(is_array($labelData)) return isset($labelData[$lblName]) ? $labelData[$lblName] : (isset($labelData[$name]) ? $labelData[$name] : null); return null; } /** * @param string $name */ protected function getItemValue($name) { $data = $this->prop('data'); if(is_object($data)) return isset($data->$name) && !is_array($data->$name) && !is_object($data->$name) ? strval($data->$name) : null; if(is_array($data)) return isset($data[$name]) ? strval($data[$name]) : null; return null; } /** * @param \zin\item $item */ public function onBuildItem($item) { return new formGroup(inherit($item)); } /** * @param \zin\item|mixed[] $item * @param string|int $key * @param bool|string|null $requiredFields * @param mixed[]|null $foldableItems * @param mixed[]|null $pinnedItems * @param bool $isGrid */ protected function getItemProps($item, $key, $foldableItems = null, $pinnedItems = null, $isGrid = false, $requiredFields = null) { if(is_string($key) && !isset($item['name'])) $item['name'] = $key; $name = $item['name']; if(is_string($name)) { if(!isset($item['value'])) $item['value'] = $this->getItemValue($name); if(!isset($item['label'])) $item['label'] = $this->getItemLabel($name); if($requiredFields !== false && (!isset($item['required']) || $item['required'] === 'auto')) $item['required'] = isFieldRequired($name, $requiredFields); if(!isset($item['required']) || !$item['required']) { if(!isset($item['foldable']) && is_array($foldableItems)) $item['foldable'] = in_array($name, $foldableItems); if(!isset($item['pinned']) && is_array($pinnedItems)) $item['pinned'] = in_array($name, $pinnedItems); } else { $item['foldable'] = false; $item['pinned'] = true; } if($isGrid) { if(!isset($item['width'])) $item['width'] = '1/2'; $control = isset($item['control']) ? $item['control'] : null; if(is_null($control)) $control = array('name' => $name); if(is_string($control)) $control = array('control' => $control, 'name' => $name); if(is_array($control) && !isset($control['id'])) $control['id'] = ''; $item['control'] = $control; } } return $item; } protected function buildCustomBtn() { global $lang; list($customBtn, $customUrl) = $this->prop(array('customBtn', 'customUrl')); if(is_null($customUrl)) { global $app; $customUrl = createLink('custom', 'ajaxSaveCustomFields', "module={$app->rawModule}§ion=custom&key={$app->rawMethod}Fields"); } return btn ( setClass('gray-300-outline rounded-full btn-custom-form'), setData(array('title' => $lang->fieldDisplaySetting, 'tip' => $lang->fieldSettingTip, 'customUrl' => $customUrl, 'saveText' => $lang->save, 'cancelText' => $lang->cancel, 'resetText' => $lang->restore)), set::icon('cog-outline'), on::click()->call('zui.FormSetting.show', jsRaw('$.extend({element: $element[0]}, $element.data())')), is_array($customBtn) ? set($customBtn) : null ); } public function buildItems() { global $lang; list($items, $foldableItems, $pinnedItems, $requiredFields, $customBtn, $toolbar) = $this->prop(array('items', 'foldableItems', 'pinnedItems', 'requiredFields', 'customBtn', 'toolbar')); if(!is_array($items)) $items = array(); if(is_string($pinnedItems) && !empty($pinnedItems)) $pinnedItems = explode(',', $pinnedItems); if(is_string($foldableItems) && !empty($foldableItems)) $foldableItems = explode(',', $foldableItems); if(is_null($customBtn) && !empty($foldableItems)) $customBtn = true; $isGrid = $this->isLayout('grid'); $list = array(); $foldableList = array(); foreach($items as $key => $item) { if(empty($item)) continue; if($item instanceof node && !($item instanceof item)) { $list[] = $item; continue; } if($item instanceof item) $item = $item->props->toJson(); elseif($item instanceof field) $item = $item->toArray(); $itemsProps = $this->getItemProps($item, $key, $foldableItems, $pinnedItems, $isGrid, $requiredFields); $formGroup = new formGroup(set($itemsProps)); if(isset($itemsProps['foldable']) && $itemsProps['foldable'] && (!isset($itemsProps['hidden']) || !$itemsProps['hidden'])) $foldableList[] = $formGroup; else $list[] = $formGroup; } $toolbarList = array(); if(!empty($toolbar)) { if(!isset($toolbar['items'])) $toolbar = array('items' => $toolbar); $toolbarList[] = toolbar(set($toolbar)); } if(!empty($foldableList)) { $list[] = div(setClass('panel-form-divider')); $toolbarList[] = toolbar ( setClass('size-sm'), btn ( setClass('gray-300-outline rounded-full btn-toggle-fold'), setData(array('collapse-text' => $lang->hideMoreInfo, 'expand-text' => $lang->showMoreInfo)) ), $customBtn ? $this->buildCustomBtn() : null ); } if(!empty($toolbarList)) $foldableList[] = div(setClass('panel-form-toolbar'), $toolbarList); return array_merge($list, $foldableList); } protected function buildActions() { $sticky = $this->prop('stickyActions'); $actions = parent::buildActions(); if($sticky) { $actions->setProp('zui-create', 'sticky'); $actions->setProp('data-side', 'bottom'); if(is_array($sticky)) $actions->add(setData($sticky)); } if($this->isLayout('horz') && !empty($actions)) $actions = div(setClass('form-row'), $actions); return $actions; } protected function buildProps() { $props = parent::buildProps(); $layout = $this->prop('layout'); $props[] = setClass("form-$layout", $this->prop('requiredFields') === false ? 'no-required' : ''); if($layout == 'horz') { $labelWidth = $this->prop('labelWidth'); if(!empty($labelWidth)) $props[] = setCssVar('form-horz-label-width', $labelWidth); } return $props; } protected function buildAfter() { $after = parent::buildAfter(); if($this->isLayout('grid')) { $options = $this->props->pick(array('loadUrl', 'autoLoad', 'fullModeOrders')); $after[] = zui::formGrid ( set::_to('#' . $this->id()), set($options) ); } return $after; } protected function buildContent() { $items = $this->buildItems(); if($this->isLayout('horz')) { foreach($items as $key => $item) { if(!($item instanceof formGroup)) continue; $items[$key] = new formRow($item); } } else { $items[] = setData('fullModeOrders', $this->prop('fullModeOrders')); } return $items; } protected function build() { $this->beforeBuild(); return parent::build(); } }