/**
  * upload a field
  * @param FlexiTableFieldObject $oField
  * @param array $oStore
  * @param array $oRow (new form row)
  * @param array $oCurrentRow  (old row)
  */
 public function doUploadField(FlexiTableFieldObject $oField, &$oStore, &$oForm, $oCurrentRow)
 {
     $sName = $oField->getName();
     $sSavePath = is_null($oField->savepath) ? FlexiFileUtil::getFullUploadPath("media/libraries") : $oField->savepath;
     //relative path is to cut out prefix of path before saving to field
     $sFullRelativeBasePath = empty($oField->savepath) ? "" : realpath($oField->savepath);
     //if multiple file
     //var_dump($oField->type);
     if ($oField->type == "multiimage-text") {
         $aCurrentFile = array();
         if (!empty($oCurrentRow[$sName])) {
             $aCurrentFile = explode($oField->uploadseparator, $oCurrentRow[$sName]);
         }
         $aResultFile = array();
         //var_dump($oForm);
         for ($c = 1; $c <= $oField->uploadcount; $c++) {
             if (isset($oForm[$sName . "_" . $c])) {
                 $sNewFile = "media." . FlexiStringUtil::createRandomAlphaNumeric() . "_" . time();
                 $aStatus = FlexiFileUtil::storeUploadFile($oForm[$sName . "_" . $c], $sSavePath, $sNewFile . ".");
                 $this->onGetUploadFileName($sSaveDir, $sNewFile);
                 if ($aStatus["status"]) {
                     //replace photo if already exists
                     if (!empty($aCurrentFile[$c - 1])) {
                         unlink(FlexiFileUtil::getFullPathFrom($aCurrentFile[$c - 1], $sFullRelativeBasePath));
                     }
                     if ($oField->isUploadImage() && !empty($oField->maxwidth) || !empty($oField->maxheight)) {
                         FlexiImageUtil::imageResize($oField->maxwidth, $oField->maxheight, $aStatus["path"]);
                     }
                     //if savepath not declared, full path from root is saved
                     //  if declared, only save filename
                     //  "" => use base root path
                     //resize image based on max width, height
                     //FlexiImageUtil::imageResize(345, 287, $aStatus["path"]);
                     $aResultFile[$c - 1] = FlexiFileUtil::getRelativePathFrom($aStatus["path"], $sFullRelativeBasePath);
                 } else {
                     //No file
                     $aResultFile[$c - 1] = $aCurrentFile[$c - 1];
                 }
             }
         }
         //for each file
         $oStore[$sName] = implode($oField->uploadseparator, $aResultFile);
     } else {
         //single file upload
         if (!isset($oForm[$sName])) {
             return;
         }
         //isupload form, presume
         if (is_array($oForm[$sName])) {
             $sNewFile = "media." . FlexiStringUtil::createRandomAlphaNumeric() . "_" . time();
             //var_dump($oRow[$sName]);
             $aStatus = FlexiFileUtil::storeUploadFile($oForm[$sName], $sSavePath, $sNewFile . ".");
             $this->onGetUploadFileName($sSaveDir, $sNewFile);
             if ($aStatus["status"]) {
                 //replace photo if already exists
                 if (!empty($oCurrentRow[$sName])) {
                     $sOldPath = FlexiFileUtil::getRelativePathFrom($oCurrentRow[$sName], $sFullRelativeBasePath);
                     unlink($sOldPath);
                 }
                 if ($oField->isUploadImage() && !empty($oField->maxwidth) || !empty($oField->maxheight)) {
                     FlexiImageUtil::imageResize($oField->maxwidth, $oField->maxheight, $aStatus["path"]);
                 }
                 //if savepath not declared, full path from root is saved
                 //  if declared, only save filename
                 //  "" => use base root path
                 //resize image based on max width, height
                 //FlexiImageUtil::imageResize(345, 287, $aStatus["path"]);
                 $oStore[$sName] = FlexiFileUtil::getRelativePathFrom($aStatus["path"], $sFullRelativeBasePath);
             } else {
                 //No file
             }
         } else {
             if (is_string($oForm[$sName])) {
                 //could be manually saved or from old path
                 $sNewFile = $oForm[$sName];
                 //delete old file if different from new file
                 if (!empty($oCurrentRow[$sName]) && !empty($sNewFile)) {
                     $sOldPath = FlexiFileUtil::getFullPathFrom($oCurrentRow[$sName], $sFullRelativeBasePath);
                     $sNewPath = FlexiFileUtil::getFullPathFrom($sNewFile, $sFullRelativeBasePath);
                     $sOldPathReal = realpath($sOldPath);
                     if (!empty($sOldPathReal) && $sOldPathReal != realpath($sNewPath)) {
                         unlink($sOldPathReal);
                     }
                 }
                 $oStore[$sName] = FlexiFileUtil::getRelativePathFrom($sNewFile, $sFullRelativeBasePath);
             } else {
                 throw new Exception("Invalid upload value: " . $oForm[$sName]);
             }
         }
         //else error
     }
     //if single file
 }
Beispiel #2
0
 public static function processUploadPhoto($sFieldName, $sSavePath, $sSaveName, $iMaxWidth = null, $iMaxHeight = null, $aiMaxUpload = null, $bReplace = true, $sOldPath = null)
 {
     $aExtension = array("jpg", "jpeg", "pjpeg", "pjpg", "png");
     $result = self::processUpload($sFieldName, $sSavePath, $aExtension, $sSaveName, $aiMaxUpload, $bReplace, $sOldPath);
     if ($result === false) {
         return false;
     }
     if ($result["status"]) {
         //resize image based on max width, height
         FlexiImageUtil::imageResize($iMaxWidth, $iMaxHeight, $result["path"]);
     } else {
         $sNotice = flexiT("Save upload file failed", true);
         throw new FlexiException($sNotice, ERROR_UNKNOWN);
     }
     return $result;
 }