Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/application/classes/ac/misc.php
<?php

class Ac_Misc
{
    public static function get_user_info()
    {
        //var_dump($_SERVER['REMOTE_ADDR']);
        //var_dump($_SERVER);
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['agent'] = $_SERVER['HTTP_USER_AGENT'];

        return serialize($data);
    }


    public static function countArtistFollowers($artistId)
    {
        $follow= new Model_Follow();
        return $follow->get_by_artist($artistId);
    }

    public static function countArtistAwards ($artist_id)
    {
        $votes = new Model_Vote();
        return $votes->get_by_artist($artist_id);
    }

    public static function countWorksVotes($artistId)
    {
        $vote= new Model_Vote();
        return $vote->get_by_artist($artistId);
    }
    
    public static function showAvatar($user)
    {
        $avatar_url = self::get_avatar_src($user);
        return '<img src="'.$avatar_url.'" width="33" height="33" alt="'.$user->firstname. ' '. $user->lastname.'" class="artist-avatar" />';
    }

    public static function get_avatar_src($user,$size=null)
    {
        //return self::get_avatar_scaled($user->avatar, $size);
        
        $avatar_url = '/static/images/avatar.png';
        if (strlen($user->avatar) > 3) {
            $avatar_url =  "/uploads/". $user->avatar.$size;
        }
        return $avatar_url;
    }

    public static function get_avatar_scaled($avatar_name, $width = null)
    {
        $default_avatar_src = '/static/images/avatar.png';
        return self::get_image_scaled($default_avatar_src, $avatar_name, $width);
    }

    public static function get_art_scaled($art_name, $width = null)
    {
        $default_image_src = Kohana::config('site.art_default_thumb');
        return self::get_image_scaled($default_image_src, $art_name, $width);
    }

    public static function get_image_scaled($default_image_src, $image_name, $width = null)
    {
        $return_image_src = $default_image_src;
        $target_image_suffix = '_'. $width. 'px.jpg';

        // jesli nie ma podanej szer zwracamy oryginal
        if ($width == null){
            $return_image_src = '/uploads/'. $image_name;
        } 
        // jesli jest szerokosc i istnieje oryginal to zwracamy przeskalowany
        elseif (strlen($image_name) > 5) {
            try {
                $target_image_path = UPLOAD_DIR. DIRECTORY_SEPARATOR. $image_name. $target_image_suffix ;
                if (!file_exists($target_image_path)) {
                    require_once APPPATH. '/vendors/PhpThumb/ThumbLib.inc.php';
                    $source_image_path = UPLOAD_DIR. DIRECTORY_SEPARATOR. $image_name;
                    $thumb = PhpThumbFactory::create($source_image_path, array('jpegQuality' => 95));
                    $thumb->resize($width, $width);
                    $thumb->save($target_image_path);
                    unset ($thumb);
                }
                $return_image_src = '/uploads/'. $image_name. $target_image_suffix;
            } catch (Exception $e) {
                // jak cos nie poszlo to zostaje domyslny ale przeskalowany
                $image_name = 'default';
                $target_image_path = UPLOAD_DIR. DIRECTORY_SEPARATOR. $image_name. $target_image_suffix ;
                if (!file_exists($target_image_path)) {
                    require_once APPPATH. '/vendors/PhpThumb/ThumbLib.inc.php';
                    $source_image_path =  APPPATH .'../' .$default_image_src;
                    $thumb = PhpThumbFactory::create($source_image_path, array('jpegQuality' => 95));
                    $thumb->resize($width, $width);
                    $thumb->save($target_image_path);
                    unset ($thumb);
                }
                $return_image_src = '/uploads/'. $image_name. $target_image_suffix;
            }
        }
        // w innym przypadku pozostaje obrazek domyslny ustawiony na poczatku

        return $return_image_src;
    }

    /**
     * Zwraca tekst daty
     * @param int timestap
     * @return string X minut temu | wczoraj | 12.01.2011
     */
    public static function whenDate ($time)
    {
        
        $oneDay = 60*60*24*1;//
        $now = time();
        // czy dzisiaj
        if (date(Kohana::config('site.dateFormat'), $time) ==
                date(Kohana::config('site.dateFormat'), $now)) {
            
            
            // czy godziny
            if (($now - $time) > 60*60){
                return floor(($now - $time)/60/60). ' '. __t('godz temu');
            }
            // czy minuty
            else if(($now - $time) > 60){
                return floor(($now - $time)/60).' '. __t('min temu');
            }
            // czyli sekundy
            else {
                return floor(($now - $time)).' '. __t('sek temu');
            }
        }
        // czy wczoraj
        //echo(date('d',$now-$time));
        if (((int)date('d',$now-$time-$oneDay)) == '1'){
            return __t('wczoraj');
        }
        // domyslnie pelna formatowana data
        return date(Kohana::config('site.dateFormat'), $time);
    }

    static public function howLong($time)
    {
        $one_day = 60*60*24*1;
        $now = time();
        // czy ten sam miesiac
        if (date('Ym',$now) == date('Ym',$time)) {
            $days =  (int)date('d',$now-$time);
            return '<strong>'. $days. '</strong> '. __t('dni');
        } else {
            //$years =  (int)date('Y',$now) - (int)date('Y',$time);
            //$years =  (int)date('Ymd',$now-$time);
            $years =  (int)date('Y',$now-$time)-1970;
            $moths =  (int)date('m',$now-$time) + $years*12;
            return '<strong>'. $moths. '</strong> '. __t('miesięcy');
        }
        
        
    }

    public static function artistList($artists,$is_rookies= false){

         return View::factory('artists/_list')
                ->bind('is_rookies',$is_rookies)
                ->bind('artists',$artists);
    }
    public static function commentsList($comments){
        
         return View::factory('arts/_comments')
                ->bind('comments',$comments);
    }

    public static function commentsForm($action){

        return View::factory('_partials/commentsform')->bind('action',$action);

    }


    public static function popularArtists(){

        $limit= Kohana::config('site.popular_artists_limit');

        $query = DB::query(Database::SELECT,
                'SELECT `users`.*,(SELECT count(vote) FROM `follows` WHERE `follows`.`artist_id`=`users`.`id`) as coun_vote
                    FROM `users`
                    WHERE `users`.`is_artist` = 1 
                    AND `users`.`logins` > 1 
                    ORDER BY `coun_vote` DESC
                    LIMIT '.$limit.'');

        $result = $query->as_object()->execute();

//        $artists= ORM::factory('user')
//                    ->where('is_artist','=',1)
//                    ->where('logins','>',1)
//                    ->limit($limit)
//                    ->find_all();
        return $result;
    }

    /**
     * Zwraca widok html dla komentarzy (partial View)
     * @param int $element_id
     * @param int $element_type art|user
     * @param int $page - opcjonalnie, bez podania przyjmuje 0
     * @return View
     */
    public static function xcommentsList($element_id, $element_type='art', $page=0){
        $hasMore    =   false;
        $limit      =   Kohana::config('site.comments_count');
        $offset     =   $limit*$page;

        $comments   =   ORM::factory('comment');
        $comments2  =   ORM::factory('comment');

        if ($element_type== 'art'){
            $comments2->where('art_id', '=', $element_id);
            $comments->where('art_id', '=', $element_id);
        }
        else {
            $comments2->where('user_id', '=', $element_id);
            $comments->where('user_id', '=', $element_id);
        }

        
        $commentsCount  =   $comments2->count_all();
        $commentList    =   $comments->limit($limit)->offset($offset)->order_by('id','desc')->find_all();


        $testPage= ($page ==0)?1: $page;
        if ($commentsCount > ($testPage *$limit)){
            $hasMore= true;
        }
        $allPages= ceil($commentsCount / $limit);
        return View::factory('_partials/comments')
                ->bind('page',          $page)
                ->bind('allPages',      $allPages)
                ->bind('hasMore',       $hasMore)
                ->bind('comments',      $commentList);
    }


    public static function pageName(){


        $action =  Request::current()->action;

        $name = Kohana::config('infosite.'.$action);

        
        return $name;
    }

}