Exemplo n.º 1
0
 public function runDoImportCSV(TBGRequest $request)
 {
     try {
         if ($request->getParameter('csv_data') == '') {
             throw new Exception(TBGContext::getI18n()->__('No data supplied to import'));
         }
         // Split data into individual lines
         $data = str_replace("\r\n", "\n", $request->getParameter('csv_data'));
         $data = explode("\n", $data);
         if (count($data) <= 1) {
             throw new Exception(TBGContext::getI18n()->__('Insufficient data to import'));
         }
         $headerrow = $data[0];
         $headerrow = html_entity_decode($headerrow, ENT_QUOTES);
         $headerrow = explode(',', $headerrow);
         $headerrow2 = array();
         for ($i = 0; $i != count($headerrow); $i++) {
             $headerrow2[$i] = trim($headerrow[$i], '" ');
         }
         $errors = array();
         // inspect for correct rows
         switch ($request->getParameter('type')) {
             case 'clients':
                 $namecol = null;
                 $emailcol = null;
                 $telephonecol = null;
                 $faxcol = null;
                 $websitecol = null;
                 for ($i = 0; $i != count($headerrow2); $i++) {
                     if ($headerrow2[$i] == 'name') {
                         $namecol = $i;
                     } elseif ($headerrow2[$i] == 'email') {
                         $emailcol = $i;
                     } elseif ($headerrow2[$i] == 'telephone') {
                         $telephonecol = $i;
                     } elseif ($headerrow2[$i] == 'fax') {
                         $faxcol = $i;
                     } elseif ($headerrow2[$i] == 'website') {
                         $websitecol = $i;
                     }
                 }
                 $rowlength = count($headerrow2);
                 if ($namecol === null) {
                     $errors[] = TBGContext::getI18n()->__('Required column \'%col%\' not found in header row', array('%col%' => 'name'));
                 }
                 break;
             case 'projects':
                 $namecol = null;
                 $prefix = null;
                 $scrum = null;
                 $owner = null;
                 $owner_type = null;
                 $lead = null;
                 $lead_type = null;
                 $qa = null;
                 $qa_type = null;
                 $descr = null;
                 $doc_url = null;
                 $freelance = null;
                 $en_builds = null;
                 $en_comps = null;
                 $en_editions = null;
                 $workflow_id = null;
                 $client = null;
                 $show_summary = null;
                 $summary_type = null;
                 $issuetype_scheme = null;
                 $allow_reporting = null;
                 $autoassign = null;
                 for ($i = 0; $i != count($headerrow2); $i++) {
                     if ($headerrow2[$i] == 'name') {
                         $namecol = $i;
                     } elseif ($headerrow2[$i] == 'prefix') {
                         $prefix = $i;
                     } elseif ($headerrow2[$i] == 'scrum') {
                         $scrum = $i;
                     } elseif ($headerrow2[$i] == 'owner') {
                         $owner = $i;
                     } elseif ($headerrow2[$i] == 'owner_type') {
                         $owner_type = $i;
                     } elseif ($headerrow2[$i] == 'lead') {
                         $lead = $i;
                     } elseif ($headerrow2[$i] == 'lead_type') {
                         $lead_type = $i;
                     } elseif ($headerrow2[$i] == 'qa') {
                         $qa = $i;
                     } elseif ($headerrow2[$i] == 'qa_type') {
                         $qa_type = $i;
                     } elseif ($headerrow2[$i] == 'descr') {
                         $descr = $i;
                     } elseif ($headerrow2[$i] == 'doc_url') {
                         $doc_url = $i;
                     } elseif ($headerrow2[$i] == 'freelance') {
                         $freelance = $i;
                     } elseif ($headerrow2[$i] == 'en_builds') {
                         $en_builds = $i;
                     } elseif ($headerrow2[$i] == 'en_comps') {
                         $en_comps = $i;
                     } elseif ($headerrow2[$i] == 'en_editions') {
                         $en_editions = $i;
                     } elseif ($headerrow2[$i] == 'workflow_id') {
                         $workflow_id = $i;
                     } elseif ($headerrow2[$i] == 'client') {
                         $client = $i;
                     } elseif ($headerrow2[$i] == 'show_summary') {
                         $show_summary = $i;
                     } elseif ($headerrow2[$i] == 'summary_type') {
                         $summary_type = $i;
                     } elseif ($headerrow2[$i] == 'issuetype_scheme') {
                         $issuetype_scheme = $i;
                     } elseif ($headerrow2[$i] == 'allow_reporting') {
                         $allow_reporting = $i;
                     } elseif ($headerrow2[$i] == 'autoassign') {
                         $autoassign = $i;
                     }
                 }
                 $rowlength = count($headerrow2);
                 if ($namecol === null) {
                     $errors[] = TBGContext::getI18n()->__('Required column \'%col%\' not found in header row', array('%col%' => 'name'));
                 }
                 break;
             case 'issues':
                 $title = null;
                 $project = null;
                 $descr = null;
                 $repro = null;
                 $state = null;
                 $status = null;
                 $posted_by = null;
                 $owner = null;
                 $owner_type = null;
                 $assigned = null;
                 $assigned_type = null;
                 $resolution = null;
                 $issue_type = null;
                 $priority = null;
                 $category = null;
                 $severity = null;
                 $reproducability = null;
                 $votes = null;
                 $percentage = null;
                 $blocking = null;
                 $milestone = null;
                 for ($i = 0; $i != count($headerrow2); $i++) {
                     if ($headerrow2[$i] == 'title') {
                         $title = $i;
                     } elseif ($headerrow2[$i] == 'project') {
                         $project = $i;
                     } elseif ($headerrow2[$i] == 'assigned') {
                         $assigned = $i;
                     } elseif ($headerrow2[$i] == 'repro') {
                         $repro = $i;
                     } elseif ($headerrow2[$i] == 'state') {
                         $state = $i;
                     } elseif ($headerrow2[$i] == 'status') {
                         $status = $i;
                     } elseif ($headerrow2[$i] == 'posted_by') {
                         $posted_by = $i;
                     } elseif ($headerrow2[$i] == 'owner') {
                         $owner = $i;
                     } elseif ($headerrow2[$i] == 'owner_type') {
                         $owner_type = $i;
                     } elseif ($headerrow2[$i] == 'assigned') {
                         $assigned = $i;
                     } elseif ($headerrow2[$i] == 'assigned_type') {
                         $assigned_type = $i;
                     } elseif ($headerrow2[$i] == 'resolution') {
                         $resolution = $i;
                     } elseif ($headerrow2[$i] == 'issue_type') {
                         $issue_type = $i;
                     } elseif ($headerrow2[$i] == 'priority') {
                         $priority = $i;
                     } elseif ($headerrow2[$i] == 'category') {
                         $category = $i;
                     } elseif ($headerrow2[$i] == 'severity') {
                         $severity = $i;
                     } elseif ($headerrow2[$i] == 'reproducability') {
                         $reproducability = $i;
                     } elseif ($headerrow2[$i] == 'votes') {
                         $votes = $i;
                     } elseif ($headerrow2[$i] == 'percentage') {
                         $percentage = $i;
                     } elseif ($headerrow2[$i] == 'blocking') {
                         $blocking = $i;
                     } elseif ($headerrow2[$i] == 'type') {
                         $issue_type = $i;
                     } elseif ($headerrow2[$i] == 'milestone') {
                         $milestone = $i;
                     }
                 }
                 $rowlength = count($headerrow2);
                 if ($title === null) {
                     $errors[] = TBGContext::getI18n()->__('Required column \'%col%\' not found in header row', array('%col%' => 'title'));
                 }
                 if ($project === null) {
                     $errors[] = TBGContext::getI18n()->__('Required column \'%col%\' not found in header row', array('%col%' => 'project'));
                 }
                 if ($issue_type === null) {
                     $errors[] = TBGContext::getI18n()->__('Required column \'%col%\' not found in header row', array('%col%' => 'issue_type'));
                 }
                 break;
             default:
                 throw new Exception('Sorry, this type is unimplemented');
                 break;
         }
         // Check if rows are long enough
         for ($i = 1; $i != count($data); $i++) {
             $activerow = $data[$i];
             $activerow = html_entity_decode($activerow, ENT_QUOTES);
             $activerow = explode(',', $activerow);
             if (count($activerow) != $rowlength) {
                 $errors[] = TBGContext::getI18n()->__('Row %row% does not have the same number of elements as the header row', array('%row%' => $i + 1));
             }
         }
         reset($data);
         // Check if fields are empty
         for ($i = 1; $i != count($data); $i++) {
             $activerow = $data[$i];
             $activerow = html_entity_decode($activerow, ENT_QUOTES);
             $activerow = explode(',', $activerow);
             for ($j = 0; $j != count($activerow); $j++) {
                 if ($activerow[$j] == '' || $activerow[$j] == '""') {
                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col% has no value', array('%col%' => $j + 1, '%row%' => $i + 1));
                 }
             }
         }
         if (count($errors) == 0) {
             // Check if fields are valid
             switch ($request->getParameter('type')) {
                 case 'projects':
                     for ($i = 1; $i != count($data); $i++) {
                         $activerow = $data[$i];
                         $activerow = html_entity_decode($activerow, ENT_QUOTES);
                         $activerow = explode(',', $activerow);
                         // Check if project exists
                         $key = trim($activerow[$namecol], '" ');
                         $key = str_replace(' ', '', $key);
                         $key = strtolower($key);
                         $tmp = TBGProject::getByKey($key);
                         if ($tmp !== null) {
                             $errors[] = TBGContext::getI18n()->__('Row %row%: A project with this name already exists', array('%row%' => $i + 1));
                         }
                         // First off are booleans
                         $boolitems = array($scrum, $allow_reporting, $autoassign, $freelance, $en_builds, $en_comps, $en_editions, $show_summary);
                         foreach ($boolitems as $boolitem) {
                             if ($boolitem !== null && trim($activerow[$boolitem], '"') != 0 && trim($activerow[$boolitem], '"') != 1) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be 1/0)', array('%col%' => $boolitem + 1, '%row%' => $i + 1));
                             }
                         }
                         // Now identifiables
                         $identifiableitems = array(array($qa, $qa_type), array($lead, $lead_type), array($owner, $owner_type));
                         foreach ($identifiableitems as $identifiableitem) {
                             if (($identifiableitem[0] === null || $identifiableitem[1] === null) && !($identifiableitem[0] === null && $identifiableitem[1] === null)) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row%: Both the type and item ID must be supplied for owner/lead/qa fields', array('%row%' => $i + 1));
                                 continue;
                             }
                             if ($identifiableitem[1] !== null && trim($activerow[$identifiableitem[1]], '"') != 1 && trim($activerow[$identifiableitem[1]], '"') != 2) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be 1 for a user or 2 for a team)', array('%col%' => $identifiableitem[1] + 1, '%row%' => $i + 1));
                             }
                             if ($identifiableitem[0] !== null && !is_numeric(trim($activerow[$identifiableitem[0]], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                             } elseif ($identifiableitem[0] !== null && is_numeric(trim($activerow[$identifiableitem[0]], '"'))) {
                                 // check if they exist
                                 switch (trim($activerow[$identifiableitem[1]], '"')) {
                                     case 1:
                                         try {
                                             TBGContext::factory()->TBGUser(trim($activerow[$identifiableitem[0]], '" '));
                                         } catch (Exception $e) {
                                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: user does not exist', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                                         }
                                         break;
                                     case 2:
                                         try {
                                             TBGContext::factory()->TBGTeam(trim($activerow[$identifiableitem[0]], '" '));
                                         } catch (Exception $e) {
                                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: team does not exist', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                                         }
                                         break;
                                 }
                             }
                         }
                         // Now check client exists
                         if ($client !== null) {
                             if (!is_numeric(trim($activerow[$client], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $client + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGClient(trim($activerow[$client], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: client does not exist', array('%col%' => $client + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // Now check if workflow exists
                         if ($workflow_id !== null) {
                             if (!is_numeric(trim($activerow[$workflow_id], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $workflow_id + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGWorkflowScheme(trim($activerow[$workflow_id], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: workflow scheme does not exist', array('%col%' => $workflow_id + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // Now check if issuetype scheme
                         if ($issuetype_scheme !== null) {
                             if (!is_numeric(trim($activerow[$issuetype_scheme], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $issuetype_scheme + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGIssuetypeScheme(trim($activerow[$issuetype_scheme], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: issuetype scheme does not exist', array('%col%' => $issuetype_scheme + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // Finally check if the summary type is valid. At this point, your error list has probably become so big it has eaten up all your available RAM...
                         if ($summary_type !== null) {
                             if (trim($activerow[$summary_type], '"') != 'issuetypes' && trim($activerow[$summary_type], '"') != 'milestones') {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be \'issuetypes\' or \'milestones\')', array('%col%' => $summary_type + 1, '%row%' => $i + 1));
                             }
                         }
                     }
                     break;
                 case 'issues':
                     for ($i = 1; $i != count($data); $i++) {
                         $activerow = $data[$i];
                         $activerow = html_entity_decode($activerow, ENT_QUOTES);
                         $activerow = explode(',', $activerow);
                         // Check if project exists
                         try {
                             $prjtmp = TBGContext::factory()->TBGProject($activerow[$project]);
                         } catch (Exception $e) {
                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: Project does not exist', array('%col%' => $project + 1, '%row%' => $i + 1));
                             break;
                         }
                         // First off are booleans
                         $boolitems = array($state, $blocking);
                         foreach ($boolitems as $boolitem) {
                             if ($boolitem !== null && trim($activerow[$boolitem], '"') != 0 && trim($activerow[$boolitem], '"') != 1) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be 1/0)', array('%col%' => $boolitem + 1, '%row%' => $i + 1));
                             }
                         }
                         // Now numerics
                         $numericitems = array($votes, $percentage);
                         foreach ($numericitems as $numericitem) {
                             if ($numericitem !== null && !is_numeric(trim($activerow[$numericitem], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $numericitem + 1, '%row%' => $i + 1));
                             }
                         }
                         // Percentage must be 0-100
                         if ($numericitem !== null && (trim($activerow[$percentage], '"') < 0 || trim($activerow[$percentage], '"') > 100)) {
                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: Percentage must be from 0 to 100 inclusive', array('%col%' => $percentage + 1, '%row%' => $i + 1));
                         }
                         // Now identifiables
                         $identifiableitems = array(array($owner, $owner_type), array($assigned, $assigned_type));
                         foreach ($identifiableitems as $identifiableitem) {
                             if (($identifiableitem[0] === null || $identifiableitem[1] === null) && !($identifiableitem[0] === null && $identifiableitem[1] === null)) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row%: Both the type and item ID must be supplied for owner/lead/qa fields', array('%row%' => $i + 1));
                                 continue;
                             }
                             if ($identifiableitem[1] !== null && trim($activerow[$identifiableitem[1]], '"') != 1 && trim($activerow[$identifiableitem[1]], '"') != 2) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be 1 for a user or 2 for a team)', array('%col%' => $identifiableitem[1] + 1, '%row%' => $i + 1));
                             }
                             if ($identifiableitem[0] !== null && !is_numeric(trim($activerow[$identifiableitem[0]], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                             } elseif ($identifiableitem[0] !== null && is_numeric(trim($activerow[$identifiableitem[0]], '"'))) {
                                 // check if they exist
                                 switch (trim($activerow[$identifiableitem[1]], '"')) {
                                     case 1:
                                         try {
                                             TBGContext::factory()->TBGUser(trim($activerow[$identifiableitem[0]], '" '));
                                         } catch (Exception $e) {
                                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: user does not exist', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                                         }
                                         break;
                                     case 2:
                                         try {
                                             TBGContext::factory()->TBGTeam(trim($activerow[$identifiableitem[0]], '" '));
                                         } catch (Exception $e) {
                                             $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: team does not exist', array('%col%' => $identifiableitem[0] + 1, '%row%' => $i + 1));
                                         }
                                         break;
                                 }
                             }
                         }
                         // Now check user exists for postedby
                         if ($posted_by !== null) {
                             if (!is_numeric(trim($activerow[$posted_by], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $posted_by + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGUser(trim($activerow[$posted_by], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: user does not exist', array('%col%' => $posted_by + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // Now check milestone exists and is valid
                         if ($milestone !== null) {
                             if (!is_numeric(trim($activerow[$milestone], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $milestone + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     $milestonetmp = TBGContext::factory()->TBGMilestone(trim($activerow[$milestone], '" '));
                                     if ($milestonetmp->getProject()->getID() != $activerow[$project]) {
                                         $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: milestone does not apply to the specified project', array('%col%' => $milestone + 1, '%row%' => $i + 1));
                                     }
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: milestone does not exist', array('%col%' => $milestone + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // status
                         if ($status !== null) {
                             if (!is_numeric(trim($activerow[$status], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $status + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGStatus(trim($activerow[$status], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: status does not exist', array('%col%' => $status + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // resolution
                         if ($resolution !== null) {
                             if (!is_numeric(trim($activerow[$resolution], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $resolution + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGResolution(trim($activerow[$resolution], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: resolution does not exist', array('%col%' => $resolution + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // priority
                         if ($priority !== null) {
                             if (!is_numeric(trim($activerow[$priority], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $priority + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGPriority(trim($activerow[$priority], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: priority does not exist', array('%col%' => $priority + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // category
                         if ($category !== null) {
                             if (!is_numeric(trim($activerow[$category], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $category + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGCategory(trim($activerow[$category], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: category does not exist', array('%col%' => $category + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // severity
                         if ($severity !== null) {
                             if (!is_numeric(trim($activerow[$severity], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $severity + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGSeverity(trim($activerow[$severity], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: severity does not exist', array('%col%' => $severity + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // reproducability
                         if ($reproducability !== null) {
                             if (!is_numeric(trim($activerow[$reproducability], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $reproducability + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     TBGContext::factory()->TBGReproducability(trim($activerow[$reproducability], '" '));
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: reproducability does not exist', array('%col%' => $reproducability + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                         // type
                         if ($issue_type !== null) {
                             if (!is_numeric(trim($activerow[$issue_type], '"'))) {
                                 $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: invalid value (must be a number)', array('%col%' => $issue_type + 1, '%row%' => $i + 1));
                             } else {
                                 try {
                                     $typetmp = TBGContext::factory()->TBGIssuetype(trim($activerow[$issue_type], '" '));
                                     if (!$prjtmp->getIssuetypeScheme()->isSchemeAssociatedWithIssuetype($typetmp)) {
                                         $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: this project does not support issues of this type (%type%)', array('%type%' => $typetmp->getName(), '%col%' => $issue_type + 1, '%row%' => $i + 1));
                                     }
                                 } catch (Exception $e) {
                                     $errors[] = TBGContext::getI18n()->__('Row %row% column %col%: issue type does not exist', array('%col%' => $issue_type + 1, '%row%' => $i + 1));
                                 }
                             }
                         }
                     }
                     break;
             }
         }
         // Handle errors
         if (count($errors) != 0) {
             $errordiv = '<ul>';
             foreach ($errors as $error) {
                 $errordiv .= '<li>' . $error . '</li>';
             }
             $errordiv .= '</ul>';
             $this->getResponse()->setHttpStatus(400);
             return $this->renderJSON(array('failed' => true, 'errordetail' => $errordiv, 'error' => TBGContext::getI18n()->__('Errors occured while importing, see the error list in the import screen for further details')));
         }
     } catch (Exception $e) {
         $this->getResponse()->setHttpStatus(400);
         return $this->renderJSON(array('failed' => true, 'errordetail' => $e->getMessage(), 'error' => $e->getMessage()));
     }
     if ($request->getParameter('csv_dry_run')) {
         return $this->renderJSON(array('failed' => false, 'message' => TBGContext::getI18n()->__('Dry-run successful, you can now uncheck the dry-run box and import your data.')));
     } else {
         switch ($request->getParameter('type')) {
             case 'clients':
                 for ($i = 1; $i != count($data); $i++) {
                     try {
                         $activerow = $data[$i];
                         $activerow = html_entity_decode($activerow, ENT_QUOTES);
                         $activerow = explode(',', $activerow);
                         $client = new TBGClient();
                         $client->setName(trim($activerow[$namecol], '" '));
                         if ($emailcol !== null) {
                             $client->setEmail(trim($activerow[$emailcol], '" '));
                         }
                         if ($websitecol !== null) {
                             $client->setWebsite(trim($activerow[$websitecol], '" '));
                         }
                         if ($faxcol !== null) {
                             $client->setFax(trim($activerow[$faxcol], '" '));
                         }
                         if ($telephonecol !== null) {
                             $client->setTelephone(trim($activerow[$telephonecol], '" '));
                         }
                         $client->save();
                     } catch (Exception $e) {
                         $errors[] = TBGContext::getI18n()->__('Row %row% failed: %err%', array('%row%' => $i + 1, '%err%' => $e->getMessage()));
                     }
                 }
                 break;
             case 'projects':
                 for ($i = 1; $i != count($data); $i++) {
                     try {
                         $activerow = $data[$i];
                         $activerow = html_entity_decode($activerow, ENT_QUOTES);
                         $activerow = explode(',', $activerow);
                         $project = new TBGProject();
                         $project->setName(trim($activerow[$namecol], '" '));
                         if ($prefix !== null) {
                             $project->setPrefix(trim($activerow[$prefix], '" '));
                             $project->setUsePrefix(true);
                         }
                         if ($scrum !== null) {
                             if (trim($activerow[$websitecol], '"') == '1') {
                                 $project->setUseScrum(true);
                             }
                         }
                         if ($owner !== null && $owner_type !== null) {
                             switch (trim($activerow[$owner_type], '"')) {
                                 case TBGIdentifiableClass::TYPE_USER:
                                     $user = new TBGUser(trim($activerow[$owner], '" '));
                                     $project->setOwner($user);
                                     break;
                                 case TBGIdentifiableClass::TYPE_TEAM:
                                     $team = new TBGTeam(trim($activerow[$owner], '" '));
                                     $project->setOwner($team);
                                     break;
                             }
                         }
                         if ($lead !== null && $lead_type !== null) {
                             switch (trim($activerow[$lead_type], '"')) {
                                 case TBGIdentifiableClass::TYPE_USER:
                                     $user = new TBGUser(trim($activerow[$lead], '" '));
                                     $project->setLeader($user);
                                     break;
                                 case TBGIdentifiableClass::TYPE_TEAM:
                                     $team = new TBGTeam(trim($activerow[$lead], '" '));
                                     $project->setLeader($team);
                                     break;
                             }
                         }
                         if ($qa !== null && $qa_type !== null) {
                             switch (trim($activerow[$qa_type], '"')) {
                                 case TBGIdentifiableClass::TYPE_USER:
                                     $user = new TBGUser(trim($activerow[$qa], '" '));
                                     $project->setQaResponsible($user);
                                     break;
                                 case TBGIdentifiableClass::TYPE_TEAM:
                                     $team = new TBGTeam(trim($activerow[$qa], '" '));
                                     $project->setQaResponsible($team);
                                     break;
                             }
                         }
                         if ($descr !== null) {
                             $project->setDescription(trim($activerow[$descr], '" '));
                         }
                         if ($doc_url !== null) {
                             $project->setDocumentationUrl(trim($activerow[$doc_url], '" '));
                         }
                         if ($freelance !== null) {
                             if (trim($activerow[$freelance], '"') == '1') {
                                 $project->setChangeIssuesWithoutWorkingOnThem(true);
                             }
                         }
                         if ($en_builds !== null) {
                             if (trim($activerow[$en_builds], '"') == '1') {
                                 $project->setBuildsEnabled(true);
                             }
                         }
                         if ($en_comps !== null) {
                             if (trim($activerow[$en_comps], '"') == '1') {
                                 $project->setComponentsEnabled(true);
                             }
                         }
                         if ($en_editions !== null) {
                             if (trim($activerow[$en_editions], '"') == '1') {
                                 $project->setEditionsEnabled(true);
                             }
                         }
                         if ($workflow_id !== null) {
                             $workflow = TBGContext::factory()->TBGWorkflowScheme(trim($activerow[$workflow_id], '" '));
                             $project->setWorkflowScheme($workflow);
                         }
                         if ($client !== null) {
                             $client_object = TBGContext::factory()->TBGWorkflowScheme(trim($activerow[$client], '" '));
                             $project->setClient($client_object);
                         }
                         if ($show_summary !== null) {
                             if (trim($activerow[$show_summary], '"') == '1') {
                                 $project->setFrontpageSummaryVisibility(true);
                             }
                         }
                         if ($summary_type !== null) {
                             $project->setFrontpageSummaryType(trim($activerow[$summary_type], '" '));
                         }
                         if ($issuetype_scheme !== null) {
                             $project->setIssuetypeScheme(TBGContext::factory()->TBGIssuetypeScheme(trim($activerow[$issuetype_scheme], '"')));
                         }
                         if ($allow_reporting !== null) {
                             $project->setLocked(trim($activerow[$allow_reporting], '" '));
                         }
                         if ($autoassign !== null) {
                             $project->setAutoassign(trim($activerow[$autoassign], '" '));
                         }
                         $project->save();
                     } catch (Exception $e) {
                         $errors[] = TBGContext::getI18n()->__('Row %row% failed: %err%', array('%row%' => $i + 1, '%err%' => $e->getMessage()));
                     }
                 }
                 break;
             case 'issues':
                 for ($i = 1; $i != count($data); $i++) {
                     try {
                         $activerow = $data[$i];
                         $activerow = html_entity_decode($activerow, ENT_QUOTES);
                         $activerow = explode(',', $activerow);
                         $issue = new TBGIssue();
                         $issue->setTitle(trim($activerow[$title], '" '));
                         $issue->setProject(trim($activerow[$project], '" '));
                         $issue->setIssuetype(trim($activerow[$issue_type], '" '));
                         if ($issue_type !== null) {
                             $issue->setIssuetype(trim($activerow[$issue_type], '" '));
                         }
                         if ($descr !== null) {
                             $issue->setDescription(trim($activerow[$descr], '" '));
                         }
                         if ($repro !== null) {
                             $issue->setReproduction(trim($activerow[$repro], '" '));
                         }
                         if ($state !== null) {
                             $issue->setState(trim($activerow[$state], '" '));
                         }
                         if ($status !== null) {
                             $issue->setStatus(trim($activerow[$status], '" '));
                         }
                         if ($posted_by !== null) {
                             $issue->setPostedBy(TBGContext::factory()->TBGUser(trim($activerow[$posted_by], '"')));
                         }
                         if ($owner !== null && $owner_type !== null) {
                             switch (trim($activerow[$owner_type], '"')) {
                                 case TBGIdentifiableClass::TYPE_USER:
                                     $user = new TBGUser(trim($activerow[$owner], '" '));
                                     $issue->setOwner($user);
                                     break;
                                 case TBGIdentifiableClass::TYPE_TEAM:
                                     $team = new TBGTeam(trim($activerow[$owner], '" '));
                                     $issue->setOwner($team);
                                     break;
                             }
                         }
                         if ($assigned !== null && $assigned_type !== null) {
                             switch (trim($activerow[$assigned_type], '"')) {
                                 case TBGIdentifiableClass::TYPE_USER:
                                     $user = new TBGUser(trim($activerow[$assigned], '" '));
                                     $issue->setAssignee($user);
                                     break;
                                 case TBGIdentifiableClass::TYPE_TEAM:
                                     $team = new TBGTeam(trim($activerow[$assigned], '" '));
                                     $issue->setAssignee($team);
                                     break;
                             }
                         }
                         if ($resolution !== null) {
                             $issue->setResolution(trim($activerow[$resolution], '" '));
                         }
                         if ($priority !== null) {
                             $issue->setPriority(trim($activerow[$priority], '" '));
                         }
                         if ($category !== null) {
                             $issue->setCategory(trim($activerow[$category], '" '));
                         }
                         if ($blocking !== null) {
                             $issue->setBlocking(trim($activerow[$blocking], '" '));
                         }
                         if ($severity !== null) {
                             $issue->setSeverity(trim($activerow[$severity], '" '));
                         }
                         if ($reproducability !== null) {
                             $issue->setReproducability(trim($activerow[$reproducability], '" '));
                         }
                         if ($votes !== null) {
                             $issue->setVotes(trim($activerow[$votes], '" '));
                         }
                         if ($percentage !== null) {
                             $issue->setPercentage(trim($activerow[$percentage], '" '));
                         }
                         if ($milestone !== null) {
                             $issue->setMilestone(trim($activerow[$milestone], '" '));
                         }
                         $issue->save();
                     } catch (Exception $e) {
                         $errors[] = TBGContext::getI18n()->__('Row %row% failed: %err%', array('%row%' => $i + 1, '%err%' => $e->getMessage()));
                     }
                 }
                 break;
         }
         // Handle errors
         if (count($errors) != 0) {
             $errordiv = '<ul>';
             foreach ($errors as $error) {
                 $errordiv .= '<li>' . $error . '</li>';
             }
             $errordiv .= '</ul>';
             $this->getResponse()->setHttpStatus(400);
             return $this->renderJSON(array('failed' => true, 'errordetail' => $errordiv, 'error' => TBGContext::getI18n()->__('Errors occured while importing, see the error list in the import screen for further details')));
         } else {
             return $this->renderJSON(array('failed' => false, 'message' => TBGContext::getI18n()->__('Successfully imported %num% rows!', array('%num%' => count($data) - 1))));
         }
     }
 }