-
-
Save kamaulynder/b51e8a9ee5eaa7739c29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') OR die('No direct access allowed.'); | |
/** | |
* Ushahidi Media Repository | |
* | |
* @author Ushahidi Team <[email protected]> | |
* @package Ushahidi\Application | |
* @copyright 2014 Ushahidi | |
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero | |
* General Public License Version 3 (AGPL3) | |
*/ | |
use Ushahidi\Entity\Media; | |
use Ushahidi\Entity\MediaRepository; | |
use Ushahidi\Entity\User; | |
class Ushahidi_Repository_Media implements MediaRepository | |
{ | |
public function get($id) | |
{ | |
$query = DB::select('*') | |
->from('media') | |
->where('id', '=', $id) | |
; | |
$result = $query->execute(); | |
return new Media($result->current()); | |
} | |
public function add($media) | |
{ | |
$upload_dir = Kohana::$config->load('media.media_upload_dir'); | |
// Make media/uploads/ directory if it doesn't exist | |
if ( ! file_exists($upload_dir)) | |
{ | |
// Make directory recursively | |
mkdir($upload_dir, 0755, TRUE); | |
} | |
// Upload the file | |
$file = upload::save($media_data['file'], NULL, $upload_dir); | |
$filename = strtolower(Text::random('alnum', 3))."_".time(); | |
// Save original size | |
$o_image = Image::factory($file); | |
$o_image->save($upload_dir.$filename."_o.jpg"); | |
if (file_exists($file)) | |
{ | |
// Remove the temporary file | |
Unlink($file); | |
} | |
// Save details to the database | |
$media = $this->resource(); | |
// Link media with user | |
$media->user_id = $this->user->id; | |
// Set original details | |
$media->o_width = $o_image->width; | |
$media->o_height = $o_image->height; | |
$media->o_filename = $filename."_o.jpg"; | |
// Set mime type | |
$media->mime = $o_image->mime; | |
unset($media['id']); // always autoincrement | |
$query = DB::insert('media') | |
->columns(array_keys($media)) | |
->values(array_values($media)) | |
; | |
list($media->id, $count) = $query->execute(); | |
return (bool) $count; | |
} | |
public function getAllForUser($user_id) | |
{ | |
$query = DB::select('*') | |
->from('media') | |
->where('user_id', '=', $user_id); | |
$result = $query->execute(); | |
return (count($results) === 0); | |
} | |
public function remove($media_id) | |
{ | |
if (!$media_id) | |
{ | |
throw new Exception("Media does not have an id"); | |
} | |
$query = DB::delete('media') | |
->where('id', '=', $media_id) | |
; | |
$count = $query->execute(); | |
return $count; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment