Kohana


CONTROLLER:


<?php defined('SYSPATH') OR die('No direct access allowed.');
class Welcome_Controller extends Layout_Controller
{
    const ALLOW_PRODUCTION = FALSE;
    public $template = 'kohana/template';
    public function __construct()
    {
        parent::__construct();
        $this->home = new Home_Model();
    }

}


ROUTES:

$config['admin-deal/cash-delivery.html'] = "/admin_deals/cash_on_delivery";
$config['admin-deal/cash-delivery.html/page/(.*)'] = "/admin_deals/cash_on_delivery/$1";

PATH:

        $path = explode('.',$_SERVER['SCRIPT_NAME']);
        $url = explode('/',$path[0]);
        $cnt = count($url)-1;
        $SEGMENT = "";
        for($i=0; $i<$cnt; $i++){
            $SEGMENT .= $url[$i].'/';
        }
        $this->session = Session::instance();
        define('PATH', "http://".$_SERVER['HTTP_HOST'].$SEGMENT);


ADD:

    public function add_deal()
    {
            if($_POST){
                $this->userPost = $this->input->post();
                $post = Validation::factory(array_merge($_POST,$_FILES))
                                ->pre_filter('trim', 'title')
                                ->add_rules('title', 'required')
                                ->add_rules('description', 'required',array($this,'check_required'))
                                ->add_rules('image', 'upload::valid', 'upload::type[gif,jpg,png,jpeg]', 'upload::size[1M]');
                       if($post->validate()){
                    $deal_key = text::random($type = 'alnum', $length = 8);
                    $status = $this->deals->add_deals(arr::to_object($this->userPost),$deal_key,$adminid);
                    if($status > 0 && $deal_key){
                            if($_FILES['image']['name']['0'] != "" ){
                                $i=1;
                            foreach(arr::rotate($_FILES['image']) as $files){
                                if($files){
                                    $filename = upload::save($files);
                                    if($filename!=''){

                                        $IMG_NAME = $deal_key."_".$i.'.png';
                                        common::image($filename, 620,752, DOCROOT.'images/deals/1000_800/'.$IMG_NAME);
                                        unlink($filename);
                                    }
                                }
                                $i++;
                              }
                            }

                        common::message(1, $this->Lang["DEAL_ADD_SUC"]);
                        url::redirect(PATH."admin/manage-deals.html");
                    }
                    $this->form_error["city"] = $this->Lang["DEAL_EXIST"];
                }
                else{
                    $this->form_error = error::_error($post->errors());
                }
            }
        $this->category_list = $this->deals->get_category_list();
        $this->template->title = $this->Lang["ADD_DEALS"];
        $this->template->content = new View("admin_deal/add_deals");
    }

MANAGE:
    public function manage_deals($type= "", $page = "")
    {
        $this->page = $page ==""?1:$page;
        $this->type = $this->input->get('sort');
       $this->sort = $this->input->get('param');
        $serch=$this->input->get("id");
         
        $this->count_deal_all_record = $this->deals->get_all_deals_count();
        $this->pagination = new Pagination(array(
                'base_url'       => $this->url.'/page/'.$page."/",
                'uri_segment'    => 4,
                'total_items'    => $this->count_deal_all_record,
                'items_per_page' => 25,
                'style'          => 'digg',
                'auto_hide' => TRUE
        ));
        $this->all_deal_list = $this->deals->get_all_deals_list($this->pagination->sql_offset, $this->pagination->items_per_page);
           
        $this->template->content = new View("admin_deal/manage_deals");
    }



MODEL:

<?php defined('SYSPATH') or die('No direct script access.');
class Home_Model extends Model
{
    public function __construct()
    {
       parent::__construct();
       $this->db = new Database();
       $this->session = Session::instance();
       $this->city_id = $this->session->get("CityID");
       $this->UserID = $this->session->get("UserID");
    }

}

$this->session->set(array("UserID" => $a->user_id, "UserName" => $a->firstname , "UserEmail" => $a->email, "city_id" => $a->city_id,"UserType" => $a->user_type));

text::random($type = 'alnum', $length = 8);


VIEW:

 <tr>
                   <td><label><?php echo $this->Lang["TITLE"]; ?></label> <span>*</span></td>
          
                   <td><label>:</label></td>
                   <td>
                       <input type="text" name="title" value="<?php if(!isset($this->form_error["title"])&&isset($this->userPost["title"])){ echo $this->userPost["title"]; }?>" />
                         <em><?php if(isset($this->form_error["title"])){ echo $this->form_error["title"]; }?></em>
                      </td>
               </tr>



.htaccess:


# Turn on URL rewriting
RewriteEngine On

# Installation directory
#RewriteBase /kohana/

# Protect application and system files from being viewed
RewriteRule ^(application|system) - [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

#RewriteRule ^(.*)$ index.php?/$0 [L]


Phone number validation

      ->add_rules('mobile',array($this, 'validphone'), 'chars[0-9-+().]');

   public function validphone($phone = "")
    {
        if(valid::phone($phone,array(7,10,11,12,13)) == TRUE){
            return 1;
        }
        return 0;
    }

Error file: (Application/helpers/error.php)

<?php defined('SYSPATH') OR die('No direct access allowed.');
class error {

    /** ERROR HANDLING **/
   
       public function _error($err_arr = "")
        {
            $my_error = array();
            foreach($err_arr as $key => $value)
            {
                switch ($value) {
                    case 'Required*':
                       $my_error[$key] = $this->Lang["REQQ"];
                       break;

        }
            }
            return $my_error;
        }

        public function setMessage($type = "", $msg = "")
        {
            $session = Session::instance();
            if($type == 1){
                $session->set("Msg",$msg);
            }elseif($type == -1)
            {
                $session->set("Emsg",$msg);
            }
        }

        }


Ajax:
 
    $.ajax({
      url: PATHS+"dashboard/delete_notification?redirectUrl="+redirectUrl+"&notify_type="+notifyType+"&notify_status="+notifyStatus,
      type:'get',
      success: function(result) {
          if(result) {
             Condition
          }
      }
   });