Beispiel Source Code

Kontroller eines Backends


/**
 * ----------------------------------------------------------
 * Copyright (c) 2017 Jonas Bay
 * ----------------------------------------------------------
 * @author		  Jonas Bay
 */

  
class Backend_my_controller extends CI_Controller 
{
	protected $aHeaderFiles = array('aCssFiles' => array('backend/bootstrap.css', 'backend/bootstrap-theme.css', 'backend/buttons.css', 'backend/calendar.css', 'backend/forms.css', 'backend/stats.css', 'backend/styles.css', 'backend/animate.css'),
									'aJsFiles' => array('jquery.min.js', 'bootstrap.min.js', 'backend/custom.js', 'backend/bootstrap-notify.js', 'backend/loadNotifications.js'));
	protected $aModals = array();
	
	
   /**
   * name:        __construct
   *
   * 	Constructor 
   *
   * 	@param       $bLoadModel    	if true (standard) it loads the model with the same name as the controller
   *
   * 	@return      integer
   *
   **/
	
	public function __construct($bLoadModel = true) 
	{
    	parent::__construct();
    	$this->load->helper(array('url', 'backend_helper'));
    	$this->load->library(array('session', 'auth'));
    	
    	if ($this->uri->segment(2) == NULL) {
		   	$sSegment = 'Backend';
	    } else {
		   	$sSegment = ucfirst($this->uri->segment(2));
	    }

    	if ($bLoadModel) {
	    	$this->load->model($sSegment . '_model', 'Model', true);
    	}
    	
    	$this->auth->check();
    	$sModule = $sSegment;
    	if ($sModule != 'Backend') {
	    	$this->auth->checkModule($sSegment);
    	}
		
    }
    
    /**
   * 	name:        renderPage
   *
   * 	renders a page for the frontend
   *
   * 	@param		string  	$sView    		string with the name of the view
   * 	@param		array   	$aData	    	array with data which will be passed to the view
   * 	@param		array   	$aCssFiles    	CssFiles which are needed by the view
   *	@param		array   	$aJsFiles     	Js-Files with are needed by the view
   *
   **/
    
    public function renderPage($sView, $aData, $aCssFiles = array(), $aJsFiles = array()) {
	    $this->sView = $sView;
		
		$this->_loadCss($aCssFiles);
		$aHeader['aCss'] = $this->aHeaderFiles['aCssFiles'];
		$this->load->view('header_view', $aHeader);
		
		$this->load->view('top_nav_view', array());
		$this->load->view('left_nav_view', $aData);
			
		$this->load->view($sView . '_view', $aData);
		
		foreach($this->aModals as $k=>$v) {
			$this->load->view('modals/' . $v . '_modal', $aData);
		}
		
		$this->_loadJs($aJsFiles);
		$aFooter['aJs'] = $this->aHeaderFiles['aJsFiles'];
		$this->load->view('footer_view', $aFooter);   
	}

   /**
   * 	name:        _loadCss
   *
   * 	Private function: Loads additional css-files into the css-header-variable. 
   *
   * 	@param       array  $aCssFiles    array with Css-files
   *
   *
   **/
	
   	private function _loadCss($aCssFiles = array()) {
		// Load additional Css-Files
		if (count($aCssFiles) > 0) {
			foreach($aCssFiles as $k=>$v) {
				$this->aHeaderFiles['aCssFiles'][] = $v;
			}
		}
	}
   
   /**
   * 	name:        _loadJs
   *
   * 	Private function: Loads additional js-files into the js-header-variable. Also checks if a js-file with the name of the view is available.
   * 	If yes it adds this file.					
   *
   * 	@param       array  $aJsFiles    array with js-files
   *
   *
   **/
	
	private function _loadJs($aJsFiles = array()) {
		// Load additional JS-Files
		if (count($aJsFiles) > 0) {
			foreach($aJsFiles as $k=>$v) {
				$this->aHeaderFiles['aJsFiles'][] = $v;
			}
		}
		
		// Load js-File for view, if it exists
		$sUrl = 'js/backend/' . $this->sView . '.js';
		if (file_exists($sUrl)) {
			$this->aHeaderFiles['aJsFiles'][] = 'backend/' . $this->sView . '.js';
		}
	}
	
  /**
   * 	name:        addModal
   *
   * 	Adds a modal to the Class. When the page is rendered the added modals will be attached.				
   *
   * 	@param       mixed  $mModal    string with one modal or array with several modals
   *
   *
   **/
	
	public function addModal($mModal) {
		if (is_array($mModal)) {
			array_merge($this->aModals, $mModal);
		} else {
			array_push($this->aModals, $mModal);
		}
	}
	
  /**
   * 	name:        getNotifications
   *
   * 	Checks if there is a notification for the user in the session. If there is one it gets deleted in the db and puts it out as json		
   *
   * 	@param       
   *
   *
   **/
	
	public function getNotifications() {
		$this->db->where('user_id', $this->session->userdata('user'));
		$aNotifications = $this->db->get('notifications')->result_array();
		
		foreach($aNotifications as $k=>$v) {
			$this->db->where('id', $v['id']);
			$this->db->delete('notifications');
		}
		
		echo json_encode($aNotifications);
	}
	
	  /**
   * 	name:        _buildDropdown
   *
   * 	Prepares an array as key/value-pair so a view can build a dropdown.		
   *
   * 	@param      array	$aItems		array with all the data
   *	@param		string	$sKey		the key of the array-value which will be the option-value
   *	@param		string	$sValue		the key of the array-value which will be displayed in the select
   *
   **/
	
	public function _buildDropdown($aItems, $sKey, $sValue) {
		$aTemp = array();
		foreach($aItems as $k=>$v) {
			$aTemp[$v[$sKey]] = $v[$sValue];
		}
		return $aTemp;
	}
}