'mailingAddress', // methods: $this->(get|save)MailingAddress(...); * 'references', // methods: $this->(get|save)References(...); * ] */ protected $dependencies = [ 'mailing' => 'mailingAddress', 'questionnaire', 'education', 'formerEmployments', 'references', 'relationshipWithEmployers', ]; /** * @var array e.g.: [ * 'enabledTitle' => ['field' => 'enabled', 'collection' => ['y' => 'Yes', 'n' => 'No']], * ] */ protected $staticDependencies = []; public function getStaticDependencies() { return $this->staticDependencies; } public function setStaticDependencies(array $staticDependencies) { $this->staticDependencies = $staticDependencies; return $this; } protected function saveDependencies(array $data) { $key = ['employeeId' => $data['id']]; foreach ($this->dependencies as $index => $alias) { if (is_numeric($index)) { $index = $alias; } $method = 'save' . ucfirst($alias); if (array_key_exists($index, $data) && method_exists($this, $method)) { $this->$method($data[$index], $key); } } return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ /** * @param array $questions * @param array $key - e.g. ['employeeId' => 1] * @return $this */ private function saveQuestionnaire(array $questions, array $key) { $list = []; foreach ($questions as $question) { $list[] = Qs_Array::map($question, ['questionId' => 'id', 'answer', 'explanation']); } $this->getTable('Employee2Questionnaire')->saveRelations($list, $key); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveEducation(array $institutions, array $key) { $this->getTable('EmployeeEducation')->saveRelations($institutions, $key); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveFormerEmployments(array $employments, array $key) { $this->getTable('EmployeeFormerEmployment')->saveRelations($employments, $key); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveReferences(array $references, array $key) { $this->getTable('EmployeeReference')->saveRelations($references, $key); return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function saveRelationshipWithEmployers(array $relationships, array $key) { $this->getTable('EmployeeRelationshipWithEmployer')->saveRelations($relationships, $key); return $this; } public function injectDependencies(array &$data) { $key = ['employeeId' => $data['id']]; foreach ($this->dependencies as $index => $alias) { if (is_numeric($index)) { $index = $alias; } $method = 'get' . ucfirst($alias); if (method_exists($this, $method)) { $data[$index] = $this->$method($key); } } return $this; } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getQuestionnaire(array $key) { return $this->getTable('Employee2Questionnaire')->getList('*', $key); } public function getFullQuestionnaire(array $key) { $select = $this->db->select(); $select->from(Qs_Db::getPair('EmployeeQuestionnaire', 'q'), ['id', 'question']); $select->join( Qs_Db::getPair('Employee2Questionnaire', 'eq'), '`eq`.`questionId` = `q`.`id`', ['answer', 'explanation'] ); $select->where(Qs_Db::getWhereSql($key)); $select->order('q.sorter'); return $this->db->fetchAll($select); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getFormerEmployments(array $key) { return $this->getTable('EmployeeFormerEmployment')->getList('*', $key); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getEducation(array $key) { return $this->getTable('EmployeeEducation')->getList('*', $key); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getFormerEmployment(array $key) { return $this->getTable('EmployeeFormerEmployment')->getList('*', $key); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getReferences(array $key) { return $this->getTable('EmployeeReference')->getList('*', $key); } /** @noinspection PhpUnusedPrivateMethodInspection */ private function getRelationshipWithEmployers(array $key) { return $this->getTable('EmployeeRelationshipWithEmployer')->getList('*', $key); } public function getQuestions(array $filter) { return (new QuestionnaireModel([ 'tableAlias' => 'EmployeeQuestionnaire', 'db' => $this->db, ] ))->getList($filter); } public function getUsedQuestions(array $key = []) { $select = $this->db->select(); $columns = (!empty($key)) ? ['*'] : ['id', 'question']; $select->from(Qs_Db::getPair('EmployeeQuestionnaire', 'q'), $columns); $select->join( Qs_Db::getPair('Employee2Questionnaire', 'eq'), '`eq`.`questionId` = `q`.`id`', ['answer', 'explanation'] ); if (!empty($key)) { $select->where(Qs_Db::getWhereSql($key)); } else { $select->group('q.id'); } $select->order('q.sorter'); $list = $this->db->fetchAll($select); $number = 'A'; foreach ($list as &$question) { $question['number'] = $number++; } return $list; } public function injectStaticDependencies(array &$data) { foreach ($this->getStaticDependencies() as $field => $options) { if (($value = Qs_Array::get($data, $options['field']))) { $data[$field] = Qs_Array::get($options['collection'], $value); } } return $this; } }