Ejemplo n.º 1
0
 public function import()
 {
     $objPHPExcel = @PHPExcel_IOFactory::load($this->file->getTempName());
     /** @var PHPExcel_Worksheet $worksheet */
     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
         $rowIterator = $worksheet->getRowIterator();
         /** @var PHPExcel_Worksheet_Row $row */
         foreach ($rowIterator as $row) {
             $alternative = new Alternative();
             $alternative->setDecision($this->decision);
             $alternative->setStatus('Reviewed');
             $cellIterator = $row->getCellIterator();
             $cellIterator->setIterateOnlyExistingCells();
             /** @var PHPExcel_Cell $cell */
             foreach ($cellIterator as $cell) {
                 if (!is_null($cell)) {
                     $column = $cell->getColumn();
                     if ($column == 'A') {
                         $alternative->setName($cell->getValue());
                     } else {
                         if ($column == 'B') {
                             $alternative->setAdditionalInfo($cell->getValue());
                         }
                     }
                 }
             }
             if ($alternative->getName()) {
                 $alternative->setCreatedBy($this->created_and_updated_by);
                 $alternative->setUpdatedBy($this->created_and_updated_by);
                 $alternative->save();
                 $this->alternatives[] = $alternative;
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @param sfWebRequest $request
  * @return sfView
  */
 public function executeImportFromCustomFields(sfWebRequest $request)
 {
     $decision_id = $request->getParameter('decision_id', false);
     $decision = DecisionTable::getInstance()->getDecisionForUser($this->getUser()->getGuardUser(), $decision_id);
     $this->forward404Unless(is_object($decision));
     $header = $request->getParameter('header');
     $data = $request->getParameter('data');
     if (array_search('name', $header) === false) {
         return $this->renderText(json_encode(array('status' => 'error', 'text' => 'Please ensure that data field "Name" is mapped to a column in your SpreadSheet.')));
     }
     foreach ($data as $item) {
         $alternative = null;
         if (array_key_exists('id', $item) && !empty($item['id'])) {
             $alternative = AlternativeTable::getInstance()->createQuery('a')->leftJoin('a.Decision d')->leftJoin('d.User u')->leftJoin('u.TeamMember tm')->whereIn('d.user_id', sfGuardUserTable::getInstance()->getUsersInTeamIDs($this->getUser()->getGuardUser()))->andWhere('a.item_id = ?', $item['id'])->andWhere('d.id = ?', $decision->getId())->fetchOne();
         }
         if (!is_object($alternative)) {
             $alternative = new Alternative();
             $alternative->setDecision($decision);
             $alternative->setCreatedBy(Alternative::generateUpdateAndCreatedBy($this->getUser()->getGuardUser()));
             $alternative->setUpdatedBy(Alternative::generateUpdateAndCreatedBy($this->getUser()->getGuardUser()));
         }
         $custom_fields = array();
         foreach ($item as $prop => $value) {
             if (array_key_exists($prop, $header)) {
                 if ($header[$prop] == '_new') {
                     $custom_fields[$prop] = $value;
                 } elseif (!in_array($header[$prop], array('id')) && in_array($header[$prop], array('name', 'status', 'work progress', 'additional info', 'notes', 'due date', 'notify date', 'tags'))) {
                     if ($header[$prop] == 'tags') {
                         // Process tags
                         $tags_request = array_map('trim', explode(',', $value));
                         $tags = array();
                         foreach ($alternative->getTagAlternative() as $tag) {
                             $tags[] = $tag->Tag->name;
                         }
                         foreach (array_diff($tags_request, $tags) as $result) {
                             Tag::newTag($this->getUser()->getGuardUser(), $alternative->getId(), $result, 'alternative');
                         }
                         foreach (array_diff($tags, $tags_request) as $result) {
                             Tag::removeTag($this->getUser()->getGuardUser(), $alternative->getId(), $result, 'alternative');
                         }
                     } else {
                         $alternative->{str_replace(' ', '_', $header[$prop])} = $value;
                     }
                 }
             }
         }
         if ($custom_fields) {
             $alternative->setCustomFields(json_encode($custom_fields));
         }
         if (!$alternative->getName()) {
             $alternative->setName('New ' . InterfaceLabelTable::getInstance()->get($this->getUser()->getGuardUser(), InterfaceLabelTable::ITEM_TYPE));
         }
         $alternative->save();
     }
     return $this->renderText(json_encode(array('status' => 'success')));
 }