/** * Import module from given array datas * * @param array $data The module datas to import * @param array $params The import parameters. * array( * 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 (!$this->getID()) { if (!isset($params['create']) || $params['create'] == true) { //if module does not exists yet, add codename and default admin frontend $this->setCodename($data['codename']); $this->setAdminFrontend('index.php'); } else { $infos .= 'Module does not exists and parameter does not allow to create it ...' . "\n"; return false; } } if (!$this->getID() && (!isset($params['create']) || $params['create'] == true) || $this->getID() && (!isset($params['update']) || $params['update'] == true)) { if (isset($data['labels'])) { //create labels $this->setLabel($cms_language->createMessage($this->_codename, $data['labels'])); } if (!$this->writeToPersistence()) { $infos .= 'Error writing module ...' . "\n"; return false; } elseif (isset($data['parameters']) && is_array($data['parameters']) && $data['parameters']) { //write module parameters $this->_hasParameters = 1; $filename = PATH_MODULES_FS . "/" . $this->_codename . "_rc.xml"; if (!file_exists($filename)) { $file = new CMS_file($filename); $file->writeToPersistence(true); } $this->setAndWriteParameters($data['parameters']); $this->writeToPersistence(); } } //append codename to parameters $params['module'] = $this->_codename; //add categories if (isset($data['categories']) && $data['categories']) { if (!CMS_moduleCategories_catalog::fromArray($data['categories'], $params, $cms_language, $idsRelation, $infos)) { $infos .= 'Error during categories import ...' . "\n"; return false; } } if (!isset($params['files']) || $params['files'] == true) { //add JS if (isset($data['js']) && $data['js']) { foreach ($data['js'] as $jsFile) { if ($jsFile && file_exists(PATH_TMP_FS . $jsFile)) { if (file_exists(PATH_REALROOT_FS . $jsFile) && (!isset($params['updateJs']) || $params['updateJs'] == true) || (!isset($params['create']) || $params['create'] == true)) { if (CMS_file::moveTo(PATH_TMP_FS . $jsFile, PATH_REALROOT_FS . $jsFile)) { CMS_file::chmodFile(FILES_CHMOD, PATH_REALROOT_FS . $jsFile); } else { $infos .= 'Error during copy of file ' . $jsFile . ' ...' . "\n"; } } } } } } if (!isset($params['files']) || $params['files'] == true) { //add CSS if (isset($data['css']) && $data['css']) { foreach ($data['css'] as $cssFile) { if ($cssFile && file_exists(PATH_TMP_FS . $cssFile)) { if (file_exists(PATH_REALROOT_FS . $cssFile) && (!isset($params['updateCss']) || $params['updateCss'] == true) || (!isset($params['create']) || $params['create'] == true)) { if (CMS_file::moveTo(PATH_TMP_FS . $cssFile, PATH_REALROOT_FS . $cssFile)) { CMS_file::chmodFile(FILES_CHMOD, PATH_REALROOT_FS . $cssFile); } else { $infos .= 'Error during copy of file ' . $cssFile . ' ...' . "\n"; } } } } } } if (!isset($params['files']) || $params['files'] == true) { //add IMG if (isset($data['img']) && $data['img']) { foreach ($data['img'] as $imgFile) { if ($imgFile && file_exists(PATH_TMP_FS . $imgFile)) { if (file_exists(PATH_REALROOT_FS . $imgFile) && (!isset($params['updateImg']) || $params['updateImg'] == true) || (!isset($params['create']) || $params['create'] == true)) { if (CMS_file::moveTo(PATH_TMP_FS . $imgFile, PATH_REALROOT_FS . $imgFile)) { CMS_file::chmodFile(FILES_CHMOD, PATH_REALROOT_FS . $imgFile); } else { $infos .= 'Error during copy of file ' . $imgFile . ' ...' . "\n"; } } } } } } if (!isset($params['files']) || $params['files'] == true) { //add rows if (isset($data['rows']) && $data['rows']) { if (!CMS_rowsCatalog::fromArray($data['rows'], $params, $cms_language, $idsRelation, $infos)) { $infos .= 'Error during rows import ...' . "\n"; return false; } } } return true; }
/** * 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; }