public static function setValues($fields, $bind_id, $bind_type, $sf_user, $request) { $extra_fields = ExtraFieldsList::getFieldsByType($bind_type, $sf_user, false, array('all' => true)); foreach ($extra_fields as $ef) { //escape view only fields if (in_array($sf_user->getAttribute('users_group_id'), explode(',', $ef['view_only_access']))) { continue; } if (isset($fields[$ef['id']])) { $v = $fields[$ef['id']]; } else { $v = ''; } $v = app::strip_tags($v); if ($ef['type'] == 'file') { $files = $request->getFiles(); if ($extra_fields_files_delete = $request->getParameter('extra_fields_files_delete')) { if (isset($extra_fields_files_delete[$ef['id']])) { if ($a = Doctrine_Core::getTable('Attachments')->find($extra_fields_files_delete[$ef['id']])) { if (is_file($file_path = sfConfig::get('sf_upload_dir') . '/attachments/' . $a->getFile())) { unlink($file_path); } $a->delete(); } } } if ($extra_fields_files = $request->getParameter('extra_fields_files')) { if (isset($extra_fields_files[$ef['id']])) { $v = $extra_fields_files[$ef['id']]; } } if (strlen($files['extra_fields'][$ef['id']]['name']) > 0) { if ($v > 0) { if ($a = Doctrine_Core::getTable('Attachments')->find($v)) { if (is_file($file_path = sfConfig::get('sf_upload_dir') . '/attachments/' . $a->getFile())) { unlink($file_path); } $a->delete(); } } $file = $files['extra_fields'][$ef['id']]; $filename = rand(111111, 999999) . '-' . $file['name']; move_uploaded_file($file['tmp_name'], sfConfig::get('sf_upload_dir') . '/attachments/' . $filename); $a = new Attachments(); $a->setFile($filename); $a->setBindType('extra_fields'); $a->setBindId($ef['id']); $a->save(); $v = $a->getId(); } } elseif ($ef['type'] == 'date_dropdown') { $v = implode('-', $v); } elseif ($ef['type'] == 'date_range') { $v = implode('|', $v); } elseif ($ef['type'] == 'checkbox' and is_array($v)) { $v = implode("\n", $v); } elseif (is_array($v)) { $v = implode(',', $v); } $f = Doctrine_Core::getTable('ExtraFieldsList')->createQuery()->addWhere('bind_id=?', $bind_id)->addWhere('extra_fields_id=?', $ef['id'])->fetchOne(); if ($f) { $f->setValue($v); $f->save(); } else { $f = new ExtraFieldsList(); $f->setBindId($bind_id); $f->setExtraFieldsId($ef['id']); $f->setValue($v); $f->save(); } } }
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'); } } } } }