private function saveAndDeleteMedia($type) { /** @var $model Media [ ] */ $model = new Media(); $mockRecord = $this->getMockRecord(); $instruction = Deal::model()->findByPk($mockRecord['med_row']); $this->assertNotEmpty($instruction, "instruction not exist"); $property = Property::model()->findByPk($instruction->dea_prop); $this->assertNotEmpty($property, "property not exist"); $this->assertNotNull($property->addressId, "property has no address"); $address = Address::model()->findByPk($property->addressId); $this->assertNotEmpty($address, " Address not exist"); $model->setAttributes($this->getMockRecord()); $model->file = $this->getMockCuploadedImage('image/jpeg', 1); if ($type == Media::TYPE_PHOTO) { $model->setCropFactor($this->getCropFactor()); } elseif ($type == Media::TYPE_EPC || $type == Media::TYPE_FLOORPLAN) { $model->otherMedia = $type; } $this->assertTrue($model->validate(), "record not validated"); $this->assertTrue($model->save(), "record not saved"); foreach ($model->getImageSizes() as $imageSize) { $this->assertFileExists($model->getFullPath($imageSize), $imageSize . " does not exist"); } $this->deleteMedia($model->med_id); }
/** * Save uploaded file and add associated media object */ public function saveAssociatedMedia($file) { if (!$file instanceof CUploadedFile) { return; } $fileAttribute = $this->fileAttribute; $media = new Media(); $username = Yii::app()->user->getName(); // file uploaded through form $tempName = $file->getTempName(); $media->setAttributes(array('associationType' => $this->associationType, 'associationId' => $this->getAssociationId(), 'uploadedBy' => $username, 'createDate' => time(), 'lastUpdated' => time(), 'fileName' => preg_replace('/ /', '_', $file->getName()), 'mimetype' => $file->type), false); $media->resolveNameConflicts(); if (!$media->save()) { throw new CException(implode(';', $media->getAllErrorMessages())); } if (!FileUtil::ccopy($tempName, "uploads/protected/media/{$username}/{$media->fileName}")) { throw new CException(); } }
public function save($runValidation = true, $attributes = null) { if ($this->photo) { // save related photo record $transaction = Yii::app()->db->beginTransaction(); try { // save the event $ret = parent::save($runValidation, $attributes); if (!$ret) { throw new CException(implode(';', $this->getAllErrorMessages())); } // add media record for file $media = new Media(); $media->setAttributes(array('fileName' => $this->photo->getName(), 'mimetype' => $this->photo->type), false); $media->resolveNameConflicts(); if (!$media->save()) { throw new CException(implode(';', $media->getAllErrorMessages())); } // save the file $tempName = $this->photo->getTempName(); $username = Yii::app()->user->getName(); if (!FileUtil::ccopy($tempName, "uploads/protected/media/{$username}/{$media->fileName}")) { throw new CException(); } // relate file to event $join = new RelationshipsJoin('insert', 'x2_events_to_media'); $join->eventsId = $this->id; $join->mediaId = $media->id; if (!$join->save()) { throw new CException(implode(';', $join->getAllErrorMessages())); } $transaction->commit(); return $ret; } catch (CException $e) { $transaction->rollback(); return false; } } else { return parent::save($runValidation, $attributes); } }
public function convertToMedia(array $attributes = array()) { $username = Yii::app()->user->getName(); $name = $this->name; $tempFilename = 'uploads/protected/media/temp/' . $this->folder . '/' . $this->name; if (FileUtil::ccopy($tempFilename, "uploads/protected/media/{$username}/{$name}")) { $model = new Media(); $model->name = $model->fileName = $name; $model->uploadedBy = $username; $model->createDate = time(); $model->lastUpdated = time(); $model->resolveType(); $model->resolveSize(); $model->setAttributes($attributes, false); if ($model->save()) { return $model; } } return false; }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionUpload() { $model = new Media(); if (isset($_POST['Media'])) { $temp = TempFile::model()->findByPk($_POST['TempFileId']); $userFolder = Yii::app()->user->name; // place uploaded files in a folder named with the username of the user that uploaded the file $userFolderPath = 'uploads/protected/media/' . $userFolder; // if user folder doesn't exit, try to create it if (!(file_exists($userFolderPath) && is_dir($userFolderPath))) { if (!@mkdir('uploads/protected/media/' . $userFolder, 0777, true)) { // make dir with edit permission // ERROR: Couldn't create user folder var_dump($userFolder); exit; } } rename($temp->fullpath(), $userFolderPath . '/' . $temp->name); // save media info $model->fileName = $temp->name; $model->createDate = time(); $model->lastUpdated = time(); $model->uploadedBy = Yii::app()->user->name; $model->setAttributes($_POST['Media']); $model->path; // File type setter is embedded in the magic getter for path if (empty($model->name)) { $model->name = $model->fileName; } if (empty($model->associationType)) { $model->associationType = 'none'; } if ($model->save()) { $this->createAttachmentAction($model); $this->redirect(array('view', 'id' => $model->id)); } } /* uncomment when media module supports custom forms if(isset($_POST['x2ajax'])){ $this->renderInlineCreateForm ($model, isset ($ajaxErrors) ? $ajaxErrors : false); } else { */ $this->render('upload', array('model' => $model)); //} }