getPrimaryKey())) { return ('y' == $this->_getFromDb($key, 'isApprovalEmailSent')); } return false; } public function setApprovalEmailSent() { if (($key = $this->getPrimaryKey())) { return $this->_getTable()->updateByKey(['isApprovalEmailSent' => 'y'], $key); } return false; } public function getFilterFields() { if (!$this->_filterFields) { $filterFields = $this->getConfig('filterFields'); $filterFields = ($filterFields) ? $filterFields->toArray() : []; $this->_filterFields = $filterFields; } return $this->_filterFields; } protected function _getReorderSelect() { $select = parent::_getReorderSelect(); $select->join( $this->_getPair('LeadershipGroup'), '`LeadershipGroup`.`id` = `User`.`leadershipGroupId`', array( 'category' => 'IFNULL(`LeadershipGroup`.`name`, " --- ")', 'categoryEnabled' => 'IFNULL(`LeadershipGroup`.`enabled`, "y")', 'leadershipGroupName' => 'name', ) ); return $select; } protected function _filter(\Zend_Db_Select $select) { $select->where('`' . $this->_tableAlias . '`.`bought` = "y"'); if ($listId = Qs_Array::get($this->_filter, 'listId')) { $select->where('EXISTS (' . $this->_getListsFilterSelect($listId) . ')'); } return parent::_filter($select); } protected function _getListsFilterSelect($listId) { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'List'), [new Zend_Db_Expr('1')]); $select->where('`' . $this->_tableAlias . 'List`.`userId` = `' . $this->_tableAlias . '`.`id`'); $select->where('`' . $this->_tableAlias . 'List`.`listId` = ?', $listId, Qs_Db::INT_TYPE); $select->limit(1); return $select; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['reorderLeadershipTitle'] = Model::getFullNameWithNicknameAndPositionDbExpr(); return $columns; } protected function _getFromDbSelect($primaryKey) { $select = parent::_getFromDbSelect($primaryKey); $select->joinLeft($this->_getPair('Company'), '`Company`.`id` = `User`.`companyId`', ['companyName' => 'name']); $select->joinLeft($this->_getPair('UserStatus', 's'), '`s`.`id` = `User`.`status`', ['statusTitle' => 'title']); $select->joinLeft($this->_getPair('LeadershipGroup', 'ld'), '`ld`.`id` = `User`.`leadershipGroupId`', ['leadershipGroupTitle' => 'name']); return $select; } public function getCompanyTitleById($id) { $select = $this->_getCompanySelect(); $select->where('`id` = ?', $id, Qs_Db::INT_TYPE); $row = $this->_db->fetchRow($select); return Qs_Array::get($row, 'title'); } protected function _insertDependency() { parent::_insertDependency(); if (array_key_exists('mailing', $this->_data)) { $this->_saveAddress('mailing', $this->getData('mailing')); } $this->_insertLists(); return $this; } protected function _saveAddress($type, $address) { $table = $this->_getTable('UserAddress'); $key = ['userId' => $this->getPrimaryKey(), 'type' => $type]; if (($row = $table->searchBy($key))) { $table->updateByKey($address, $key); } else { $address = array_merge($address, $key); $table->insert($address); } return $this; } protected function _updateDependency() { parent::_updateDependency(); if (array_key_exists('mailing', $this->_data)) { $this->_saveAddress('mailing', $this->getData('mailing')); } $this->_deleteLists(); $this->_insertLists(); if ($this->_previousData) { if ($this->_previousData['companyId'] != $this->_getCurrentData('companyId')) { $where = $this->_db->quoteInto('`userId` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); $this->_getTable('CompanyPermission')->delete($where); $this->_getTable('CompanyUtilityStaff')->delete($where); if ($this->_previousData['companyId']) { $where = $this->_db->quoteInto('`surveyContactId` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); $this->_getTable('Company')->update(['surveyContactId' => null], $where); } } if (!$this->_data['leadershipGroupId'] && ($this->_previousData['leadershipPhoto']) ) { (new Qs_File_Transfer_Adapter_Db())->delete($this->_previousData['leadershipPhoto'], 'leadershipPhoto'); } } return $this; } protected function _deleteDependency() { parent::_deleteDependency(); $this->_deleteLists(); $this->_unsetFromCart(); return $this; } protected function _unsetFromCart() { $table = $this->_getTable('Cart'); $table->update(['userId' => 0], 'userId = ' . $this->_db->quote($this->_primaryKey, Qs_Db::INT_TYPE)); return $this; } protected function _insertLists() { $table = $this->_getTable($this->_tableAlias . 'List'); $lists = Qs_Array::get($this->_data, 'lists'); if ($lists) { foreach ($lists as $listId) { $row['userId'] = $this->getPrimaryKey(); $row['listId'] = $listId; $table->insert($row); } } return $this; } protected function _deleteLists() { $this->_deleteIds($this->_tableAlias . 'List', 'userId'); return $this; } public function getExportColumns() { $columns = $this->getConfig('export')->fields->{Module::MODE_ASSOCIATION}->toArray(); $dynamicColumns = []; // add lists $lists = Model::getLists(); foreach ($lists as $key => $title) { $dynamicColumns[Entity::EXPORT_LIST_PREFIX . $key] = $title; } $splitColumn = $this->getConfig('export')->dynamicFieldsAfter; if (false !== ($splitPos = Qs_Array::keyPos($splitColumn, $columns))) { $splitPos++; $after = array_slice($columns, 0, $splitPos, true); $before = array_slice($columns, $splitPos, null, true); $columns = $after + $dynamicColumns + $before; } else { $columns += $dynamicColumns; } foreach ($columns as $key => $title) { if ('' == $title) { if (strtolower($key) === $key) { $title = ucfirst($key); } else { $title = ucfirst(Qs_String::decamelize($key)); } $columns[$key] = $title; } } return $columns; } public function getList4ExportStatement() { $select = $this->_db->select(); $select->from($this->_getPair()); $surveyContactExpr = new Zend_Db_Expr('IF (`Company`.`surveyContactId` = `User`.`id`, "Yes", "No")'); $select->joinLeft( $this->_getPair('Company'), '`Company`.`id` = `User`.`companyId`', ['companyName' => 'name', 'surveyContact' => $surveyContactExpr] ); $select->joinLeft( $this->_getPair('CompanyStatus', 'cs'), '`cs`.`id` = `Company`.`status`', ['companyStatus' => 'title'] ); $select->joinLeft( $this->_getPair('LeadershipGroup', 'lg'), '`lg`.`id` = `User`.`leadershipGroupId`', array('leadershipGroup' => 'name') ); $select->joinLeft( $this->_getPair('CompanyPermission', 'cp'), '`cp`.`companyId` = `Company`.`id` AND `cp`.`userId` = `User`.`id`', ['managementPrivileges' => 'IF(`cp`.`userId` IS NULL, "n", "y")'] ); $select->joinLeft( $this->_getPair($this->_tableAlias . 'Status', 's'), '`s`.`id` = `User`.`status`', array('status' => 'title') ); $select->where('`User`.`bought` = "y"'); $this->_filter($select); $this->_applySelectOptions($select, $this->_selectOptions); return $select->query(); } public function _prepareExportMailingAddress(array &$data) { if (!array_key_exists(Entity::ADDRESS_MAILING, $data)) { $data[Entity::ADDRESS_MAILING] = []; } Model::filterAddress($data[Entity::ADDRESS_MAILING]); $data[Entity::ADDRESS_MAILING] = array_filter($data[Entity::ADDRESS_MAILING]); if (empty($data[Entity::ADDRESS_MAILING])) { $data[Entity::ADDRESS_MAILING] = Model::getCompanyAddress($data['companyId'], 'mailing', []); } return $this; } public function prepareExportRow(array &$data) { $data['leadershipBio'] = Qs_String::stripTags($data['leadershipBio']); // prepare addresses $this->_addAddressesFromDb($data); if (array_key_exists(Entity::ADDRESS_SHIPPING, $data) && 'y' == Qs_Array::get($data[Entity::ADDRESS_SHIPPING], 'asBilling') ) { $data[Entity::ADDRESS_SHIPPING] = array_merge( $data[Entity::ADDRESS_SHIPPING], $data[Entity::ADDRESS_BILLING] ); } else { $data[Entity::ADDRESS_SHIPPING]['asBilling'] = 'n'; } $this->_prepareExportMailingAddress($data); foreach (array(Entity::ADDRESS_BILLING, Entity::ADDRESS_SHIPPING, Entity::ADDRESS_MAILING) as $type) { if (array_key_exists($type, $data)) { foreach ($data[$type] as $key => $val) { $index = $type . ucfirst($key); $data[$index] = $val; } } } $data['managementPrivileges'] = Qs_Array::get(['y' => 'Yes', 'n' => 'No'], $data['managementPrivileges']); $this->_addListsFromDb($data); if (array_key_exists('lists', $data)) { $lists = array_keys(Model::getLists()); foreach ($lists as $listId) { $key = Entity::EXPORT_LIST_PREFIX . $listId; $data[$key] = (in_array($listId, $data['lists'])) ? 'Yes' : 'No'; } } return $this; } protected function _getCurrentData($field = null) { if (($key = $this->getPrimaryKey())) { return $this->_getFromDb($key, $field); } return false; } protected function _initPreviousData() { $this->_previousData = $this->_getCurrentData(); return $this; } public function update(array $data = null) { $this->_initPreviousData(); return parent::update($data); } }