[ AdminAcl::ACL_LICENSING_ID => [ 'list', 'view', 'cancel', 'new', 'insert', 'edit', 'update', 'back', 'renderFormPart', 'approve', 'pending', 'decline', 'renew', 'disable', 'exportToCsv', 'exportToPdf', 'downloadPdfExport', 'uploadBgScreeningReport', ], AdminAcl::ACL_LICENSING_BACKGROUND_SCREENING => [ 'list', 'view', 'cancel', 'back', 'renderFormPart', 'uploadBgScreeningReport', ], ], AdminObj::TYPE_TRIBAL_ACCOUNT => [ TribalAccountAcl::ACL_VENDOR_SERVICES_READ_ID => [ 'view', 'back', ], TribalAccountAcl::ACL_LICENSES_READ_ID => [ 'list', 'view', 'back', 'cancel', 'renderFormPart', 'exportToCsv', 'exportToPdf', 'downloadPdfExport', ], TribalAccountAcl::ACL_LICENSES_READ_APPROVE_ID => [ 'list', 'view', 'cancel', 'new', 'insert', 'edit', 'update', 'back', 'renderFormPart', 'approve', 'pending', 'exportToCsv', 'exportToPdf', 'downloadPdfExport', ], TribalAccountAcl::ACL_LICENSES_READ_APPROVE_DECLINE_ID => [ 'list', 'view', 'cancel', 'new', 'insert', 'edit', 'update', 'back', 'renderFormPart', 'approve', 'pending', 'decline', 'exportToCsv', 'exportToPdf', 'downloadPdfExport', ], ], ]; protected function _init() { $this->_messageTemplates[self::MSG_DISABLED] = '%itemName% has been disabled'; $this->_messageTemplates[self::MSG_APPROVED] = '%itemName% has been approved'; $this->_messageTemplates[self::MSG_PENDING] = '%itemName% status has been changed to pending'; $this->_messageTemplates[self::MSG_DECLINED] = '%itemName% has been declined'; $this->_messageTemplates[self::MSG_RENEWED] = '%itemName% has been renewed'; $this->_messageTemplates[self::MSG_SURCHARGE] = '%itemName% Vendors have been notified about Premium'; return parent::_init(); } protected function _initAction() { $this->_initPermittedActions(); return parent::_initAction(); } protected function _initPermittedActions() { $aclIds = AclPermission::getAllowedResources(); if (AclPermission::isTribalAccount()) { $this->_getDataObj()->setTribeId(AclPermission::getAdminTribeId()); $type = AdminObj::TYPE_TRIBAL_ACCOUNT; if (in_array(TribalAccountAcl::ACL_LICENSES_READ_ID, $aclIds)) { $this->_actions = array_merge( $this->_actions, $this->_allowedActions[$type][TribalAccountAcl::ACL_LICENSES_READ_ID] ); } if (in_array(TribalAccountAcl::ACL_LICENSES_READ_APPROVE_ID, $aclIds)) { $this->_actions = array_merge( $this->_actions, $this->_allowedActions[$type][TribalAccountAcl::ACL_LICENSES_READ_APPROVE_ID] ); } if (in_array(TribalAccountAcl::ACL_LICENSES_READ_APPROVE_DECLINE_ID, $aclIds)) { $this->_actions = array_merge( $this->_actions, $this->_allowedActions[$type][TribalAccountAcl::ACL_LICENSES_READ_APPROVE_DECLINE_ID]); } if (in_array(TribalAccountAcl::ACL_VENDOR_SERVICES_READ_ID, $aclIds)) { $this->_actions = array_merge( $this->_actions, $this->_allowedActions[$type][TribalAccountAcl::ACL_VENDOR_SERVICES_READ_ID] ); } } else { $availableAclIds = array_intersect($aclIds, array_keys($this->_allowedActions[AdminObj::TYPE_ADMIN])); foreach ($availableAclIds as $availableAclId) { $this->_actions = array_merge( $this->_actions, $this->_allowedActions[AdminObj::TYPE_ADMIN][$availableAclId] ); } $this->_actions = array_unique($this->_actions); if ($this->getDoc()->getAuth()->getSuMode()) { $this->_actions[] = 'delete'; } } return $this; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['allowDelete'] = in_array('delete', $this->_actions); $options['viewExportPdf'] = $this->getDoc()->getAuth()->getSuMode() && constant('DEBUG'); $options['allowFullPermission'] = (!AclPermission::isTribalAccount() && in_array(AdminAcl::ACL_LICENSING_ID, AdminAcl::getAllowedResources())); $options['allowedActions'] = $this->_actions; return $options; } protected function _getList() { if (null === $this->_list) { parent::_getList(); if (in_array('uploadBgScreeningReport', $this->_actions)) { $this->_list->updateColumn('bgScreeningReport', [ 'url' => self::getPage('fullAlias') . '?action=uploadBgScreeningReport&id={id}', ]); } } return $this->_list; } protected function _getActionTribeIds() { $admin = App_Admin_Auth::getInstance()->getData(); if (AdminObj::TYPE_TRIBAL_ACCOUNT == $admin['type']) { if (!$admin['tribeId']) { throw new RuntimeException('Tribe ID is not set for Tribal Admin'); } $tribeIds = [$admin['tribeId']]; } else { $tribeIds = Qs_Request::getRequestValue('tribes', []); } return $tribeIds; } protected function _getActionTribeData() { $data = []; $admin = App_Admin_Auth::getInstance()->getData(); if (AdminObj::TYPE_TRIBAL_ACCOUNT == $admin['type']) { if (!$admin['tribeId']) { throw new RuntimeException('Tribe ID is not set for Tribal Admin'); } $useSurcharge = $this->_getDataObj()->getModel()->getTribeUseSurcharge($admin['tribeId']); if ($useSurcharge == 'y') { $form = $this->_getSurchargeForm(); if ($form->validate()) { $values = $form->getValues(); $data['tribeIds'] = [$admin['tribeId']]; $data['surcharge'] = ('y' == $values['useSurcharge'] && $values['surcharge']) ? [$admin['tribeId'] => $values['surcharge']] : [$admin['tribeId'] => null]; } else { $data['tribeIds'] = null; $data['surcharge'] = null; } } else { $data['tribeIds'] = [$admin['tribeId']]; $data['surcharge'] = [$admin['tribeId'] => null]; } } else { $data['tribeIds'] = Qs_Request::getRequestValue('tribes', []); $surcharge = Qs_Request::getRequestValue('surcharge', []); $data['surcharge'] = $this->_prepareSurchargeData($data['tribeIds'], $surcharge); } return $data; } protected function _prepareSurchargeData($tribeIds, $surcharges) { $result = null; $tribeUseSurcharge = $this->_getDataObj()->getModel()->getTribeUseSurcharge(); foreach ($tribeIds as $tribeId) { if (isset($surcharges[$tribeId]) && $surcharges[$tribeId] && $tribeUseSurcharge[$tribeId] == 'y') { $result[$tribeId] = filter_var($surcharges[$tribeId], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); } else { $result[$tribeId] = null; } } return $result; } protected function _getSurchargeForm(array $options = []) { return $this->_getFormInstance('surcharge', $options); } protected function _addTribesSelectItem(array $options = []) { $dataObj = $this->_getDataObj(); $license = $dataObj->getData(); if (AclPermission::isTribalAccount()) { $useSurcharge = $this->_getDataObj()->getModel()->getTribeUseSurcharge(AclPermission::getAdminTribeId()); if ($useSurcharge == 'y') { $this->_doc->addScript('js/app/license/tribeSurcharge.js'); $surcharge = Qs_Array::fetchPairs($license['tribes'], ['tribeId', 'surcharge'])[AclPermission::getAdminTribeId()]; $form = $this->_getSurchargeForm(['defaults' => ['surcharge' => $surcharge]]); $form->setDefaults(); $this->_doc->addInitObject('app.license.tribeSurcharge', [[ 'id' => $form->getId(), 'nodes' => [ 'useSurcharge' => '[name="useSurcharge"]', 'surcharge' => '[name="surcharge"]', ], ]]); $item = array_merge([ 'tpl' => $this->getTemplate('surcharge-form.tpl'), 'form' => $form->render(), ], $options); } } else { $this->_doc->addScript('js/jquery-autoNumeric.js'); $this->_doc->addScript('js/app/license/selectTribeSurcharge.js'); $this->_doc->addInitObject('app.license.selectTribeSurcharge', [[ 'id' => 'select-tribes-form', 'surchargeElementPrefix' => 'surcharge-', 'tribesElementPrefix' => 'tribes-', 'nodes' => [ 'tribes' => '[name="tribes[]"]', 'surcharges' => '[name^="surcharges"]', ], ]]); $item = array_merge([ 'tpl' => $this->getTemplate('tribes-form.tpl'), 'license' => $license, 'tribes' => $dataObj->getTribe4Select(), 'action' => $this->_action, 'backUrl' => $this->_getBackUrl(), ], $options); } $this->_addItem($item); return $this; } protected function _doDisable() { $this->_getDataObj()->disable(); $this->_setBackMessage(self::MSG_DISABLED); Mail::sendLicenseDisabledToUser($this->_getDataObj()->getPrimaryKey()); $this->_doBack(); return $this; } protected function _doRenew() { try { $this->_getDataObj()->renew(); $this->_setBackMessage(self::MSG_RENEWED); Mail::sendLicenseRenewedToAdmin($this->_getDataObj()->getPrimaryKey()); Mail::sendLicenseRenewedToUser($this->_getDataObj()->getPrimaryKey()); } catch (RuntimeException $e) { $this->_setBackError($e->getMessage()); } $this->_doBack(); return $this; } protected function _doApproveAjax() { $form = $this->_getSurchargeForm(['defaults' => Qs_Request::getPost()]); $this->_displayJson($form->validateAjax()); } protected function _doApprove() { try { $actionTribeData = $this->_getActionTribeData(); if (!($tribeIds = $actionTribeData['tribeIds'])) { $this->_addTribesSelectItem(['title' => 'Approving License #' . $this->_getDataObj()->getData('id')]); return $this; } if (!empty($actionTribeData['surcharge'])) { $notEmptySurcharge = array_filter($actionTribeData['surcharge']); $notEmptySurchargeIds = array_keys($notEmptySurcharge); $this->_getDataObj()->updateTribeSurcharge( array_keys($actionTribeData['surcharge']), $actionTribeData['surcharge']); if ($notEmptySurchargeIds) { $this->_getDataObj()->approvePending($notEmptySurchargeIds); $this->_setBackMessage(self::MSG_SURCHARGE); Mail::sendLicenseSurchargeToUser($this->_getDataObj()->getPrimaryKey(), $notEmptySurchargeIds); } //tribeIds without surcharge for approve $tribeIds = array_diff($tribeIds, $notEmptySurchargeIds); } if (!empty($tribeIds)) { //Do Approve $this->_getDataObj()->approve($tribeIds); $this->_setBackMessage(self::MSG_APPROVED); Mail::sendLicenseApprovedToUser($this->_getDataObj()->getPrimaryKey(), $tribeIds); LibraryMail::sendLibraryUpdatedToUser(LibraryObj::RECORD_TYPE_LICENSE, $this->_getDataObj()->getPrimaryKey(), null, $tribeIds); } } catch (RuntimeException $e) { $this->_setBackError($e->getMessage()); } $this->_doBack(); return $this; } protected function _doDecline() { try { if (!($tribeIds = $this->_getActionTribeIds())) { $this->_addTribesSelectItem(['title' => 'Declining License #' . $this->_getDataObj()->getData('id')]); return $this; } $this->_getDataObj()->decline($tribeIds); $this->_setBackMessage(self::MSG_DECLINED); Mail::sendLicenseDeclinedToUser($this->_getDataObj()->getPrimaryKey(), $tribeIds); } catch (RuntimeException $e) { $this->_setBackError($e->getMessage()); } $this->_doBack(); return $this; } protected function _doPending() { try { if (!($tribeIds = $this->_getActionTribeIds())) { $this->_addTribesSelectItem( ['title' => 'Change status to Pending for License #' . $this->_getDataObj()->getData('id')]); return $this; } $this->_getDataObj()->pending($tribeIds); $this->_setBackMessage(self::MSG_PENDING); Mail::sendLicensePendingToUser($this->_getDataObj()->getPrimaryKey(), $tribeIds); } catch (RuntimeException $e) { $this->_setBackError($e->getMessage()); } $this->_doBack(); return $this; } protected function _getFormOptions($type = null) { $options = parent::_getFormOptions($type); $vendorId = null; if ($type == 'new' && ($vendorId = Qs_Request::getGetValue('vendorId'))) { $options['defaults']['vendorId'] = $vendorId; } if (in_array($type, ['new', 'edit'])) { $options['vendorAutocompleteUrl'] = VendorAutocompleteAdminView::getPage('url'); if ($vendorId || ($vendorId = $this->_getDataObj()->getData('vendorId'))) { $options['vendorAutocompleteTitle'] = VendorAutocompleteAdminObj::getAutocompleteItemTitle($vendorId); } $options['questions'] = $this->_getDataObj()->getQuestions(); $options['cancelUrl'] = $this->_getBackUrl(); } if ($type == 'filter') { $options['vendorAutocompleteUrl'] = VendorAutocompleteAdminView::getPage('url'); if (($vendorId = Qs_Request::getGetValue('vendorId'))) { $options['vendorAutocompleteTitle'] = VendorAutocompleteAdminObj::getAutocompleteItemTitle($vendorId); } } return $options; } protected function _getFormInstance($type, array $options = []) { $form = parent::_getFormInstance($type, $options); if ($form instanceof AbstractForm && ($naicsCodes = $form->getElement('naicsCodes'))) { $query = Qs_Request::getRequestValue('query'); $value = Qs_Array::get($options, 'defaults[naicsCodes]', []); if (!empty($value) && isset($value[0]['code'])) { $value = Qs_Array::fetchColAll($value, 'code'); } $naicsCodes->setAttrib('codes', $this->_getDataObj()->getNaicsCodes(null, $query)); $naicsCodes->setAttrib('chosenCodes', $this->_getDataObj()->getNaicsChosenCodes($value)); } return $form; } protected function _initFromForm(Qs_Form $form) { $data = $form->getValues(); $data['ip'] = Qs_Request::getClientIP(); $this->_getDataObj()->initFromForm($data); return $this; } protected function _getDefaultLinks() { $links = parent::_getDefaultLinks(); $query = $this->getFilter(); $query['orderBy'] = $this->_getList()->getOrderBy(); $query = array_filter($query); if (AclPermission::isTribalAccount()) { $query['tribeId'] = AclPermission::getAdminTribeId(); } $links['exportToCsv'] = [ 'title' => 'Export to .csv', 'url' => $this->url(array_merge(['action' => 'exportToCsv'], $query)), ]; $this->injectPdfExportLinks($links, $query); return $links; } protected function _doExportToCsv() { $dataObj = $this->_getDataObj(); if ($dataObj->hasFilter()) { $form = $this->_getFilterForm(); if ($form->validate()) { $dataObj->addFilter($form->getValues()); } } $options = ['order' => $this->_getList()->getOrderBy()]; $dataObj->setSelectOptions($options); $stmt = $dataObj->getList4ExportStatement(); $rowCallback = function () use ($stmt, $dataObj) { $row = $stmt->fetch(); if ($row) { $dataObj->prepareExportRow($row); } return $row; }; $helper = new ExportCsv([ 'fileName' => $this->getConfig('itemsName') . ' {date}.csv', 'columns' => $dataObj->getExportCsvColumns(), 'rowCallback' => $rowCallback, ]); $helper->export(); exit; } protected function _doView() { $options = $this->_doc->getOptions(); $options['doc'] = $this->_doc; $options['licenseId'] = $this->_getDataObj()->getPrimaryKey(); $options['handler'] = 'admin'; $view = new DetailPageView($options); $view->exec(); return $this; } protected function _doUploadBgScreeningReport() { $data = $this->_getDataObj()->getData(); $data = array_merge($data, Qs_Request::getPost()); $form = $this->_getFormInstance('bgScreeningReport', ['defaults' => $data]); $form->setDefaults(); $form->setAction($this->url(['action' => $this->_action])); if (Qs_Request::isPost() && $form->validate()) { $data = $form->getValues(); $this->_getDataObj()->update($data); $this->_setBackMessage(self::MSG_UPDATED); $this->_doBack(); } $this->_addFormItem($form); } }