/** * retrieves a current logo file */ public function getLogo() { $documents = new DocumentTeam(); $documents->ID_TEAM = $this->ID; $documents->STATUS = 1; $documents->ID_DOCUMENT = 11; foreach ($documents->search()->getData() as $document) { } $path = isset($document) ? $document->PATH : ($thumbURL = Yii::app()->getBaseUrl() . '/uploads/default/defaultTeam.jpg'); return $path; }
/** * * @param int $id * @throws CHttpException */ public function actionUploadDocument($id) { $this->loadModel($id); Yii::import("xupload.models.XUploadForm"); //Here we define the paths where the files will be stored temporarily $path = realpath(Yii::app()->getBasePath() . "/../uploads") . "/teams/{$id}/"; $publicPath = Yii::app()->getBaseUrl() . "/uploads/teams/{$id}/"; //This is for IE which doens't handle 'Content-type: application/json' correctly header('Vary: Accept'); if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) { header('Content-type: application/json'); } else { header('Content-type: text/plain'); } //Here we check if we are deleting and uploaded file if (isset($_GET["_method"])) { if ($_GET["_method"] == "delete") { if ($_GET["file"][0] !== '.') { $file = $path . $_GET["file"]; if (is_file($file)) { $idDocumentModel = $_GET["idDocumentModel"]; $documentModel = DocumentTeam::model()->findByPk($idDocumentModel); $documentModel->STATUS = 0; $documentModel->save(); unlink($file); } } echo json_encode(true); } } else { $model = new XUploadForm(); $model->file = CUploadedFile::getInstance($model, 'file'); //We check that the file was successfully uploaded if ($model->file !== null) { //Grab some data $model->mime_type = $model->file->getType(); $model->size = $model->file->getSize(); $model->name = $model->file->getName(); //(optional) Generate a random name for our file $filename = md5(Yii::app()->user->id . microtime() . $model->name); $filename .= "." . $model->file->getExtensionName(); if ($model->validate()) { if (!is_dir($path . 'thumbs/')) { mkdir($path . 'thumbs/', 0777, true); } //Move our file to our temporary dir $model->file->saveAs($path . $filename); chmod($path . $filename, 0777); //here you can also generate the image versions you need //using something like PHPThumb try { $thumb = Yii::app()->phpThumb->create($path . $filename); $thumb->resize(100, 100); $thumb->save($path . "thumbs/{$filename}"); $thumbURL = $publicPath . "thumbs/{$filename}"; } catch (Exception $e) { $thumbURL = Yii::app()->getBaseUrl() . "/uploads/default/defaultFile.png"; } //Now we need to save this path to the user's session if (Yii::app()->user->hasState('images')) { $userImages = Yii::app()->user->getState('images'); } else { $userImages = array(); } $userImages[] = array("path" => $path . $filename, "thumb" => $path . $filename, "filename" => $filename, 'size' => $model->size, 'mime' => $model->mime_type, 'name' => $model->name); Yii::app()->user->setState('images', $userImages); /** * Database implementation * * * */ $postDocuments = array(); $postDocuments = $_POST['_DOCUMENT']; $document = $postDocuments[$model->name]; $documentToSave = new DocumentTeam(); $documentToSave->ID_DOCUMENT = $document['TYPE']; $documentToSave->NAME = $model->name; $documentToSave->DESCRIPTION = $document['DESCRIPTION']; $documentToSave->setOwnerId($id); $documentToSave->PATH = $publicPath . $filename; $documentToSave->SIZE = $model->size; $documentToSave->TYPE = $model->mime_type; $documentToSave->THUMBNAIL = $thumbURL; $documentToSave->DELURL = $this->createUrl("uploadDocument", array("id" => $id, "_method" => "delete", "file" => $filename)); $documentToSave->DELTYPE = 'POST'; $documentToSave->STATUS = 1; $documentToSave->save(); //Now we need to tell our widget that the upload was succesfull //We do so, using the json structure defined in // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup echo json_encode(array(array("name" => $model->name, "type" => $model->mime_type, "size" => $model->size, "url" => $publicPath . $filename, "description" => $documentToSave->DESCRIPTION, "tDocument" => $documentToSave->iDDOCUMENT->NAME, "thumbnail_url" => $thumbURL, "delete_url" => $this->createUrl("uploadDocument", array("id" => $id, "_method" => "delete", "file" => $filename, "idDocumentModel" => $documentToSave->ID_DOCUMENT_TEAM)), "delete_type" => "POST"))); } else { //If the upload failed for some reason we log some data and let the widget know echo json_encode(array(array("error" => $model->getErrors('file')))); Yii::log("XUploadAction: " . CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "xupload.actions.XUploadAction"); } } else { throw new CHttpException(500, "Could not upload file"); } } }