Exemplo n.º 1
0
 /**
  * Import row from given array datas
  *
  * @param array $data The module datas to import
  * @param array $params The import parameters.
  *		array(
  *				module	=> false|true : the module to create categories (required)
  *				create	=> false|true : create missing objects (default : true)
  *				update	=> false|true : update existing objects (default : true)
  *				files	=> false|true : use files from PATH_TMP_FS (default : true)
  *			)
  * @param CMS_language $cms_language The CMS_langage to use
  * @param array $idsRelation : Reference : The relations between import datas ids and real imported ids
  * @param string $infos : Reference : The import infos returned
  * @return boolean : true on success, false on failure
  * @access public
  */
 function fromArray($data, $params, $cms_language, &$idsRelation, &$infos)
 {
     if (!isset($params['module'])) {
         $infos .= 'Error : missing module codename for categories importation ...' . "\n";
         return false;
     }
     $module = CMS_modulesCatalog::getByCodename($params['module']);
     if ($module->hasError()) {
         $infos .= 'Error : invalid module for categories importation : ' . $params['module'] . "\n";
         return false;
     }
     if (!$this->getID() && CMS_moduleCategories_catalog::uuidExists($data['uuid'])) {
         //check imported uuid. If categories does not have an Id, the uuid must be unique or must be regenerated
         $uuid = io::uuid();
         //store old uuid relation
         $idsRelation['categories-uuid'][$data['uuid']] = $uuid;
         $data['uuid'] = $uuid;
     }
     //set category uuid if not exists
     if (!$this->_uuid) {
         $this->_uuid = $data['uuid'];
     }
     if (!isset($params['files']) || $params['files'] == true) {
         if (isset($data['icon'])) {
             $icon = $data['icon'];
             if ($icon && file_exists(PATH_TMP_FS . $icon)) {
                 //destroy old file if any
                 if ($this->getIconPath(false, PATH_RELATIVETO_WEBROOT, true)) {
                     @unlink($this->getIconPath(true, PATH_RELATIVETO_FILESYSTEM, true));
                     $this->setAttribute('icon', '');
                 }
                 //move and rename uploaded file
                 $filename = PATH_TMP_FS . $icon;
                 $basename = pathinfo($filename, PATHINFO_BASENAME);
                 if (!$this->getID()) {
                     //need item ID
                     $this->writeToPersistence();
                 }
                 //create file path
                 $path = $this->getIconPath(true, PATH_RELATIVETO_FILESYSTEM, false) . '/';
                 $extension = pathinfo($icon, PATHINFO_EXTENSION);
                 $newBasename = "cat-" . $this->getID() . "-icon." . $extension;
                 $newFilename = $path . '/' . $newBasename;
                 if (CMS_file::moveTo($filename, $newFilename)) {
                     CMS_file::chmodFile(FILES_CHMOD, $newFilename);
                     //set it
                     $this->setAttribute('icon', $newBasename);
                 }
             } elseif (!$icon) {
                 //destroy old file if any
                 if ($this->getIconPath(false, PATH_RELATIVETO_WEBROOT, true)) {
                     @unlink($this->getIconPath(true, PATH_RELATIVETO_FILESYSTEM, true));
                     $this->setAttribute('icon', '');
                 }
             }
         }
     }
     if (isset($data['labels'])) {
         foreach ($data['labels'] as $language => $label) {
             $this->setLabel($label, $language);
         }
     }
     if (isset($data['descriptions'])) {
         foreach ($data['descriptions'] as $language => $desc) {
             $this->setDescription($desc, $language);
         }
     }
     if (!isset($params['files']) || $params['files'] == true) {
         if (isset($data['files']) && is_array($data['files'])) {
             foreach ($data['files'] as $language => $file) {
                 if ($file && file_exists(PATH_TMP_FS . $file)) {
                     //destroy old file if any
                     if ($this->getFilePath($language, false, PATH_RELATIVETO_WEBROOT, true)) {
                         @unlink($this->getFilePath($language, true, PATH_RELATIVETO_FILESYSTEM, true));
                         $this->setFile('', $language);
                     }
                     //move and rename uploaded file
                     $filename = PATH_TMP_FS . $file;
                     $basename = pathinfo($filename, PATHINFO_BASENAME);
                     if (!$this->getID()) {
                         //need item ID
                         $this->writeToPersistence();
                     }
                     //create file path
                     $path = $this->getFilePath($language, true, PATH_RELATIVETO_FILESYSTEM, false) . '/';
                     $extension = pathinfo($file, PATHINFO_EXTENSION);
                     $newBasename = "cat-" . $this->getID() . "-file-" . $language . "." . $extension;
                     $newFilename = $path . '/' . $newBasename;
                     if (CMS_file::moveTo($filename, $newFilename)) {
                         CMS_file::chmodFile(FILES_CHMOD, $newFilename);
                         //set it
                         $this->setFile($newBasename, $language);
                     }
                 } elseif (!$file) {
                     //destroy old file if any
                     if ($this->getFilePath($language, false, PATH_RELATIVETO_WEBROOT, true)) {
                         @unlink($this->getFilePath($language, true, PATH_RELATIVETO_FILESYSTEM, true));
                         $this->setFile('', $language);
                     }
                 }
             }
         }
     }
     //write object
     if (!$this->writeToPersistence()) {
         $infos .= 'Error : can not write category ...' . "\n";
         return false;
     }
     //if current category id has changed from imported id, set relation
     if (isset($data['id']) && $data['id'] && $this->getID() != $data['id']) {
         $idsRelation['categories'][$data['id']] = $this->getID();
         if (isset($data['uuid']) && $data['uuid']) {
             $idsRelation['categories'][$data['uuid']] = $this->getID();
         }
     }
     //set category order
     if (isset($data['order']) && $data['order']) {
         CMS_moduleCategories_catalog::moveCategoryIndex($this, $data['order']);
     }
     //set categories childs
     if (isset($data['childs']) && $data['childs']) {
         return CMS_moduleCategories_catalog::fromArray($data['childs'], $params, $cms_language, $idsRelation, $infos);
     }
     return true;
 }