Ejemplo n.º 1
0
 public function executeXlsTasksImport(sfWebRequest $request)
 {
     app::setPageTitle('Import Spreadsheet', $this->getResponse());
     if ($request->isMethod(sfRequest::PUT)) {
         if ($request->hasParameter('import_file')) {
             if (is_file($import_spreadsheet_file = sfConfig::get('sf_upload_dir') . '/' . $request->getParameter('import_file'))) {
                 $import_fields = $this->getUser()->getAttribute('import_fields');
                 $data = new Spreadsheet_Excel_Reader($import_spreadsheet_file);
                 $projects_id = $request->getParameter('projects_id');
                 if ($request->getParameter('import_first_row') == 1) {
                     $first_row = 1;
                 } elseif ($data->rowcount() > 2) {
                     $first_row = 2;
                 } else {
                     $first_row = 1;
                 }
                 for ($i = $first_row; $i <= $data->rowcount(); $i++) {
                     $t = new Tasks();
                     $t->setCreatedBy($this->getUser()->getAttribute('id'))->setCreatedAt(date('Y-m-d H:i:s'))->setProjectsId($request->getParameter('projects_id'));
                     $extra_fields = array();
                     for ($j = 1; $j <= $data->colcount(); $j++) {
                         if (isset($import_fields[$j])) {
                             $v = $data->val($i, $j);
                             if (strlen(trim($v)) == 0) {
                                 continue;
                             }
                             switch ($import_fields[$j]) {
                                 case 'TasksGroups':
                                     if ($id = app::getProjectCfgItemIdByName($v, 'TasksGroups', $projects_id)) {
                                         $t->setTasksGroupsId($id);
                                     } else {
                                         $cfg = new TasksGroups();
                                         $cfg->setName($v);
                                         $cfg->setProjectsId($projects_id);
                                         $cfg->save();
                                         $t->setTasksGroupsId($cfg->getId());
                                     }
                                     break;
                                 case 'Versions':
                                     if ($id = app::getProjectCfgItemIdByName($v, 'Versions', $projects_id)) {
                                         $t->setVersionsId($id);
                                     } else {
                                         $cfg = new Versions();
                                         $cfg->setName($v);
                                         $cfg->setProjectsId($projects_id);
                                         $cfg->save();
                                         $t->setVersionsId($cfg->getId());
                                     }
                                     break;
                                 case 'ProjectsPhases':
                                     if ($id = app::getProjectCfgItemIdByName($v, 'ProjectsPhases', $projects_id)) {
                                         $t->setProjectsPhasesId($id);
                                     } else {
                                         $cfg = new ProjectsPhases();
                                         $cfg->setName($v);
                                         $cfg->setProjectsId($projects_id);
                                         $cfg->save();
                                         $t->setProjectsPhasesId($cfg->getId());
                                     }
                                     break;
                                 case 'TasksPriority':
                                     if ($id = app::getCfgItemIdByName($v, 'TasksPriority')) {
                                         $t->setTasksPriorityId($id);
                                     } else {
                                         $cfg = new TasksPriority();
                                         $cfg->setName($v);
                                         $cfg->save();
                                         $t->setTasksPriorityId($cfg->getId());
                                     }
                                     break;
                                 case 'TasksLabels':
                                     if ($id = app::getCfgItemIdByName($v, 'TasksLabels')) {
                                         $t->setTasksLabelId($id);
                                     } else {
                                         $cfg = new TasksLabels();
                                         $cfg->setName($v);
                                         $cfg->save();
                                         $t->setTasksLabelId($cfg->getId());
                                     }
                                     break;
                                 case 'name':
                                     $t->setName($v);
                                     break;
                                 case 'TasksStatus':
                                     if ($id = app::getCfgItemIdByName($v, 'TasksStatus')) {
                                         $t->setTasksStatusId($id);
                                     } else {
                                         $cfg = new TasksStatus();
                                         $cfg->setName($v);
                                         $cfg->save();
                                         $t->setTasksStatusId($cfg->getId());
                                     }
                                     break;
                                 case 'TasksTypes':
                                     if ($id = app::getCfgItemIdByName($v, 'TasksTypes')) {
                                         $t->setTasksTypeId($id);
                                     } else {
                                         $cfg = new TasksTypes();
                                         $cfg->setName($v);
                                         $cfg->save();
                                         $t->setTasksTypeId($cfg->getId());
                                     }
                                     break;
                                     break;
                                 case 'assigned_to':
                                     $assigned_to = array();
                                     foreach (explode(',', $v) as $n) {
                                         if ($user = Doctrine_Core::getTable('Users')->createQuery()->addWhere('name=?', trim($n))->fetchOne()) {
                                             $assigned_to[] = $user->getId();
                                         }
                                     }
                                     $t->setAssignedTo(implode(',', $assigned_to));
                                     break;
                                 case 'estimated_time':
                                     $t->setEstimatedTime($v);
                                     break;
                                 case 'start_date':
                                     $t->setStartDate(date('Y-m-d', strtotime($v)));
                                     break;
                                 case 'due_date':
                                     $t->setDueDate(date('Y-m-d', strtotime($v)));
                                     break;
                                 case 'progress':
                                     $t->setProgress($v);
                                     break;
                             }
                             if (strstr($import_fields[$j], 'extra_field_')) {
                                 $extra_fields[str_replace('extra_field_', '', $import_fields[$j])] = $v;
                             }
                         }
                     }
                     $t->save();
                     foreach ($extra_fields as $id => $v) {
                         $f = new ExtraFieldsList();
                         $f->setBindId($t->getId());
                         $f->setExtraFieldsId($id);
                         $f->setValue($v);
                         $f->save();
                     }
                 }
                 $this->getUser()->setFlash('userNotices', t::__('Spreadsheet imported'));
                 $this->redirect('tasks/index?projects_id=' . $request->getParameter('projects_id'));
             }
         } elseif (($projects_id = $request->getParameter('projects_id')) > 0) {
             $f = $request->getFiles();
             if ($f['import_file']) {
                 $this->getUser()->setAttribute('import_fields', array());
                 move_uploaded_file($f['import_file']['tmp_name'], sfConfig::get('sf_upload_dir') . '/' . $f['import_file']['name']);
                 $this->import_file = $f['import_file']['name'];
                 if (is_file($import_spreadsheet_file = sfConfig::get('sf_upload_dir') . '/' . $this->import_file)) {
                     $this->data = new Spreadsheet_Excel_Reader($import_spreadsheet_file);
                     $this->setTemplate('xlsTasksImportBind');
                 } else {
                     $this->getUser()->setFlash('userNotices', array('type' => 'error', 'text' => t::__('There is an error with uploading file. Please try again with less file size.')));
                     $this->redirect('tools/xlsTasksImport');
                 }
             }
         }
     }
 }