⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

alternative coding style -- a couple of examples refactored



sfthurber
  • profile picture
  • Member

Posted 27 January 2017 - 20:15 PM

thought i would post examples.php with employees_management() re-factored to a coding style that reduces repetition:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* class assumes the following autoloader config settings:
$autoload['libraries'] = array('database', 'grocery_CRUD');
$autoload['helper'] = array('security', 'url');
*/

class Examples extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function _example_output($output = null)
	{
		$this->load->view('example.php',$output);
	}

	public function employees_management()
	{
		$this->_example_output(
			$this->grocery_crud
				->set_table('employees')
				->set_relation('officeCode','offices','city')
				->display_as('officeCode','Office City')
				->set_subject('Employee')
				->required_fields('lastName')
				->set_field_upload('file_url','assets/uploads/files')
				->render()
		);
	}

sfthurber
  • profile picture
  • Member

Posted 31 January 2017 - 13:29 PM

the refactored example is now on github

 

 

https://github.com/sfthurber/grocery_crud_example_refactored

 




sfthurber
  • profile picture
  • Member

Posted 02 February 2017 - 14:09 PM

i've posted an update on github that shows how to include in the edit for customers a link to a crud that lists the orders for that customer only. like so:


	public function customers_management()
	{
		$this->the_output(
			$this->grocery_crud ->set_table('customers')
				->unset_export()
				->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit')
				->display_as('salesRepEmployeeNumber','Sales Employee')
				->display_as('customerName','Name')
				->display_as('contactLastName','Last Name')
				->callback_edit_field('customerName',array($this,'_edit_field_callback_customers'))
				->set_subject(' ')
				->set_relation('salesRepEmployeeNumber','employees','lastName')
				->render()
		);
	}
		function _edit_field_callback_customers($value, $primary_key)
		{
			$this->session->set_userdata('customerID', $primary_key); // stateful
			return '<input type="text" maxlength="50" value="'.$value.'" name="customerName"> &nbsp; &nbsp;
					<a href="'.site_url().'example/cust_orders_management">Click Here to View Customer Orders</a>'; 
		}


	
	public function cust_orders_management()
	{
		if($this->session->userdata('customerID')===0){ die('invalid request'); } // stateful
		$this->the_output(
			$this->grocery_crud	->set_table('orders') 
				->where('orders.customerNumber', $this->session->userdata('customerID'))
				->set_relation('customerNumber','customers','customerName')
				->display_as('customerNumber','Customer')
				->unset_export()
				->set_subject(' ')
				->unset_add()
				->unset_delete()
				->render()
		);
	}

sfthurber
  • profile picture
  • Member

Posted 15 September 2018 - 15:11 PM

the refactored examples are now here:

 

https://github.com/sfthurber/cim-xa/tree/master/variousGroceryCrudRefactorsCI3