#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,167
    Rep Power
    220

    How can I make this logic a global easy to use module?


    Hi;

    It looks pretty abd to set html code in controller.
    It also looks bad to have login in view.

    wtf?

    I think I want to use set_video_access() as a global module so I could access it from different controllers and have both controller and views nice and tidy..

    What's the best way?

    Thanks


    PHP Code:
    <?php
    defined
    ('BASEPATH') OR exit('No direct script access allowed');

    class 
    Programs extends CI_Controller {

        public function 
    load($id=NULL)
            {
                
    $this->validate($id);
                
    $this->data['list_videos'] = $this->get_data($id);
                
    $this->set_video_access();
                
    $this->load_views();
            }
        private function 
    validate($id=NULL)
            {
                
    $this->login_library->is_logged();
                if(
    $id==NULL)
                    {
                        
    $this->error();
                    }
                
    $query $this->db->get_where('pum_programs', array('id' => $id));
                if(
    $query->num_rows()==1)
                    {
                        return 
    true;
                    }
                else 
                    {
                        
    $this->error();
                    }        
            }
        protected function 
    get_data($id)
            {
                
    $query $this->db->get_where('pum_programs', array('id' => $id));
                
    $this->data['program_title']=$query->result_array()[0]['title'];
                
    $this->data['program_id']=$id;
                
    $this->data['program_thumbnail']=$query->result_array()[0]['program_thumbnail'];
                
    $query_videos $this->db->query("SELECT * FROM pum_videos WHERE program_id = ".$id." ORDER BY sort");
                return 
    $query_videos->result_array();
            }
        private function 
    load_views()
            {
                
    $this->load->view('header'$this->data);
                
    $this->load->view('nav'$this->data);
                
    $this->load->view('program_view'$this->data);
                
    $this->load->view('footer'$this->data);
            }
        protected function 
    error()
            {
                echo 
    "Invalid Request";exit();
            }
        protected function 
    set_video_access()
            {
                foreach(
    $this->data['list_videos'] as $val =>$row)
                    {
                        foreach(
    $_SESSION['membership_level_data'] as $membership_levels_val =>$membership_levels_row)
                                {
                                    if(
    $membership_levels_row['membership_level_id']==3)
                                        {
                                            
    $package 'economy';
                                        }
                                    if(
    $membership_levels_row['membership_level_id']==2)
                                        {
                                            
    $package 'gold';
                                        }
                                    if(
    $membership_levels_row['membership_level_id']==1)
                                        {
                                            
    $package 'platinum';
                                        }
                                    
    $video_release_after $row['release_day_after_signup_'.$package.'_package'];
                                    
                                    
    $membership_started $membership_levels_row['days_from_transaction']." days ago";
                                            
    $video_release_after $row['release_day_after_signup_'.$package.'_package'];
                                            if(
    $video_release_after<=$membership_levels_row['days_from_transaction'])
                                                {
                                                    
    $this->data['list_videos'][$val]['access_to_this_video'] = '<small><mark class = "green"><i class="fa fa-unlock" aria-hidden="true"></i> Available Now</mark></small>';
                                                    
    $this->data['list_videos'][$val]['watch_button'] = '<a href = "'.base_url('watch/'.$row['program_id']."/".$row['id']).'"><button type="button" class="btn btn-primary btn-xs">Watch <i class="fa fa-play" aria-hidden="true"></i></button></a>';
                                                }
                                            else 
                                                {
                                                    
    $when $row['release_day_after_signup_'.$package.'_package'] - $membership_levels_row['days_from_transaction'];
                                                    
    $this->data['list_videos'][$val]['access_to_this_video'] = '<small><mark class = "red"> <i class="fa fa-hourglass-start" aria-hidden="true"></i> Available in '.$when.' day(s)</mark></small>';
                                                    
    $this->data['list_videos'][$val]['watch_button'] = '<button type="button" class="btn btn-warning btn-xs">Coming Soon</button>';
                                                }  
                                    
    $membership_levels_id $membership_levels_row['membership_level_id'];
                                }
                            
    $pos strpos($row['package'], ''.$membership_levels_id.'');
                            if(
    $pos === false)                 
                                {
                                    
    $when "";
                                    
    $this->data['list_videos'][$val]['access_to_this_video'] = '<small><mark class = "red"> <i class="fa fa-lock" aria-hidden="true"></i> Not included in your membership </mark></small>';
                                    
    $this->data['list_videos'][$val]['watch_button'] = '<button type="button" class="btn btn-danger btn-xs">Upgrade</button>';
                                }    
                    }
            }
    }
  2. #2
  3. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,167
    Rep Power
    220
    I came up with this. Might be righ, works fine but I'm really not sure if this is good or bad practise.

    Basically I can just do this
    PHP Code:
    $result $this->get_video_access_library->check($video_id); 

    PHP Code:
    protected function set_video_access()
            {
                foreach(
    $this->data['list_videos'] as $val =>$row)
                    {
                        
    $result $this->get_video_access_library->check($row['id']);    
        
    $this->data['list_videos'][$val]['access_to_this_video']=$result['access_to_this_video'];
        
    $this->data['list_videos'][$val]['watch_button']=$result['watch_button'];
                    }
                    
            } 
    And then have this as a library
    PHP Code:
    <?php
    defined
    ('BASEPATH') OR exit('No direct script access allowed');

        class 
    Get_video_access_library 
              
    {
                   public function 
    check($video_id)
                          {
                          
    $CI =& get_instance();
                              
    $result = array();

                   
    $query $CI->db->get_where('pum_videos', array('id' => $video_id));
                   
    $video_details $query->result_array()[0];

                   foreach(
    $_SESSION['membership_level_data'] as $membership_levels_val =>$membership_levels_row)
                                {
                                    if(
    $membership_levels_row['membership_level_id']==3)
                                        {
                                            
    $package 'economy';
                                        }
                                    if(
    $membership_levels_row['membership_level_id']==2)
                                        {
                                            
    $package 'gold';
                                        }
                                    if(
    $membership_levels_row['membership_level_id']==1)
                                        {
                                            
    $package 'platinum';
                                        }
                                    
    $video_release_after $video_details['release_day_after_signup_'.$package.'_package'];
                                    
                                    
    $membership_started $membership_levels_row['days_from_transaction']." days ago";
                                            
    $video_release_after $video_details['release_day_after_signup_'.$package.'_package'];
                                            if(
    $video_release_after<=$membership_levels_row['days_from_transaction'])
                                                {
                                                    
                                                    
    $result['access_to_this_video'] =  '<small><mark class = "green"><i class="fa fa-unlock" aria-hidden="true"></i> Available Now</mark></small>';
                                                    
    $result['watch_button'] = '<a href = "'.base_url('watch/'.$video_details['program_id']."/".$video_details['id']).'"><button type="button" class="btn btn-primary btn-xs">Watch <i class="fa fa-play" aria-hidden="true"></i></button></a>';
                                                }
                                            else 
                                                {
                                                    
    $when $video_details['release_day_after_signup_'.$package.'_package'] - $membership_levels_row['days_from_transaction'];
                                                    
    $result['access_to_this_video'] = '<small><mark class = "red"> <i class="fa fa-hourglass-start" aria-hidden="true"></i> Available in '.$when.' day(s)</mark></small>';
                                                    
    $result['watch_button'] = '<button type="button" class="btn btn-warning btn-xs">Coming Soon</button>';
                                                }  
                                    
    $membership_levels_id $membership_levels_row['membership_level_id'];
                                       }
                                  
    $pos strpos($video_details['package'], ''.$membership_levels_id.'');
                                  if(
    $pos === false)                 
                                      {
                                          
    $when "";
                                          
    $result['list_videos']['access_to_this_video'] = $CI->data['list_videos'][$val]['access_to_this_video'] = '<small><mark class = "red"> <i class="fa fa-lock" aria-hidden="true"></i> Not included in your membership </mark></small>';
                                          
    $result['list_videos']['watch_button'] = $CI->data['list_videos'][$val]['watch_button'] = '<button type="button" class="btn btn-danger btn-xs">Upgrade</button>';
                                      }
              return 
    $result;
              }
         }
    Last edited by English Breakfast Tea; August 13th, 2017 at 04:12 AM.

IMN logo majestic logo threadwatch logo seochat tools logo