text private $asImages; // [id_step][id_image]=>array('name'=>file name, 'desc'=> description) function __construct($oMySql) { parent::__construct(__CLASS__, Settings::DEBUG); $this->oMySql = $oMySql; $this->iPrevProcId = 0; $this->setProcedureId(0); $this->sTitle = ''; $this->sDescription = ''; $this->dLed = ''; $this->asSteps = array(); $this->asImages = array(); } public function getProcedureId() { return $this->iProcId; } public function getProcedureLed() { return $this->dLed; } public function setProcedureId($iProcId) { $this->iProcId = $iProcId; } public function inputForm($asPost) { //User $this->iUserId = $asPost['id_user']; //Titles $this->sTitle = isset($asPost['title'])?$asPost['title']:''; $this->sDescription = isset($asPost['description'])?$asPost['description']:''; //Steps $sStepPattern = '/c(?P\d+)_step_text/u'; $sImagePattern = '/c(?P\d+)_(?P\d+)_image_(?P\w+)/u'; foreach($asPost as $sFormId=>$sValue) { //Step Text preg_match($sStepPattern, $sFormId, $asMatches); if(isset($asMatches['step_id'])) { $this->asSteps[$asMatches['step_id']] = $sValue; } else { preg_match($sImagePattern, $sFormId, $asMatches); if ( isset($asMatches['step_id'], $asMatches['image_id'], $asMatches['image_info']) && ($asMatches['image_info'] == 'name' || $asMatches['image_info']== 'desc') ) { $this->asImages[$asMatches['step_id']][$asMatches['image_id']][$asMatches['image_info']] = $sValue; } } } } private function isImageSaved($sFileName, $bInTemp=false) { $sPath = $bInTemp?self::IMAGE_FOLDER_TMP:self::IMAGE_FOLDER; return file_exists($sPath.$sFileName); } public function checkIntegrity() { $asErrors = array(); //Check title if($this->sTitle=='') $asErrors['title'] = 'Un titre est nécessaire'; //Check steps if(count($this->asSteps)==0) { $asErrors['step'] = 'Au moins une étape est nécessaire'; } else { foreach($this->asSteps as $iStepId=>$sText) { if($sText=='') { $asErrors['step_'.$iStepId] = 'Une étape ne possède pas de texte'; } } } //Check images foreach($this->asImages as $iStepId=>$asImages) { foreach($asImages as $iImageId=>$asFileInfo) { //file exists in temp or already stored if(!$this->isImageSaved($asFileInfo['name']) && !$this->isImageSaved($asFileInfo['name'], true)) { $asErrors['image_'.$iStepId.'_'.$iImageId] = 'Une image n\'a pas été téléchargée'; } if($asFileInfo['desc']=='') { $asErrors['image_'.$iStepId.'_'.$iImageId] = 'Une image n\'a pas de nom (description)'; } } } return $asErrors; } public function saveProcedure($iPrevProcId) { //Add new procedure $asData = array('title'=>$this->sTitle, 'description'=>$this->sDescription, MySqlManager::getId(Databap::USER_TABLE)=>$this->iUserId); $iDbProcId = $this->oMySql->insertRow(Databap::PROC_TABLE, $asData); //Add new steps $asStepData = $asImgData = array(MySqlManager::getId(Databap::PROC_TABLE)=>$iDbProcId); foreach($this->asSteps as $iStepId=>$sStepDesc) { $asStepData['description'] = $sStepDesc; $iDbStepId = $this->oMySql->insertRow(Databap::STEP_TABLE, $asStepData); //Add new images if(array_key_exists($iStepId, $this->asImages)) { $asImgData[MySqlManager::getId(Databap::STEP_TABLE)] = $iDbStepId; foreach($this->asImages[$iStepId] as $asImageInfo) { $asImgData['description'] = $asImageInfo['desc']; $asImgData['file_name'] = $asImageInfo['name']; $iDbImageId = $this->oMySql->insertRow(Databap::IMG_TABLE, $asImgData); $this->saveImage($asImgData['file_name']); } } } //Add this procedure to the group $bCreation = ($iPrevProcId==0); //$this->oMySql->deleteRow(Databap::PROC_TABLE, $this->iPrevProcId); //$this->oMySql->updateRow(Databap::PROC_TABLE, $iPrevProcId, array('refer_id'=>$iDbProcId)); $iReferId = $bCreation?$iDbProcId:$this->oMySql->selectValue(Databap::PROC_TABLE, 'refer_id', $iPrevProcId); $this->oMySql->updateRow(Databap::PROC_TABLE, $iDbProcId, array('refer_id'=>$iReferId)); //Update new Procedure Id //$this->setProcedureId($iDbProcId); //Reload Procedure $this->loadProcedure($iDbProcId); //return creation or edition return $bCreation; } private function saveImage($sFileName) { if(!$this->isImageSaved($sFileName)) { $sTempFilePath = self::IMAGE_FOLDER_TMP.$sFileName; $sFilePath = self::IMAGE_FOLDER.$sFileName; //Real Size Image if(!rename($sTempFilePath, $sFilePath)) { $this->addError('Unmoveable file : '.$sTempFilePath); } //Thumbnail picture $asResult = ToolBox::createThumbnail($sFilePath, self::THUMB_MAX_SIZE, self::THUMB_MAX_SIZE, self::IMAGE_FOLDER_THUMB, false, Databap::$UPLOAD_IMG_EXTS, true); if($asResult['error']!='') $this->addError('Unable to create thumbnail : '.$asResult['out']. '('.$asResult['error'].')'); } } public function loadProcedure($iProcId) { //Column names $sProcIdCol = MySqlManager::getId(Databap::PROC_TABLE); $sStepIdCol = MySqlManager::getId(Databap::STEP_TABLE); $sImageIdCol = MySqlManager::getId(Databap::IMG_TABLE); //Get last id available $this->setProcedureId($iProcId); $this->refreshProcId(); $asInfo = array('from'=>Databap::PROC_TABLE, 'constraint'=>array($sProcIdCol=>$this->getProcedureId())); //Load procedure info $asResult = $this->oMySql->selectRow(Databap::PROC_TABLE, $asInfo['constraint']); $this->sTitle = $asResult['title']; $this->sDescription = $asResult['description']; $this->iUserId = $asResult[MySqlManager::getId(Databap::USER_TABLE)]; $this->dLed = $asResult['led']; //Load steps info $asInfo['from'] = Databap::STEP_TABLE; $this->asSteps = $this->oMySql->selectRows($asInfo, true, $sStepIdCol); //Load images info $asInfo['from'] = Databap::IMG_TABLE; foreach($this->oMySql->selectRows($asInfo) as $asRow) { $this->asImages[$asRow[$sStepIdCol]][$asRow[$sImageIdCol]]['name'] = $asRow['file_name']; $this->asImages[$asRow[$sStepIdCol]][$asRow[$sImageIdCol]]['desc'] = $asRow['description']; } } public function getProcedure() { foreach($this->asSteps as $iStepId=>$asStep) { $this->asSteps[$iStepId]['formated_description'] = ToolBox::formatText($asStep['description']); } return array( 'proc_id'=>$this->iProcId, 'id_user'=>$this->iUserId, 'title'=>Databap::getDescriptionFormat($this->sTitle), 'description'=>Databap::getDescriptionFormat($this->sDescription), 'led'=>Databap::getDateFormat($this->dLed), 'steps'=>$this->asSteps, 'images'=>$this->asImages); } private function refreshProcId() { /* $iReferId = $this->getProcedureId(); do { $iProcId = $iReferId; $iReferId = $this->oMySql->selectValue(Databap::PROC_TABLE, 'refer_id', $iProcId); } while($iReferId!=0); $this->setProcedureId($iProcId); */ $iReferId = $this->oMySql->selectValue(Databap::PROC_TABLE, 'refer_id', $this->getProcedureId()); $sSelect = "MAX(".MySqlManager::getId(Databap::PROC_TABLE).")"; $iProcId = $this->oMySql->selectValue(Databap::PROC_TABLE, $sSelect, array('refer_id'=>$iReferId)); $this->setProcedureId($iProcId); } } ?>