Пример #1
0
 private function travelCreateDepartment($items, $companyId, $parentId = null, $privateSource = \Home\Model\Consts::PRIVATE_SOURCE_OFFICEVG)
 {
     $result = array();
     if ($items && count($items)) {
         $companyMapper = $this->getServiceLocator()->get('\\Company\\Model\\CompanyMapper');
         $departmentMapper = $this->getServiceLocator()->get('\\Company\\Model\\DepartmentMapper');
         foreach ($items as $node) {
             $item = $node['obj'];
             if ($item['is_company'] == '1') {
                 //nếu dc đanh dấu là company, sẽ kiểm tra nếu company chưa tồn tại thì tạo mới company.
                 //Sau đó chạy đệ quy với companyId mới
                 $company = new \Company\Model\Company();
                 $company->setOneofficeId($item['ID']);
                 $company->setPrivateSource($privateSource);
                 if (!$companyMapper->isExistedOfficeId($company)) {
                     $company->setName($item['title']);
                     $company->setParentId($companyId);
                     if (!$companyMapper->isExisted($company)) {
                         $company->setCreatedById(1);
                         $company->setCreatedDateTime(DateBase::getCurrentDateTime());
                         $company->setHeadquartersCityId(2);
                         $company->setHeadquartersDistrictId(6);
                         $company->setHeadquartersAddress('51 Lê Đại Hành');
                     } else {
                         $company->setParentId($companyId);
                     }
                 } else {
                     $company->setParentId($companyId);
                 }
                 $companyMapper->save($company);
                 if (isset($node['childs']) && count($node['childs'])) {
                     $this->travelCreateDepartment($node['childs'], $company->getId(), null, $privateSource);
                 }
             } else {
                 $department = new \Company\Model\Department();
                 $department->setOneofficeId($item['ID']);
                 $department->setPrivateSource($privateSource);
                 if (!$departmentMapper->isExistedOfficeId($department)) {
                     $department->setName($item['title']);
                     $department->setParentId($parentId);
                     $department->setCompanyId($companyId);
                     if (!$departmentMapper->isExisted($department)) {
                         $department->setStatus(\Company\Model\Department::STATUS_ACTIVE);
                         $department->setCreatedById(1);
                         $department->setCreatedDateTime(DateBase::getCurrentDateTime());
                     }
                 } else {
                     $department->setParentId($parentId);
                     $department->setCompanyId($companyId);
                 }
                 $departmentMapper->save($department);
                 if (isset($node['childs']) && count($node['childs'])) {
                     $this->travelCreateDepartment($node['childs'], $companyId, $department->getId(), $privateSource);
                 }
             }
         }
     }
 }