Location: PHPKode > projects > ConCentric Event Planning Software > CC/system/application/controllers/concentric/roomSetups.php
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MODULE NAME   : roomSetups.php
 *
 * DESCRIPTION   : roomSetups module controller
 *
 * MODIFICATION HISTORY
 *   V1.0   2008-10-31 11:59 PM   - Leane Verhulst     - Created
 *
 * @package			ConCentric
 * @subpackage		roomSetups component Class
 * @author			Leane Verhulst
 * @copyright		Copyright (c) 2008
 * @license			http://www.gnu.org/licenses/gpl.html
 */

class RoomSetups extends Concentric_Controller
{
	/**
	* Contructor function
	*
	* Load the instance of CI by invoking the parent constructor
	*
	* @access      public
	* @return      none
	*/
	function RoomSetups()
	{
		// Call parent constructor
		parent::Concentric_Controller();

		$this->form_name = "RoomSetups";
		$this->load->model('roomsetups_model');    // Instantiate the model
	}

	// -------------------------------------------------------------------------
	/**
	* "Index" Page
	*
	* Default class action
	*
	* @access      public
	* @return      none
	*/
	function index()
	{
		// The default action is the browse action
		$this->browse();
	}

	// -------------------------------------------------------------------------
	// Function: browse()
	// Description: Extracts a list of all data records and
	//              displays it.
	function browse()
	{
		// fetch query object from the model
		$template['roomSetups_list'] = $this->roomsetups_model->fetchRoomSetups();

		// create urls for use in content view
		$template['base_url']   = 'concentric/roomSetups/browse/';
		$template['edit_url'] = 'concentric/roomSetups/edit/';
		$template['delete_url'] = 'concentric/roomSetups/delete/';
		$template['add_url']    = 'concentric/roomSetups/add/';
		$template['cancel_url'] = 'concentric/home';

		// set values for use in content view
		$template['form_attributes'] = array('name' => $this->form_name, 'id' => $this->form_name);
		$template['header'] = $this->form_name;

		// build content html using grid view and template data
		$data['content'] = $this->load->view('/concentric/roomSetups/roomSetups_grid', $template, TRUE);

		// set values for use in header view
		$data['header']  = $this->form_name;

		// pass all data to container view and display to user
		$this->load->view($this->_container,$data);
	}

	// -------------------------------------------------------------------------
	// Function: add()
	// Description: Prompts user for input and adds a new entry
	//              in the database.
	function add()
	{
		$this->_modify('add');
	}

	// -------------------------------------------------------------------------
	// Function: edit()
	// Description: Prompts user for input and updates an entry
	//              in the database.
	function edit()
	{
		$this->_modify('edit');
	}

	// -------------------------------------------------------------------------
	// Function: modify()
	// Description: Prompts user for input and adds/edits an entry
	//              in the database. Default to add mode.
	function _modify($mode = 'add')
	{
		// Initialize variable
		$other_scripts_data = '';

		// load validation library to help validate data
		$this->load->library('validation');

		// create urls and values for use in content view
		$template['form_url'] = 'concentric/roomSetups/' . $mode . '/';
		$template['cancel_url'] = 'concentric/roomSetups/browse';
		$template['form_attributes'] = array( 'method' => 'post', 'id' => $this->form_name, 'name' => $this->form_name );
		$template['header']  = $this->form_name;

		// create text to use the editor
		$editor_url = $this->config->item('base_url') . 'assets/tiny_mce/';
		$other_scripts_data .= $this->_tinymce_scripts($editor_url);


		$this->_initialize();   // Setup the fields
		$this->_validation();   // Load the validation rules and fields


		// if validation has not run or if it failed
		if ( $this->validation->run() == FALSE )
		{
			// Save validation errors to flashmsg
			// Will be output in content view by status->display
			$this->validation->output_errors();

			// Get values for form fields and build the form fields
			$status = $this->_get_form_values($mode);

			// If the returned values got an error
			if ($status === FALSE)
			{
				// Show error message
				flashMsg('error','There was a problem editing that record.');
				// redirect to main location
				redirect('/concentric/roomSetups/', 'location');
			}

			// Save field data to array to be passed to content view
			$template['fields'] = $this->fields;

			// build content html using details view and template data
			$data['content'] = $this->load->view('/concentric/details', $template, TRUE);
			// pass other scripts data to container
			$data['other_scripts'] = $other_scripts_data;

			// pass all data to container view and display to user
			$this->load->view($this->_container, $data);
		}
		else  // User is submitting valid data
		{
			// Store the values from the form into the db

			// XXS Filtering enforced for user input
			$data['field']['id']			= $this->input->post('id', TRUE);
			$data['field']['name']			= $this->input->post('name', TRUE);
			$data['field']['description']	= $this->input->post('description', TRUE);
			$data['field']['colorID']		= $this->input->post('colorID', TRUE);

			if ($mode == 'add')
			{
				// add the data to the table and return id
				$idField = $this->roomsetups_model->addRow('roomSetups',$data['field']);
				$status = ($idField) ? TRUE : FALSE;
			}
			else
			{
				$idField = $data['field']['id'];
				// update the data in the table
				$obj = $this->roomsetups_model->update('roomSetups', $data['field'], 'id = ' . $idField);
				$status = ($obj) ? TRUE : FALSE;
			}

			// if there was no error in add or edit
			if ($status)
			{
				// Show success message
				flashMsg('success','Record ' . $idField . ' (' . $data['field']['name'] . ') has been saved.');
			}
			else  // there was an error
			{
				// Show error message
				flashMsg('error','The record for (' . $data['field']['name'] . ') has not been saved.');
			}

			// redirect to main location
			redirect('/concentric/roomSetups/', 'location');
			// ?redirect to add/edit?
			// redirect('/concentric/roomSetups/' . $mode . '/', 'location');
		}
	}

	// -------------------------------------------------------------------------
	// Function: delete()
	// Description: Controller function to process user delete requests
	function delete()
	{
		$idField = $this->uri->segment(4);

		//$confirm = $this->_delete_confirm($idField);  //add later
		$confirm = TRUE;

		if ($confirm)
		{
			if ( $this->roomsetups_model->delete('roomSetups', 'id =' . $idField) )
			{
				flashMsg('success', 'The record has been deleted.');
			}
			else
			{
				flashMsg('error', 'There was a problem deleting the record');
			}
		}

		redirect('/concentric/roomSetups/', 'location');
	}

	// -------------------------------------------------------------------------
	function _delete_confirm($id)
	{
		//add later
	}

	// -------------------------------------------------------------------------
	function _get_form_values($mode = 'add')
	{
		if ($mode == 'add')
		{
			// Set empty values / default values
			$this->fields['id']['value'] = '';
			$this->fields['name']['value'] = '';
			$this->fields['description']['value'] = '';
			$this->fields['colorID']['value'] = '';
		}
		else
		{
			//get the id from either the hidden field or the url
			$idField = ($this->input->post('id', TRUE) != '') ? $this->input->post('id', TRUE) : $this->uri->segment(4);

			// fetch query object from the model with all fields
			$qry = $this->roomsetups_model->fetch('roomSetups','id, name, description, colorID','','id = ' . $idField);

			// There should only be 1 row. If not, return FALSE
			if ($qry->num_rows() != 1)
			{
				return FALSE;
			}

			// Get the single result as an array and save values
			$row = $qry->row_array();
			$this->fields['id']['value'] = $row['id'];
			$this->fields['name']['value'] = $row['name'];
			$this->fields['description']['value'] = $row['description'];
			$this->fields['colorID']['value'] = $row['colorID'];
			$this->fields['colorID']['selected'] = $row['colorID'];
		}

		// Now build the form fields
		foreach ($this->fields as $field)
		{
			$this->fields[$field['name']]['form'] = build_form_field($field);
		}

		return TRUE;
	}


	// -------------------------------------------------------------------------
	function _validation()
	{

		// load the rules
		$form_rules['name']  = $this->fields['name']['rules'];
		$form_rules['colorID']  = $this->fields['colorID']['rules'];
		$this->validation->set_rules($form_rules);

		// load the field labels
		$form_fields['id']   = $this->fields['id']['label'];
		$form_fields['name'] = $this->fields['name']['label'];
		$form_fields['description']   = $this->fields['description']['label'];
		$form_fields['colorID']   = $this->fields['colorID']['label'];
		$this->validation->set_fields($form_fields);

		// set error delimiters
		$this->validation->set_error_delimiters('<span class="error">', '</span>');

	}


	// -------------------------------------------------------------------------
	/**
	 * Initalize Values
	 * 
	 * @access private
	 * @return void;
	 */
	function _initialize()
	{
		// Setup custom field options - list fields in the order they should
		// appear in the details view
		//   rules - validation rules (optional)
		//   label - descriptive name of the field (optional)
		//   name - database field name ** MUST BE SAME AS KEY ** (required)
		//   desc - long description to show to user on form (optional)
		//   type - set of values (required)
		//            text - basic input box
		//            hidden - keep this field hidden
		//            textarea
		//            checkbox
		//            dropdown
		//            password
		//   class - class name (optional)
		//   script - html script code to insert on the field
		//   after_form_script - html script code to insert after the form has been displayed

		$this->fields['id'] = array('label' => 'RoomSetup ID',
							'name' => 'id',
							'type' => 'hidden');
		$this->fields['name'] = array('rules' => 'trim|required|max_length[50]|callback__unique_field[roomSetups.name]|xss_clean',
							'label' => 'RoomSetup Name',
							'name' => 'name',
							'desc' => 'Enter a name to describe this room setup.',
							'type' => 'text');
		$this->fields['colorID'] = array('rules' => 'trim|xss_clean',
							'label' => 'RoomSetup Color',
							'name' => 'colorID',
							'desc' => 'Enter the color that should be used on the grid report for room setups.',
							'type' => 'dropdown',
							'options' => $this->_get_color_array(),
							'selected' => '');
		$this->fields['description'] = array('label' => 'Description',
							'name' => 'description',
							'desc' => 'Enter a description of this room setup. Items that should be stated: style to setup chairs and number of chairs, number of tables needed, water service needed, etc. Be as detailed as possible.',
							'type' => 'textarea',
							'class' => 'mceEditor');

		return;
	}

	/**
	 * Unique_field
	 * ** Assumes that the db model has already been loaded. **
	 *
	 * @access    private
	 * @param     string  The string to check for in the db.
	 * @param     field   The table and column of the field to check in table.column format
	 * @return    bool
	 */
	function _unique_field($str, $field)
	{
		list($table, $column) = split("\.", $field, 2);

		// Set the error message to use
		$this->validation->set_message('_unique_field','The %s field must have a unique value in the database.');

		$where= isset($_POST['id']) ? array('id !='=> $_POST['id'], $column=>$str) : array($column=>$str);
		$count = $this->roomsetups_model->count_condition($table, $where);
		return ($count > 0) ? FALSE : TRUE;
	}

	/**
	 * Get color array
	 *
	 * @access    private
	 * @return    array
	 */
	 function _get_color_array()
	{
		// Load the color model
		$this->load->model('colors_model');

		// fetch query object from the model with fields id, name
		$query = $this->colors_model->fetch('colors','id, name','','','name ASC');

		// Loop through to build array
		$data = array();
		foreach ($query->result_array() as $row)
		{
			$data[$row['id']] = $row['name'];
		}

		return $data;
	}

	//return html for tiny_mce editor script info
	// @access private
	// @return string html
	function _tinymce_scripts($url = '')
	{
		$str = "\n";
		$str .= '<script type="text/javascript" src="' . $url . 'tiny_mce.js"></script>' . "\n";
		$str .= '<script type="text/javascript">' . "\n";
		$str .= 'tinyMCE.init({' . "\n";
		$str .= '	mode : "textareas",' . "\n";
		$str .= '	theme : "advanced",' . "\n";
		$str .= '	editor_selector : "mceEditor",' . "\n";
		$str .= '	editor_deselector : "mceNoEditor",' . "\n";
		$str .= '	theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,|,undo,redo,cleanup,help",' . "\n";
		$str .= '	theme_advanced_buttons2 : "fontselect,fontsizeselect,|,blockquote,hr,code",' . "\n";
		$str .= '	theme_advanced_buttons3 : "",' . "\n";
		$str .= '	theme_advanced_toolbar_location : "top",' . "\n";
		$str .= '	theme_advanced_toolbar_align : "left",' . "\n";
		$str .= '	theme_advanced_statusbar_location : "bottom",' . "\n";
		$str .= '	theme_advanced_resizing : true' . "\n";
		$str .= '});' . "\n";
		$str .= '</script>' . "\n" . "\n";

		return $str;
	}


	// --------------------------------------------------------------------------------------------
	// Reports
	// --------------------------------------------------------------------------------------------

	// -------------------------------------------------------------------------
	/**
	* Reports Page
	*
	* @access      public
	* @return      none
	*/
	function reports()
	{
		// create urls for use in content view
		$template['base_url']   = 'concentric/roomSetups/reports/';
		$template['cancel_url'] = 'concentric/home';

		// Set breadcrumb
		$this->page->set_crumb('RoomSetup Reports','concentric/roomSetups/reports/');

		// set values for use in content view
		$template['form_attributes'] = array('name' => $this->form_name, 'id' => $this->form_name);
		$template['header'] = $this->form_name;

		// build content html
		$data['content'] = $this->load->view('/concentric/roomSetups/roomSetups_reports_menu', $template, TRUE);

		// set values for use in header view
		$data['header']  = $this->form_name;

		// pass all data to container view and display to user
		$this->load->view($this->_container,$data);
	}

	// -------------------------------------------------------------------------
	/**
	* Report all data
	*
	* @access      public
	* @return      none
	*/
	function rep_all()
	{
		// fetch query object from the model
		$template['roomSetups_list'] = $this->roomsetups_model->fetchRoomSetups();

		// create urls for use in content view
		$template['base_url']   = 'concentric/roomSetups/reports/';
		$template['cancel_url'] = 'concentric/home';

		// Set breadcrumbs
		$this->page->set_crumb('RoomSetup Reports','concentric/roomSetups/reports/');
		$this->page->set_crumb('List All','concentric/roomSetups/rep_all/');

		// set values for use in content view
		$template['form_attributes'] = array('name' => $this->form_name, 'id' => $this->form_name);
		$template['header'] = $this->form_name;

		// build content html using grid view and template data
		$data['content'] = $this->load->view('/concentric/roomSetups/roomSetups_list_all', $template, TRUE);

		// set values for use in header view
		$data['header']  = $this->form_name;

		// pass all data to container view and display to user
		$this->load->view($this->_container,$data);
	}

	// -------------------------------------------------------------------------
	/**
	* Download all data
	*
	* @access      public
	* @return      none
	*/
	function rep_download_all($eor = '')
	{
		// Load the helper
		$this->load->helper('download');

		// fetch query object from the model
		$query = $this->roomsetups_model->fetchRoomSetups();

		// Initialize values
		$data = '';
		$eor = ($eor == '') ? '' : (',' . $eor);
		// Loop through the data and build the data
		foreach ($query->result_array() as $row)
		{
			$data .= '"' . $row['id'] . '","' . $row['name'] . '","' . $row['description'] . '","' . $row['color_name'] . '"' . $eor . "\n";
		}

		// Download the file
		force_download('roomSetups.csv', $data);

		redirect('/concentric/roomSetups/reports', 'location');
	}

}


/* End of file roomSetups.php */
/* Location: ./system/application/controllers/concentric/roomSetups.php */
Return current item: ConCentric Event Planning Software