Created
July 26, 2013 00:22
-
-
Save zurivy-myval/6085079 to your computer and use it in GitHub Desktop.
FB fotosoutez
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 | |
require_once 'dibi/dibi.php'; | |
class myDb { | |
const ADMIN = 'dev_admin'; | |
const ORGANIZER = 'dev_organizer'; | |
const PHOTOS = 'dev_photos'; | |
const PHOTOS_DESC = 'dev_photos.id DESC'; | |
const RATING = 'dev_rating'; | |
const SETTINGS = 'dev_settings'; | |
const USERS = 'dev_users'; | |
const MALE = 'm'; | |
const FEMALE = 'f'; | |
private $ini_array = array(); | |
public function __construct() { | |
$ini_array = parse_ini_file("config.ini", true); | |
dibi::connect($ini_array['database'] | |
); | |
} | |
public function getUser($id) { | |
return dibi::select('*') | |
->from(self::USERS) | |
->where('id = %i', $id) | |
->fetchAll(); | |
} | |
public function setUser(array $user) { | |
$count = dibi::select('*') | |
->from(self::USERS) | |
->where('id = %i', $user['id']) | |
->count(); | |
if ($count == 0) { | |
dibi::query('INSERT INTO ['.self::USERS.']', $user); | |
} | |
} | |
/* | |
* select by user_id | |
*/ | |
public function getPhoto($id) { | |
return dibi::select('*') | |
->from(self::PHOTOS) | |
->where('user_id = %i', $id) | |
->fetchAll(); | |
} | |
public function getPhotoByID($id) { | |
return dibi::select('*') | |
->from(self::PHOTOS) | |
->innerJoin(self::USERS) | |
->on(self::USERS.'.id = '.self::PHOTOS.'.user_id') | |
->where(self::PHOTOS.'.id = %i', $id) | |
->fetchAll(); | |
} | |
public function getPhotoByUser($id) { | |
return dibi::select('*, '.self::PHOTOS.'.id as pid') | |
->from(self::PHOTOS) | |
->innerJoin(self::USERS) | |
->on(self::USERS.'.id = '.self::PHOTOS.'.user_id') | |
->where(self::PHOTOS.'.user_id = %i', $id) | |
->fetchAll(); | |
} | |
public function insertPhoto(array $args) { | |
return dibi::query('INSERT INTO ['.self::PHOTOS.']', $args); | |
} | |
public function updatePhoto($id, array $args) { | |
return dibi::query('UPDATE ['.self::PHOTOS.'] SET', $args, 'WHERE id = %i', $id); | |
} | |
public function getGallery($orderBy = self::PHOTOS_DESC, $ofsset = 0, $limit = 9, $where = null) { | |
$select = dibi::select('*, '.self::PHOTOS.'.id as pid') | |
->from(self::PHOTOS) | |
->innerJoin(self::USERS) | |
->on(self::USERS.'.id = '.self::PHOTOS.'.user_id') | |
->orderBy($orderBy); | |
if(isset($where)){ | |
$select->where(self::PHOTOS.'.type LIKE "'.$where.'"'); | |
} | |
return $select->fetchAll($ofsset, $limit); | |
} | |
public function countImages($where = null) { | |
$select = dibi::select('*') | |
->from(self::PHOTOS); | |
if(isset($where)){ | |
$select->where(self::PHOTOS.'.type LIKE "'.$where.'"'); | |
} | |
return $select->count(); | |
} | |
public function getRatingByPhoto($photo_id){ | |
return dibi::select('rating') | |
->from(self::PHOTOS) | |
->where('id = %i', $photo_id) | |
->fetchSingle(); | |
} | |
public function selectRating($photo_id, $user_id) { | |
return dibi::select('*') | |
->from(self::RATING) | |
->where('user_id = %i AND photo_id = %i', $user_id, $photo_id) | |
->count(); | |
} | |
public function getLastRating($photo_id, $user_id){ | |
return dibi::select('timestamp') | |
->from(self::RATING) | |
->where('user_id = %i AND photo_id = %i', $user_id, $photo_id) | |
->orderBy('timestamp DESC') | |
->fetchSingle(); | |
} | |
public function saveRating($photo_id, $user_id) { | |
if (dibi::query('INSERT INTO ['.self::RATING.']', array('user_id' => $user_id, 'photo_id' => $photo_id))) { | |
return dibi::query('UPDATE ['.self::PHOTOS.'] SET rating = rating + 1 WHERE `id`=%i', $photo_id); | |
} | |
} | |
public function getRules() { | |
return dibi::select('condition') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function getPoradatel() { | |
return dibi::select('url') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function getCount() { | |
return dibi::select('count') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function getImgPerPage() { | |
return dibi::select('img_per_page') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function getEvent() { | |
return dibi::select('event') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function getOrganizer() { | |
return dibi::select('*') | |
->from(self::ORGANIZER) | |
->fetchAll(); | |
} | |
/* | |
* Admin | |
*/ | |
public function login($user, $pass) { | |
return dibi::select('*') | |
->from(self::ADMIN) | |
->where('user = %s AND pass = %s', $user, $pass) | |
->count(); | |
} | |
public function countRating() { | |
return dibi::select('*') | |
->from(self::RATING) | |
->count(); | |
} | |
public function updateSettings($args) { | |
return dibi::update(self::SETTINGS, $args) | |
->execute(); | |
} | |
public function getSettings() { | |
return dibi::select('*') | |
->from(self::SETTINGS) | |
->fetchAll(); | |
} | |
public function deleteImage($id) { | |
dibi::delete(self::RATING) | |
->where('photo_id = %i', $id) | |
->execute(); | |
return dibi::delete(self::PHOTOS) | |
->where('id = %i', $id) | |
->execute(); | |
} | |
public function searchGallery($search) { | |
return dibi::select('*, '.self::PHOTOS.'.id as pid') | |
->from(self::PHOTOS) | |
->innerJoin(self::USERS) | |
->on(self::USERS.'.id = '.self::PHOTOS.'.user_id') | |
->where(self::PHOTOS.'.photo_name = %s', $search) | |
->fetchAll(); | |
} | |
public function getUserByName($name) { | |
return dibi::select('id') | |
->from(self::USERS) | |
->where('username = %s', $name) | |
->fetchAll(); | |
} | |
public function searchGalleryByUser($id) { | |
return dibi::select('*, '.self::PHOTOS.'.id as pid') | |
->from(self::PHOTOS) | |
->innerJoin(self::USERS) | |
->on(self::USERS.'.id = '.self::PHOTOS.'.user_id') | |
->where(self::PHOTOS.'.user_id = %i', $id) | |
->fetchAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment