Exemple #1
0
 /**
  * Get datas vars from a form formatted by such a Automne.LinkField class
  * and build a CMS_href
  *
  * @param array $datas, the datas sent by the Automne.LinkField return
  * @param string $module, the module concerned by this link
  * @param integer $resourceID, ID to prepend the filename uploaded with
  * @param integer $fieldID, optional field ID to surcharge file name representation ("r".$resourceID."_f".$fieldID."_")
  * @return boolean true on success, false on failure
  * @access public
  */
 function create($datas = '', $module = MOD_STANDARD_CODENAME, $resourceID, $fieldID = '')
 {
     $datas = explode($this->_href->getSeparator(), $datas);
     $linkLabel = isset($datas[7]) ? $datas[7] : '';
     $linkType = isset($datas[0]) ? $datas[0] : '';
     $internalLink = isset($datas[1]) ? $datas[1] : '';
     $externalLink = isset($datas[2]) ? $datas[2] : '';
     $this->_href->setLabel($linkLabel);
     $this->_href->setLinkType($linkType);
     $this->_href->setInternalLink($internalLink);
     $this->_href->setExternalLink($externalLink);
     // Delete/Upload file
     if (isset($datas[3])) {
         switch ($module) {
             case MOD_STANDARD_CODENAME:
                 $locationType = RESOURCE_DATA_LOCATION_EDITION;
                 $uniqueName = md5(serialize($this) . microtime());
                 $fileprefix = $fieldID ? 'p' . $resourceID . '_' . $uniqueName . "_f" . $fieldID : 'p' . $resourceID . '_' . $uniqueName;
                 break;
             default:
                 $locationType = RESOURCE_DATA_LOCATION_EDITED;
                 $fileprefix = $fieldID ? 'r' . $resourceID . "_f" . $fieldID . "_" : 'r' . $resourceID . "_";
                 break;
         }
         if ($datas[3] && io::strpos($datas[3], PATH_UPLOAD_WR . '/') !== false) {
             //move and rename uploaded file
             $datas[3] = str_replace(PATH_UPLOAD_WR . '/', PATH_UPLOAD_FS . '/', $datas[3]);
             $basename = pathinfo($datas[3], PATHINFO_BASENAME);
             $path = $this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM, false);
             $newFilename = $path . '/' . $fileprefix . $basename;
             CMS_file::moveTo($datas[3], $newFilename);
             CMS_file::chmodFile(FILES_CHMOD, $newFilename);
             $datas[3] = pathinfo($newFilename, PATHINFO_BASENAME);
             //remove the old file if any
             if (is_file($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
                 if (!unlink($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
                     $this->raiseError("Could not delete old linked file");
                 }
             }
         } elseif ($datas[3]) {
             //keep old file
             $datas[3] = pathinfo($datas[3], PATHINFO_BASENAME);
         } else {
             $datas[3] = '';
             //remove the old file if any
             if (is_file($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
                 if (!unlink($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
                     $this->raiseError("Could not delete old linked file");
                 }
             }
         }
         $this->_href->setFileLink($datas[3]);
     } elseif (is_file($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
         //remove the old file
         if (!unlink($this->_href->getFileLink(true, $module, $locationType, PATH_RELATIVETO_FILESYSTEM))) {
             $this->raiseError("Could not delete old linked file");
         }
     }
     // Target and Popup > (width, height)
     list($width, $height) = explode(',', $datas[6]);
     if (sensitiveIO::isPositiveInteger($width) && sensitiveIO::isPositiveInteger($height)) {
         $this->_href->setPopup($width, $height);
     } else {
         switch ($datas[4]) {
             case "_top":
                 $this->_href->setTarget('_top');
                 $this->_href->setPopup('', '');
                 break;
             case "_blank":
                 $this->_href->setTarget('_blank');
                 $this->_href->setPopup('', '');
                 break;
         }
     }
     return true;
 }