Welcome Guest, Not a member yet? Register   Sign In
Change string in controller from model
#11

[eluser]Swedie[/eluser]
You guys are right that I am probably not following the lines of what the MVC architecture is about.

I'll try and explain my application.

I am loading the header and footer from another website of ours with a completely different system. I've marked the HTML with a bunch of markers where I can go in and put, get and replace stuff. Once done this is done, it's outputted as my header and footer in my view file. Between the header and footer I put my own new content.

In one certain method I include a large system, that is not in the MVC framework completely and it's one class that loads a lot of other classes. Everything works fine except it's ISO-8859-1 on all files and data. So I have to put a header in the file of that class that tells me it's ISO-8859-1 and thus doing so, I need to runt utf8_decode or all Swedish characters are turned into symbols.

All this works now, as I changed some code in the controller that deals with it now. But it's really only a temporary thing, so I rather wished it wasn't in the controller at all, but in the method of the model that "imports" that old system of mine with the wrong characters encoding.

In the next two posts I'm showing you my still small controller for a system of ours that will evolve to something much larger in the near future.

I welcome and actually want you you to criticize my code and give suggestions both on how to improve the PHP coding but also how to improve the use of CI's MVC framework. Rather to it right now, when there's little code than going back later and correct a lot of code. Smile
#12

[eluser]Swedie[/eluser]
Code:
class Smc_boot extends Controller {

   var $website_url = "http://localhost:88";
   var $header = "";
   var $footer = "";
   var $cacheTimeout = 10; //minutes
    var $default_view = "smc";
    var $default_model = "Smcindex";
    var $default_method = "start";

   function Smc_boot()
   {
      parent::Controller();
      $this->_saveHeaderAndFooter(); //refresh Cache true or false
   }

   function index()
   {
        /* Always load default model */
        $this->load->model($this->default_model);
            
        /* set default view, model and method */
        $load_view = $this->default_view;
        $load_model = $this->default_model;
        $load_method = ($this->uri->segment(1) ? $this->uri->segment(1) : $this->default_method);
        
        /* process method from routing information */
        switch ($load_method) {
         case 'betalterminal':
                $load_model = "pay/Smcpay";
                $this->load->model('pay/Smcpay');
                $load_method = ($this->uri->segment(2) ? $this->uri->segment(2) : $this->default_method); /* overrides current load_method */
                
                /* load method */
                $data = array("data" => $this->$load_model->$load_method());
                
                /* get view */
                if(isset($_REQUEST['ordernumber']) AND $this->$load_model->payed($_REQUEST['ordernumber'])) {
                    $this->load->view("pay/payed_view.php", $data);
                } else {
                    if(!empty($this->$load_model->load_view)) {
                        $load_view = "pay/".$this->$load_model->load_view;
                    } else {
                        $load_view = "pay/".($this->uri->segment(2) ? $this->uri->segment(2) : "start");
                    }
                }
         break;
            default:
                /* if cannot detect new method (written with utf8) expect we are importing old system, that uses ISO-8859-1 */
                /* run utf8_decode on header and footer, ugly quick fix for including the old system */
                if($load_method !== $this->default_method) {
                    $this->header = utf8_decode($this->header);
                    $this->footer = utf8_decode($this->footer);
                }
                $data = array("data" => $this->$load_model->$load_method());
         break;
      }
        
        $hf = array(
         "header" => $this->header,
         "footer" => $this->footer
      );
            
        if(!$load_view) die("Missing View");
        if(!$load_model) die("Missing Model");
        if(!$load_method) die("Missing Method");
        $this->load->view($load_view."_view", array_merge($hf, $data));
   }
#13

[eluser]Swedie[/eluser]
Code:
function _saveHeaderAndFooter()
   {
      if ($hf = $this->_getHeaderAndFooter() AND is_array($hf))
      {
         $this->header = $hf['header'];
         $this->footer = $hf['footer'];
      } else
      {
         //Get current site theme
         $page = file_get_contents($this->website_url); //turns anything into UTF-8 ! arghh...
         $page = str_replace('"/', '"' . $this->website_url . '/', $page);

         //Split header and footer
         $html_array = $this->_getBeforeAndAfterTag($page, "content", FALSE);

         //Set header and footer
         $this->header = $html_array['before'] . '<div style="padding: 5px;">';
         $this->footer = '</div>' . $html_array['after'];

            //Insert in header
            $headerscript = "[removed][removed]
            [removed][removed]
         [removed][removed]
         &lt;link rel=\"stylesheet\" type=\"text/css\" href=\"/system/application/models/smc/styles.css\" media=\"screen\" /&gt;

         [removed]
         $(document).ready(function() {
            $(\"#submit_ani\").click(function(event){
                  event.preventDefault();
                  doAniSubmit();
            });
         });
         function doAniSubmit() {
            $(\"#submit_ani\").attr('disabled', true);
            $('<img >').appendTo('.span_ani');
            window.setTimeout(function() {
               $(\"#form_ani\").submit();
            },500);
         }
         [removed]
            ";
            $this->header = $this->_putBetweenTags($this->header, "custom header", $headerscript);

         //Alter menu
         $use_new_menu = TRUE; //on & off function to inherit SMC menu or place new menu
         $new_menu_array = array(
            "Bli medlem" => "/blimedlem",
            "Betalterminal" => "/betalterminal",
                "Kurser" => "/school",
            "Resor" => "/travel",
            "Hjälp" => array(
               "Vad kostar det?" => "/faq/vad-kostar-det",
               "Vem får bli medlem?" => "/faq/vem-far-bli-medlem"
                )
         );
         if ($use_new_menu && is_array($new_menu_array))
         {
            $new_menu = "";
            //Only supports 1 sub menu
            foreach ($new_menu_array as $menu_title => $menu_link)
            {
               $new_menu .= "<li><a ><span>" . $menu_title . "</span></a>";
               if (is_array($menu_link))
               {
                  $new_menu .= "<ul>\n";
                  foreach ($menu_link as $sub_menu_title => $sub_menu_link)
                  {
                     $new_menu .= "<li><a ><span>" . $sub_menu_title . "</span></a></li>\n";
                  }
                  $new_menu .= "</ul>\n";
               }
               $new_menu .= "</li>\n";
            }
            $old_menu = $this->_getBeforeAndAfterTag($this->header, "menu", TRUE);
            $this->header = str_replace($old_menu, $new_menu, $this->header);

            $this->db->where_in("name", array('header', 'footer'));
            $this->db->delete("cache");
                foreach(array("header" => $this->header, "footer" => $this->footer) AS $name => $result)
                    $this->db->insert("cache", array(
                                                                "name" => $name,
                                                                "result" => $result,
                                                                "time" => time()
                                                                ));
         }
      }
   }
    
    
   function _getBeforeAndAfterTag($string, $tag, $returnMiddle = FALSE)
   {
      $bits = explode("&lt;!-- $tag start --&gt;", $string);
      $array['before'] = $bits['0'] . "\n\n&lt;!-- $tag start --&gt;\n\n";
      $bits = explode("&lt;!-- $tag end --&gt;", $bits['1']);
      $array['middle'] = $bits['0'];
      $array['after'] = "\n\n&lt;!-- $tag end --&gt;\n\n" . $bits['1'];
      if ($returnMiddle)
         return $array['middle']; else
         return $array;
   }

   function _getHeaderAndFooter()
   {
      $this->db->where("time > " . (time() - ($this->cacheTimeout * 60)));
      $this->db->where("result != ''");
      $this->db->where_in('name', array('header', 'footer'));
      $this->db->order_by("time", "desc");
      $this->db->limit(2);
      $query = $this->db->get('cache');
      $array = array();
      foreach ($query->result() as $row)
      {
         if(!empty($row->result)) $array[$row->name] = $row->result;
      }
      return $array;
   }
    
    function _putBetweenTags($string, $tag, $put)
   {
      $bits = explode("&lt;!-- $tag start --&gt;", $string);
      $array['before'] = $bits['0'] . "\n\n&lt;!-- $tag start --&gt;\n\n";
      $bits = explode("&lt;!-- $tag end --&gt;", $bits['1']);
      $array['middle'] = $put;
      $array['after'] = "\n\n&lt;!-- $tag end --&gt;\n\n" . $bits['1'];
        return implode("", $array);
   }

}




Theme © iAndrew 2016 - Forum software by © MyBB