_dfRelations = $this->getConfigArray('dynamicFormRelations'); if (empty($this->_defaults)) { $this->_defaults = $this->getConfigArray('defaults'); } return parent::init(); } protected function _initElements() { $this->addElement( 'radio', 'type', [ 'label' => 'Company Type', 'required' => true, 'multioptions' => (new Qs_Db_Table('CompanyType'))->get4Select(), 'separator' => str_repeat(' ', 3) ] ); $this->addElement( 'select', 'status', [ 'label' => 'Status', 'required' => true, 'multioptions' => Model::$emptyOption + (new Qs_Db_Table('CompanyStatus'))->get4Select() ] ); $this->_initCompanyFields(); $this->_initLegislativeFields(); $logo = $this->getConfig('logo'); $this->addElement( 'extendedImage', 'logo', [ 'label' => 'Logo', 'resize' => $logo->width . 'x' . $logo->height . Qs_ImageFs::getResizeMethodAlias($logo->resizeMethod), 'required' => in_array($this->_getData('type'), [Entity::TYPE_ASSOCIATE, Entity::TYPE_AFFILIATE]) ] ); $this->logo->getDecorator('Label')->setOption('class', 'required'); $this->addElement('htmlEditor', 'description', ['label' => 'Description']); $this->addElement('textarea', 'competitors', ['label' => 'Competitors']); $this->addElement( 'multiCheckbox', 'utilityCategoryId', [ 'label' => 'Utility Category', 'multioptions' => (new Qs_Db_Table('UtilityCategory'))->get4Select(), 'columns' => 3 ] ); $this->addElement( 'multiCheckbox', 'associateCategory', [ 'label' => 'Professional Category', 'multioptions' => (new Qs_Db_Table('AssociateCategory'))->get4Select(), 'required' => (Entity::TYPE_VENDOR == $this->_getData('type')), 'columns' => 3 ] ); $this->associateCategory->getDecorator('Label')->setOption('class', 'required'); $this->addElement( 'multiCheckbox', 'safetyGroupId', [ 'label' => 'Safety Groups', 'multioptions' => (new Qs_Db_Table('SafetyGroup'))->get4Select(), 'columns' => 3 ] ); if (($key = $this->getPrimaryKey())) { if (array_key_exists('_hasUsers', $_REQUEST)) { $hasUsers = Qs_Request::getRequestValue('_hasUsers'); } else { $key = reset($key); $where = Qs_Db::getInstance()->quoteInto('`companyId` = ?', $key, Qs_Db::INT_TYPE); $hasUsers = (bool) (new Qs_Db_Table('User'))->fetchRow($where); } $this->addElement('hidden', '_hasUsers', ['value' => $hasUsers]); if ($hasUsers) { $this->_initPersonnelFields(); } } return $this; } protected function _initCompanyFields() { $this->addElement('header', '_companyInfo', ['label' => 'Company Information']); $nameUniqueValidator = new Qs_Validate_Unique(new Qs_Db_Table('Company'), 'name', $this->_getData('id')); $nameUniqueValidator->setMessage('Company Name must be unique', Qs_Validate_Unique::NOT_UNIQUE); $this->addElement( 'text', 'name', ['required' => true, 'label' => 'Company Name', 'validators' => [$nameUniqueValidator]] ); $this->_initPhysicalAddress(); $this->_initMailingAddress(); $this->addElement( 'multiCheckbox', 'counties', [ 'label' => 'Counties', 'required' => (Entity::TYPE_REGULAR == $this->_getData('type')), 'columns' => 8, 'multiOptions' => (new Qs_Db_Table('County'))->get4Select() ] ); $this->counties->getDecorator('Label')->setOption('class', 'required'); $this->addElement('numeric', 'population', ['label' => 'Population', 'negative' => false, 'precision' => 0]); $this->addElement( 'select', 'statutoryAuthorityId', [ 'label' => 'Statutory Authority', 'multiOptions' => ['' => 'Select One'] + (array)(new Qs_Db_Table('StatutoryAuthority'))->get4Select() ] ); $this->addElement('email', 'email', ['label' => 'Company Email Address']); $this->addElement('phone', 'utilityPhone', ['label' => 'Utility Phone']); $this->addElement('phone', 'utilityFax', ['label' => 'Utility Fax']); $this->addElement('phone', 'cityOfficesPhone', ['label' => 'City Offices Phone']); $this->addElement('phone', 'cityOfficesFax', ['label' => 'City Offices Fax']); $this->addElement('phone', 'phone', ['label' => 'Phone']); $this->addElement('phone', 'tollFreePhone', ['label' => 'Toll-free Phone']); $this->addElement('phone', 'fax', ['label' => 'Fax']); $this->addElement('url', 'websiteUrl', ['label' => 'Website']); $yearFoundedCfg = $this->getConfig('yearFounded'); $years = range($yearFoundedCfg->min, $yearFoundedCfg->max); $this->addElement( 'select', 'yearFounded', [ 'label' => 'Year Founded', 'multioptions' => ['' => 'Select One'] + array_combine($years, $years) ] ); $between = new Zend_Validate_Between(['min' => $yearFoundedCfg->min, 'max' => $yearFoundedCfg->max]); $message = 'Year Founded should be between %min% and %max%'; $between->setMessage($message, Zend_Validate_Between::NOT_BETWEEN); $this->yearFounded->addValidator($between); $this->addElement('text', 'governedBy', ['label' => 'Governed by']); $this->addElement('textarea', 'meetingInfo', ['label' => 'Meeting Info']); $this->addElement('checkbox', 'taxExempt', ['label' => 'Tax Exempt', 'decoration' => 'simple']); return $this; } protected function _initPhysicalAddress() { $groupElements = ['address', 'address2', 'city', 'state', 'zip']; $this->addElement('text', 'address', ['label' => 'Address', 'required' => true]); $this->addElement('text', 'address2', ['label' => 'Address 2']); $this->addElement('text', 'city', ['label' => 'City', 'required' => true]); $this->addElement( 'select', 'state', [ 'label' => 'State', 'required' => true, 'multioptions' => Model::$emptyOption + State::getGrouped4Select() ] ); $this->addElement('zip', 'zip', ['label' => 'Zip', 'required' => true]); //Remove PostCode validator to allow USA and CAN states $this->getElement('zip')->removeValidator('PostCode'); $this->addDisplayGroup($groupElements, '_address', ['legend' => 'Physical Address']); if (Entity::TYPE_REGULAR == $this->_getData('type')) { $validator = new Qs_Validate_Google_Geocoding(); $validator->setAddressFields($groupElements); $this->getElement('address')->addValidator($validator, true); } return $this; } protected function _initMailingAddress() { $required = ('y' != $this->_getData('asPhysicalAddress')); $this->addElement( 'checkbox', 'asPhysicalAddress', [ 'label' => 'Mailing Address is the same as Physical Address', 'decoration' => 'simple' ] ); $this->addElement('text', 'mailingAddress', ['label' => 'Address', 'required' => $required]); $this->addElement('text', 'mailingAddress2', ['label' => 'Address 2']); $this->addElement('text', 'mailingCity', ['label' => 'City', 'required' => $required]); $this->addElement( 'select', 'mailingState', [ 'label' => 'State', 'required' => $required, 'multioptions' => Model::$emptyOption + State::getGrouped4Select() ] ); $this->addElement('zip', 'mailingZip', ['label' => 'Zip', 'required' => $required]); //Remove PostCode validator to allow USA and CAN states $this->getElement('mailingZip')->removeValidator('PostCode'); $elements = ['asPhysicalAddress', 'mailingAddress', 'mailingAddress2', 'mailingCity', 'mailingState', 'mailingZip']; $this->addDisplayGroup($elements, '_mailingAddress', ['legend' => 'Mailing Address']); $elements = ['mailingAddress', 'mailingCity', 'mailingState', 'mailingZip']; foreach ($elements as $element) { /** @var \Qs_Form_Decorator_Label $label */ $label = $this->getElement($element)->getDecorator('Label'); $label->setOption('class', 'required'); } return $this; } protected function _initLegislativeFields() { $this->addElement('header', '_legislativeInfo', ['value' => 'Legislative Information']); $form = new LegislativeSubForm(); $this->addSubForm($form, 'district'); return $this; } protected function _initPersonnelFields() { $this->addElement('header', '_personnelInfo', ['label' => 'Personnel']); $dataUrlParams = [ 'companyId' => $this->_primaryKey['id'], 'status' => [UserEntity::STATUS_ACTIVE], ]; $dataUrl = Model::getUserDataUrl($dataUrlParams); $this->addElement('autocomplete', 'surveyContactId', ['label' => 'Survey Contact', 'dataUrl' => $dataUrl]); if (($userId = $this->_getData('surveyContactId'))) { $this->surveyContactId->setTitle(Model::getUserNameById($userId)); } $this->_initPositionSubForm(); $this->addElement( 'multiDraggableSelect', 'permission', [ 'label' => 'Management Privileges', 'multioptions' => $this->_getUsers4Permission(), 'description' => 'These are individuals at the company who have "management privileges" when logging ' . 'in to the website which include editing the company profile.', 'labels' => [ 'itemsSelected' => 'individuals selected', 'availableTitle' => 'No Permission', 'selectedTitle' => 'Those with Permission', ] ] ); return $this; } protected function _getUsers4Permission() { $table = new Qs_Db_Table('User'); $columns = ['id', new Zend_Db_Expr('CONCAT_WS(" ", IFNULL(`nickname`, `firstName`), `lastName`)')]; $where = [ '`companyId` = ?' => $this->_primaryKey['id'], '`status` IN (?)' => [UserEntity::STATUS_ACTIVE, UserEntity::STATUS_INACTIVE] ]; return $table->get4Select($columns, $where, 'lastName'); } protected function _initPositionSubForm() { $required = true; if ($this->isSubmitted()) { if (!in_array($this->_getData('type'), [Entity::TYPE_REGULAR])) { $required = false; } } $defaults = $this->_getData('position', []); $form = new PositionSubForm(['label' => 'Positions', 'required' => $required, 'defaults' => $defaults]); if (($positions = $this->_getData('position'))) { foreach (array_keys($positions) as $index) { $options = ['companyId' => $this->_primaryKey['id']]; if (($rowDefaults = Qs_Array::get($defaults, (string) $index, []))) { $options['defaults'] = $rowDefaults; } $itemForm = new PositionItemSubForm($options); $form->addSubForm($itemForm, (string) $index); } } else { $form->addSubForm(new PositionItemSubForm(['companyId' => $this->_primaryKey['id']]), '0'); // add empty row } $this->addSubForm($form, 'position'); return $this; } protected function _addResources() { $doc = $this->getDoc(); $doc->addScript('js/jquery.defaultHint.js', [], 'defaultHint'); $doc->addStylesheet('css/thirdpart/jquery-ui.css'); $doc->addScript('js/jquery-ui.js'); $doc->addScript('js/app/company/admin/form.js'); $this->getSubForm('district')->initRender(); $mnHouseId = $this->getSubForm('district')->mnHouse->getId(); $usCongressionalId = $this->getSubForm('district')->usCongressional->getId(); $options = [ 'formId' => $this->getId(), 'typeField' => $this->type->getId(), 'associateCategoryField' => $this->associateCategory->getId(), 'districtDataUrl' => Model::getDistrictDataUrl(), 'districtFields' => [$mnHouseId, $usCongressionalId], 'hintOptions' => [ 'elements' => [ ['selector' => '#' . $mnHouseId, 'hint' => 'Autocomplete field'], ['selector' => '#' . $usCongressionalId, 'hint' => 'Autocomplete field'], ], 'useWrapper' => true ], ]; /** @var \Qs_Form_Element_Autocomplete $surveyContact */ if ($surveyContact = $this->getElement('surveyContactId')) { $options['hintOptions']['elements'][] = [ 'selector' => '#' . $surveyContact->getDisplayId(), 'hint' => 'Autocomplete field']; } $doc->addInitObject('app.company.admin.Form', [$options], 'companyEditForm'); return self::_addResourcesTrait(); } protected function _getRequiredFiles(Qs_Form $form = null) { /* prevent default actions */ return []; } }