This post provides some sample source code segment, extracted from our previously developed systems. The intention here is to depict the coding practices we apply within iBCScorp.
Samples given below are based on CakePHP MVC framework. iBCScorp has involved in CakePHP development since the version 1.1. After that we have continued with 1.2.x releases and then absorbed 1.3.x releases into our developments later on.
iBCScorp started using CakePHP 1.3 in projects since its pre-stable releases. Adopting the new version early helped iBCScorp to keep its source code clean and tight with new syntactical improvements, while avoiding version upgrades in near future.
FreedomTravel4You.com is one of the sites built on CakePHP 1.3. Following code segment is extracted from its codebase. (agent_tree_controller.php).
[php]
<?php
/**
* Short description for file
* This file handles the Agent Tree View functionality
*
* PHP versions > 5.0, CakePHP versions > 1.3
*
* @package FreedomTravel4You.com
* @author iBCScorp.
* @copyright iBCScorp.
* @license As described below
* @version 1.0.0
* @since File available since Release 1.1.0 dt. Friday, 04 December, 2009
*/
/*********************************************************
* Licence:
* This file is sole property of the installer.
* Any type of copy or reproduction without the consent
* of owner is prohibited.
* If in any case used leave this part intact without
* any modification.
* All Rights Reserved
* Copyright 2009 Owner
*******************************************************/
class AgentTreeController extends AppController {
var $name = ‘AgentTree’;
var $uses = array(‘User’, ‘UserTree’);
var $components = array(‘Auth’);
var $helpers = array(‘Html’, ‘Javascript’, ‘TreeView’);
function beforeFilter() {
if($this->Session->read(‘user_info.admin’)){
$this->set(‘menu_type’, ‘admin’);
} else {
$this->set(‘menu_type’, ‘agent’);
}
$this->set(‘show_side_bar’, 0);
}
function index($element_id = null) {
$this->common_tree_info($element_id);
$this->set(‘page_title’, ‘Agent’);
}
// Action to display unassigned users for logged in agent
function assign_agent() {
$curr_user_id = $this->Session->read(‘Auth.User.id’);
$unassigned_childrens = $this->User->get_unassigned_children($curr_user_id);
$this->set(‘unassigned_childrens’, $unassigned_childrens);
$this->set(‘page_title’, ‘Agent’);
}
// Display tree view to assign new agents to tree
function add_to_tree($element_id = null) {
$new_child_id;
if(isset($this->params['form']['child_id'])){
$new_child_id = $this->params['form']['child_id'];
// Store selected child-id to be assigned to tree
$this->Session->write(‘child_to_be_assigned’, $new_child_id);
} elseif($this->Session->check(‘child_to_be_assigned’)) {
$new_child_id = $this->Session->read(‘child_to_be_assigned’);
} else {
$this->redirect(‘/agent_tree/assign_agent’);
exit(0);
}
// get the child name for particular id
$new_child_info = $this->User->find(‘first’, array(
‘conditions’ => array(‘id’ => $new_child_id),
‘fields’ => array(‘first_name’)
));
// Call the common function to initialize tree
$this->common_tree_info($element_id);
$this->set(‘new_agent_name’, $new_child_info['User']['first_name']);
$this->set(‘page_title’, ‘Agent’);
}
// Funtion to apend child to tree (last step)
function append_child() {
$child = explode("_", trim($this->params['form']['child_id']));
$tree_data = array();
$tree_data['UserTree']['parent_id'] = $child[0];
$tree_data['UserTree']['child_position'] = $child[1];
$tree_data['UserTree']['child_id'] = $child[2]; // Tree ID of the new Agent
$this->UserTree->create();
$this->UserTree->save($tree_data);
$new_user_id = $this->Session->read(‘child_to_be_assigned’);
$this->User->id = $new_user_id;
$this->User->saveField(‘tree_id’, $child[2]);
$this->redirect(‘/agent_tree/index’);
exit(0);
}
// Common function to initialize tree view.
private function common_tree_info($element_id = null) {
// Get user tree ID
$curr_user_id = $this->Session->read(‘Auth.User.id’);
$curr_user_info = $this->User->find(‘first’, array(
‘conditions’ => array(‘id’=> $curr_user_id),
‘fields’ =>
array(‘first_name’, ‘tree_id’, ‘parent_available’)
));
// If this user is not yet appended to the tree disable this functionality
if($curr_user_info['User']['parent_available'] == 0) {
$this->redirect(‘/agent_tree/user_not_assigned’);
exit(0);
}
$tree_id = ”; // tree id of the current root node
// Get the user info for root element i.e. logged in or requested
if(isset($element_id)){
$tree_id = $element_id;
} else {
$tree_id = $curr_user_info['User']['tree_id'];
}
$root_info = $this->User->get_root_node_info($tree_id);
//reference_id of the current user or requested root element
$this->set(‘root_reference_id’, $root_info['u']['reference_id']);
// first name of the current root element
$root_name = $root_info['u']['first_name'] . ‘ ‘ . $root_info['u']['last_name'];
$this->set(‘root_name’, $root_name);
//id of the current root node
$this->set(‘root_id’, $root_info['u']['id']);
//logged in user’s id
$this->set(‘user_id’, $curr_user_id);
//tree_id of the current user or requested root element
$this->set(‘root_node’, $root_info['u']['tree_id']);
// Used to display move up link
$this->set(‘up_level_id’, $root_info['t']['parent_id']);
}
// Action to display on tree view, when logged in user is not assigned to the tree
function user_not_assigned() {
$this->set(‘page_title’, ‘Agent’);
}
function search_agent() {
$this->layout = ‘ajax’;
Configure::write(‘debug’, 0);
$search_value = trim($this->params['named']['q']);
// Check the type of search criteria
$search_type = ”;
// Check whether it is an email
if(stripos($search_value, ‘@’) !== FALSE) {
$search_type = ‘TYPE_EMAIL’;
} elseif (!is_numeric(substr($search_value, 0 , 2)) &&
is_numeric(substr($search_value, 2 , 6))){ // Check for reference number
$search_type = ‘TYPE_REF’;
} else { // Value is a name
$search_type = ‘TYPE_NAME’;
}
$conditions = array();
// Check whether email or reference # to be searched
if($search_type == ‘TYPE_REF’) { // ref #
$conditions['and'] = array(
‘reference_id’ => $search_value,
‘tree_id not’ => ‘NULL’
);
} elseif($search_type == ‘TYPE_EMAIL’) { // email
$conditions['and'] = array(
‘email’ => $search_value,
‘tree_id not’ => ‘NULL’
);
} elseif($search_type == ‘TYPE_NAME’) {
$conditions['and'] = array(
‘or’ => array(‘first_name like’ => ‘%’ . $search_value . ‘%’,
‘last_name like’ => ‘%’ . $search_value . ‘%’
),
‘tree_id not’ => ‘NULL’
);
}
$result = $this->User->find(‘all’, array(
‘fields’ => array(‘reference_id’, ‘first_name’,
‘last_name’, ‘email’, ‘tree_id’),
‘conditions’ => $conditions
));
$output = "";
if(sizeof($result)) {
foreach($result as $r) {
$output .= $r['User']['tree_id'] . "|" .
$r['User']['first_name'] . " " . $r['User']['last_name'] . "|" .
$r['User']['email'] . "|" .
$r['User']['reference_id'];
$output .= ‘^’;
}
$output = rtrim($output, ‘^’); // Removing the trailing ^ character at the end of output text
} else {
$output = "NO";
}
$this->set(‘search_result’, $output);
}
}
?>
[/php]