_companyId = $companyId; } public function getCompanyId() { if (!$this->_companyId) { throw new Qs_Form_Exception('$_companyId is not set.'); } return $this->_companyId; } protected function _initPaths() { parent::_initPaths(); $this->addPrefixPath( 'App\\Company\\Admin\\Form\\Element', 'App/Company/Admin/Form/Element/', self::ELEMENT ); $this->getView()->addHelperPath('App/Company/Admin/View/Helper/', 'App\\Company\\Admin\\View\\Helper\\'); return $this; } protected function _initElements() { $this->addElement('hidden', 'sorter', ['decorators' => ['ViewHelper', 'Errors', ['HtmlTag', ['tag' => 'td']]]]); $this->addElement( 'select', 'positionId', [ 'label' => 'Position', 'required' => !empty($this->_defaults['userId']), 'multioptions' => ['' => 'Select One'] + (new Qs_Db_Table('PersonnelPosition'))->get4Select(), 'class' => 'select position-select', 'decorators' => ['ViewHelper', 'Errors', ['HtmlTag', ['tag' => 'td']]] ] ); $dataUrl = $this->_getUserDataUrl([ 'companyId' => $this->getCompanyId(), 'status' => [Entity::STATUS_ACTIVE, Entity::STATUS_INACTIVE] ]); $this->addElement( 'autocomplete', 'userId', [ 'label' => 'Individual', 'dataUrl' => $dataUrl, 'required' => !empty($this->_defaults['positionId']), 'decorators' => ['JQuery_Autocomplete', 'Errors', ['HtmlTag', ['tag' => 'td']]] ] ); if (($userId = $this->_getData('userId'))) { $this->userId->setTitle(Model::getUserNameById($userId)); } $unique = new Zend_Validate_Callback(array($this, 'validateUnique')); $unique->setMessage($this->userId->getLabel() . ' must be unique', Zend_Validate_Callback::INVALID_VALUE); $this->userId->addValidator($unique, true); $this->addElement( 'positionItemOptions', 'options', ['decorators' => ['ViewHelper', ['HtmlTag', ['tag' => 'td']]]] ); return $this; } public function validateUnique() { if (Qs_Form::METHOD_POST == $this->getMethod()) { $formContext = Qs_Request::getPost(); } else { $formContext = Qs_Request::getGet(); } if (!in_array($formContext['type'], [Entity::TYPE_REGULAR, Entity::TYPE_ASSOCIATE])) { return true; } $elementName = $this->_parentForm->getName(); $currentIndex = $this->getName(); $field = 'userId'; if (($positions = Qs_Array::get($formContext, $elementName))) { $currentPrice = $positions[$currentIndex]; unset($positions[$currentIndex]); foreach ($positions as $position) { if ($position[$field] == $currentPrice[$field]) { return false; } } } return true; } public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } $decorators = $this->getDecorators(); if (empty($decorators)) { $this->addDecorator('FormElements'); $this->addDecorator(array('item-tr' => 'HtmlTag'), array('tag' => 'tr', 'class' => 'line-item item-row')); } return $this; } protected function _getUserDataUrl(array $params = []) { if (empty($params['action'])) { $params['action'] = 'autocompleteUser'; } $url = View::getPage('url'); return ($url) ? ($url . '?' . http_build_query($params)) : null; } public function initRender() { if ($this->getInitializedRender()) { return $this; } // ids fix $this->getDecorator('item-tr')->setOption('id', $this->getFullId() . $this->_rowIdSuffix); /** @var \Zend_Form_Element $element */ foreach ($this as $element) { $element->getDecorator('HtmlTag')->setOption('id', $element->getId() . '-element'); $element->getDecorator('HtmlTag')->setOption('class', $element->getName()); } return parent::initRender(); } protected function _addResources() { $doc = $this->getDoc(); $options = [ 'itemSubFromId' => $this->getFullId() . $this->_rowIdSuffix, 'hintOptions' => [ 'elements' => [ ['selector' => '#' . $this->userId->getDisplayId(), 'hint' => 'Autocomplete field'] ], 'useWrapper' => true ] ]; $doc->addScript('js/jquery.defaultHint.js', [], 'defaultHint'); $doc->addScript('js/app/company/admin/positionItemSubFrom.js'); $doc->addInitObject('app.company.admin.PositionItemSubForm', [$options]); return $this; } }