示例#1
0
 public function saveProject($args, $project_name = null, $id_product = false)
 {
     $product = null;
     if (isset($args['project_data']) && !$args['project_data']) {
         $this->error = $this->l('Select at least one product element to work with');
         return false;
     }
     //$id_project = Tools::getValue('id_project', null);
     $textMasterProject = new TextMasterProject(null);
     $textMasterProject->ctype = $textMasterProject->type = $args['ctype'];
     if (!Language::isInstalled($args['language_from'])) {
         $this->error = sprintf($this->l('Source language %s is not installed in PrestaShop'), $args['language_from']);
         return false;
     }
     if (isset($args['project_name'])) {
         $project_name = $args['project_name'];
     }
     if (!is_array($id_product) && $id_product) {
         $documents = $this->collectProductData($args, $id_product, $textMasterProject, $project_name);
     } elseif (is_array($id_product)) {
         $documents = $this->collectProductData($args, $id_product, $textMasterProject, $project_name);
     }
     $textMasterProject->name = pSQL($project_name);
     if (isset($args['same_author_must_do_entire_project'])) {
         $textMasterProject->same_author_must_do_entire_project = $args['same_author_must_do_entire_project'];
     }
     $textMasterProject->language_level = $args['language_level'];
     $textMasterProject->quality = $args['quality'];
     $textMasterProject->expertise = $args['expertise'];
     $textMasterProject->language_from = $args['language_from'];
     $textMasterProject->category = $args['category'];
     $textMasterProject->project_briefing = pSQL($args['project_briefing']);
     $textMasterProject->vocabulary_type = $args['vocabulary_type'];
     $textMasterProject->target_reader_groups = $args['target_reader_groups'];
     $textMasterProject->grammatical_person = $args['grammatical_person'];
     $textMasterProject->textmasters = $args['textmasters'];
     $textMasterProject->status = $this->available_statuses[0];
     if ($textMasterProject->type == 'translation') {
         $textMasterProject->language_to = $args['language_to'];
         if (!Language::isInstalled($args['language_to'])) {
             $this->error = sprintf($this->l('Target language %s is not installed in PrestaShop'), $args['language_to']);
             return false;
         }
         if ($args['language_from'] == $args['language_to']) {
             $this->error = $this->l('Source language cannot be the same as target language');
             return false;
         }
     }
     /*@todo Should be refactored*/
     $textMasterProject->__set('language_from', $args['language_from']);
     $textMasterProject->__set('language_to', $args['language_to']);
     $textMasterProject->__set('name', pSQL($project_name));
     if ($this->textmaster_data_with_cookies_manager_obj->projectDataExists('id_edit_project')) {
         $delete_current_project = $this->deleteProjectAfterEdit((int) $this->textmaster_data_with_cookies_manager_obj->getProjectData('id_edit_project'));
         if ($delete_current_project !== true) {
             $this->error = $delete_current_project;
             return false;
         }
     }
     /* trying to save project */
     $result = $textMasterProject->save();
     if (is_bool($result)) {
         if ($result === false) {
             $this->error = $this->l('Project was successfully created but could not be added to prestashop database. Therefore it can only be managed via Textmaster website.');
             return false;
         } else {
             /* saving documents to database after project was successfully created */
             foreach ($documents as $document) {
                 $textMasterDocument = new TextMasterDocument();
                 $textMasterDocument->setApiData($document);
                 $textMasterDocument->id_project_api = $textMasterProject->id_project_api;
                 $textMasterDocument->id_project = $textMasterProject->id;
                 if (!$textMasterDocument->save()) {
                     return false;
                 }
             }
             /* lauches newly created project */
             if (!isset($id_project) || !$id_project) {
                 $result = $textMasterProject->launch();
                 if (!is_bool($result)) {
                     $this->addFlashWarning($this->l('Project was successfully created, but could not be launched: ') . $result);
                     return true;
                 }
             }
         }
     } else {
         $this->error = $result;
         return false;
     }
     $this->addFlashMessage($this->l('Project was successfully created'));
     return true;
 }