Share Pastebin
Guest
Public paste!

Philip Sturgeon

By: a guest | Jun 18th, 2007 | Syntax: PHP | Size: 6.09 KB | Hits: 23 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package             CodeIgniter
  8.  * @author              Rick Ellis
  9.  * @copyright   Copyright (c) 2006, EllisLab, Inc.
  10.  * @license             http://www.codeignitor.com/user_guide/license.html
  11.  * @link                http://www.codeigniter.com
  12.  * @since               Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * CodeIgniter Admin Output Class
  20.  *
  21.  * Permits admin pages to be constructed easier.
  22.  *
  23.  * @package             CodeIgniter
  24.  * @subpackage  Libraries
  25.  * @category    Libraries
  26.  * @author              Philip Sturgeon
  27.  * @link               
  28.  */
  29. class Admin_output extends CI_Parser {
  30.  
  31.         var $filename = '';
  32.         var $title = '';
  33.         var $data = array();
  34.         var $sidebars = array('clock', 'to-do');
  35.        
  36.         var $mode = 'html';
  37.        
  38.         var $header = 'header.html';
  39.         var $footer = 'footer.html';
  40.  
  41.         var $wrap = true;
  42.        
  43.         var $pdf_filename = ''; // What will the PDF be stored as?
  44.  
  45.         /**
  46.          * Constructor - Calls the CI instance and sets a debug message
  47.          *
  48.          * The constructor can be passed an array of config values
  49.          */    
  50.         function Admin_output()
  51.         {              
  52.                 $CI =& get_instance();
  53.                 log_message('debug', "Admin Output Class Initialized");
  54.         }
  55.        
  56.         // --------------------------------------------------------------------
  57.  
  58.        
  59.         /**
  60.          * Set the title of the page
  61.          *
  62.          * @access      public
  63.          * @param       string
  64.          * @return      void
  65.          */    
  66.        
  67.         function title($title = '')
  68.         {
  69.                 if($title != '') $this->title = $title;
  70.         }
  71.        
  72.         /**
  73.          * Set the mode of the creation
  74.          *
  75.          * @access      public
  76.          * @param       string
  77.          
  78.          * @return      void
  79.          */    
  80.         function create($filename = '', $data = array(), $return = false)
  81.         {
  82.                 $CI =& get_instance();
  83.                
  84.                 if($filename != '') $this->filename = $filename;
  85.                 if($data != array()) $this->data = $data;
  86.                
  87.                 // Work out the basics the template will need
  88.                 $this->_set_defaults();
  89.                
  90.                 // Want header and footer file included, or just the main file?
  91.                 if($this->wrap === true):
  92.                         $output  = $this->parse('admincp/'.$this->header, $this->data, true);
  93.                         $output .= $this->parse('admincp/'.$this->filename, $this->data, true);
  94.                         $output .= $this->parse('admincp/'.$this->footer, $this->data, true);
  95.                 else:
  96.                         $output = $this->parse('admincp/'.$this->filename, $this->data, true);
  97.                 endif;
  98.                
  99.                 // Want it returned or output to browser?
  100.                 if($return):
  101.                         return $output;
  102.                 else:
  103.                         if($this->mode == 'pdf'):
  104.                                 /*
  105.                                 * Using the code from PDF library for Code Igniter applications
  106.                                 * Author: Phil Went, May 2007
  107.                                 * uses HTMLDOC http://www.htmldoc.org/ to create the pdfs
  108.                                 * modelled on the specialpdf mediawiki plugin by Thomas Hempel
  109.                                 */
  110.                                 $page = $this->pdf_filename;
  111.                                 $html = utf8_decode($output);
  112.                                 $mytemp = "/tmp/f" .time(). "-" .rand() . ".html";  
  113.                                 $article_f = fopen($mytemp,'w');
  114.                                 fwrite($article_f, $html);
  115.                                 fclose($article_f);
  116.                                 putenv("HTMLDOC_NOCGI=1");
  117.                                
  118.                                 # Write the content type to the client...
  119.                                 header("Content-Type: application/pdf");
  120.                                 header(sprintf('Content-Disposition: attachment; filename="%s.pdf"', $page));
  121.                                 flush();
  122.                
  123.                                 # if the page is on a HTTPS server and contains images that are on the HTTPS server AND also reachable with HTTP
  124.                                 # uncomment the next line
  125.                                
  126.                                 #system("perl -pi -e 's/img src=\"https:\/\//img src=\"http:\/\//g' '$mytemp'");
  127.                
  128.                                 # Run HTMLDOC to provide the PDF file to the user...
  129.                                 passthru("htmldoc -t pdf14 --charset iso-8859-1 --color --quiet --jpeg --webpage '$mytemp'");
  130.                                
  131.                                 unlink ($mytemp);
  132.                        
  133.                         else:
  134.                                 // Send it to output
  135.                                 $CI->output->set_output($output);
  136.                                
  137.                         endif;
  138.                 endif;
  139.         }
  140.        
  141.        
  142.         /**
  143.          * Should we include headers and footers?
  144.          *
  145.          * @access      public
  146.          * @param       string
  147.          * @return      void
  148.          */    
  149.         function wrap($wrap = true)
  150.         {
  151.                 $this->wrap = $wrap;
  152.         }
  153.        
  154.         /**
  155.          * Turn off wrap mode
  156.          *
  157.          * @access      public
  158.          * @param       string
  159.          * @return      void
  160.          */    
  161.         function mode($mode = '')
  162.         {
  163.                 if($mode != '') $this->mode = $mode;
  164.         }
  165.        
  166.        
  167.         function _set_defaults()
  168.         {
  169.        
  170.                 $CI =& get_instance();
  171.        
  172.                 $CI->load->library('session');
  173.                 $CI->load->model('employee_model');
  174.                
  175.                 // Get worker ID and worker Name
  176.                 if($this->data['workerID'] = $CI->session->userdata('workerID')):
  177.                         $CI->employee_model->fields = array('employeeFirstName', 'employeeLastName');
  178.                        
  179.                         $employee = $CI->employee_model->get($this->data['workerID']);
  180.                         $this->data['workerName'] = $employee['employeeFirstName'].' '.$employee['employeeLastName'];
  181.                         unset($employee);
  182.                 endif;
  183.                
  184.                 // Replace all underscores with spaces, then capitalise first letters of words
  185.                 $defaultTitle = ucwords(str_replace('_', ' ', $CI->uri->router->class .' > '.$CI->uri->router->method));
  186.                
  187.                 // Set the basic defaults
  188.                 $this->data['pageTitle'] = ($this->title != '') ? $this->title : $defaultTitle;        
  189.                 $this->data['sideBars'] = $this->_build_sidebars();
  190.                
  191.                 if($this->mode == 'print' or $this->mode == 'pdf'):
  192.                         $this->header = 'print_header.html';
  193.                         $this->footer = 'print_footer.html';
  194.                
  195.                         if(!isset($this->data['printDate'])):
  196.                                 $this->data['printDate'] = gmdate('d/m/y');
  197.                         endif;
  198.                        
  199.                         if(!isset($this->data['printTime'])):
  200.                                 $this->data['printTime'] = gmdate('H:i');
  201.                         endif;
  202.                 endif;
  203.                
  204.                 $this->data['if_manager'] = (isManager()) ? array(array()) : array();
  205.                 $this->data['if_not_manager'] = (!isManager()) ? array(array()) : array();
  206.                
  207.         }
  208.        
  209.         /**
  210.          * Set the mode of the creation
  211.          *
  212.          * @access      public
  213.          * @param       array
  214.          * @return      array
  215.          */    
  216.          
  217.         function _build_sidebars()
  218.         {
  219.                
  220.                 $sidebarArray = array();
  221.                
  222.                 foreach($this->sidebars as $sidebarName):
  223.  
  224.                         $block_data = array();
  225.                         include(APPPATH.'views/admincp/sidebars/'.$sidebarName.'_code.php');
  226.                        
  227.                         $this->data = array_merge($this->data, $block_data);
  228.                        
  229.                         $sidebarContent = $this->parse('admincp/sidebars/'.$sidebarName.'.html', $this->data, true);
  230.                         $sidebarArray[] = array('name' => $sidebarName, 'content' => $sidebarContent);
  231.                 endforeach;
  232.  
  233.                 return $sidebarArray;
  234.        
  235.         }
  236.        
  237. }
  238. // END Admin_output class
  239. ?>