[PC-BSD Commits] r1731 - in websites/pbidir.com/bibivu: bin slib tpl/pbidir/admin

svn at pcbsd.org svn at pcbsd.org
Tue Apr 15 19:37:46 PDT 2008


Author: fabry
Date: 2008-04-15 19:37:45 -0700 (Tue, 15 Apr 2008)
New Revision: 1731

Modified:
   websites/pbidir.com/bibivu/bin/admin.php
   websites/pbidir.com/bibivu/slib/table_prototype.php
   websites/pbidir.com/bibivu/tpl/pbidir/admin/form.php
   websites/pbidir.com/bibivu/tpl/pbidir/admin/view.php
Log:
Created ability to Clone records.

Modified: websites/pbidir.com/bibivu/bin/admin.php
===================================================================
--- websites/pbidir.com/bibivu/bin/admin.php	2008-04-15 16:55:10 UTC (rev 1730)
+++ websites/pbidir.com/bibivu/bin/admin.php	2008-04-16 02:37:45 UTC (rev 1731)
@@ -69,7 +69,7 @@
 		$this->display->assign('save_bulk_uri',$this->uri->create_uri(array('save'=>'bulk', 'ajax'=>'')));
 		$this->display->assign('delete_uri',$this->uri->create_uri(array('delete'=>'', 'ajax'=>'')));
 		$no_access = false;
-		if(!is_null($this->uri->get('add',NULL)) || !is_null($this->uri->get('edit',NULL))){
+		if(!is_null($this->uri->get('clone',NULL)) || !is_null($this->uri->get('add',NULL)) || !is_null($this->uri->get('edit',NULL))){
 			$this->uri->set('id', (int)$this->uri->get('id'));
 			$filters = array();
 			$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$this->uri->get('id'));
@@ -79,8 +79,12 @@
 			$this->display->assign('can_write',$this->_check_can($what, 'write', $owner, $records[0]));
 			$this->display->assign('can_delete',$this->_check_can($what, 'delete', $owner, $records[0]));
 			if($this->display->get('can_read')){
-				$this->display->assign('record',isset($records[0])?$records[0]:array());
-				if(!is_null($this->uri->get('add',NULL))){
+				if(!is_null($this->uri->get('clone',NULL))){
+					$this->uri->set('id',0);
+					unset($records[0]['id']);
+				}
+				$this->display->assign('record',$records[0]);
+				if(!is_null($this->uri->get('add',NULL)) || !is_null($this->uri->get('clone',NULL))){
 					$get_forced = array();
 					$get = $this->uri->get();
 					$str = '^('.implode('|', array_keys($this->display->get('fields'))).')$';
@@ -90,7 +94,13 @@
 						}
 					}
 					$this->display->assign('get_forced',$get_forced);
-					$this->display->assign('title_form','Add '.$what);
+					if(!is_null($this->uri->get('clone',NULL))){
+						//show a form already filled up
+						$this->display->assign('title_form','Edit Clone of '.$what);
+						$this->uri->get('add','');
+					} else {
+						$this->display->assign('title_form','Add '.$what);
+					}
 				} elseif(!is_null($this->uri->get('edit',NULL))) {
 					$this->display->assign('title_form','Edit '.$what.' '.$this->uri->get('id'));
 				}

Modified: websites/pbidir.com/bibivu/slib/table_prototype.php
===================================================================
--- websites/pbidir.com/bibivu/slib/table_prototype.php	2008-04-15 16:55:10 UTC (rev 1730)
+++ websites/pbidir.com/bibivu/slib/table_prototype.php	2008-04-16 02:37:45 UTC (rev 1731)
@@ -1,479 +1,479 @@
-<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
-// ------------------------------------------------------------------------
-
-class table_prototype{
-	protected $current = array();
-	protected $dodb = array();
-	protected $output = array();
-	protected $last_result;
-	protected $fields = array();
-	protected $related = array();
-	protected $bulk_actions = array();
-	public $db;
-	
-//	public function init(){
-//		parent::init();
-//	}
-	
-	public function __clone(){
-		$this->_reset_dodb();
-	}
-	
-	public function __construct(){
-//		parent::__construct();
-		$this->db =& load_library('db');
-		$this->_reset_dodb();
-		$this->dodb['table'] = '';
-	}
-	
-	private function _reset_dodb(){
-		$this->dodb['what']		= '';
-		$this->dodb['prepare']	= '';
-		$this->dodb['using']	= '';
-		$this->dodb['fields']	= array();
-		$this->dodb['set']		= array();
-		$this->dodb['join']		= array();
-//		$this->dodb['table']	= get_class($this);
-		$this->dodb['ttable']	= '';
-		$this->dodb['where']	= array();
-		$this->dodb['group']	= array();
-		$this->dodb['order']	= array();
-		$this->dodb['limit']	= '';
-	}
-	public function last_result(){
-		return $this->last_result;
-	}
-	
-	public function set_table_name($table){
-		$this->dodb['table'] = $table;
-		return $this;
-	}
-	
-	public function execute($prepare, $using){
-		$this->dodb['what'] = 'EXECUTE';
-		$this->dodb['prepare'] = $prepare;
-		$this->dodb['using'] = $using;
-		return $this;
-	}
-	
-	public function prepare($prepare){
-		$this->dodb['prepare'] = $prepare;
-		return $this;
-	}
-	public function insert(){
-		$this->dodb['what'] = 'INSERT';
-		return $this;
-	}
-	
-	public function replace(){
-		$this->dodb['what'] = 'REPLACE';
-		return $this;
-	}
-	
-	public function select(){
-		$this->dodb['what'] = 'SELECT';
-		return $this;
-	}
-	
-	public function update(){
-		$this->dodb['what'] = 'UPDATE';
-		return $this;
-	}
-
-	public function delete(){
-		$this->dodb['what'] = 'DELETE';
-		return $this;
-	}
-	
-	public function from($table){
-		$this->dodb['ttable'] = $table;
-		return $this;
-	}
-	
-	public function qjoin($join){
-		if(isset($join['table']) && isset($join['how'])){
-			if(!isset($join['direction'])) $join['direction'] = 'left';
-			$this->dodb['join'][] = array('table'=>$join['table'], 'how'=>$join['how'], 'direction'=>$join['direction']);
-		}
-		return $this;
-	}
-	
-	public function field($field){
-		return $this->fields($field);
-	}
-
-	public function fields($fields=array()){
-		if(is_array($fields)){
-			$this->dodb['fields'] = $fields;
-		} else {
-			$this->dodb['fields'][] = $fields;
-		}
-		return $this;
-	}
-
-	public function set($field, $value, $SQL_value=false){
-		//use $SQL_value=true if this is a SQL function
-		$this->dodb['set'][$field] = array($value,$SQL_value);
-		return $this;
-	}
-
-	public function where($where=array()){
-		if(isset($where['field'])){
-			$this->dodb['where'][] = $where;
-		} else {
-			$this->dodb['where'] = $where;
-		}
-		return $this;
-	}
-	
-	public function group($group=array()){
-		if(is_array($group)){
-			if(!empty($group) && $group!=array('')){
-				$this->dodb['group'] = $group;
-			}
-		} elseif($group!='') {
-			$this->dodb['group'][] = $group;
-		}
-		return $this;
-	}
-
-	public function order($order=array()){
-		if(is_array($order)){
-			if(!empty($order) && $order!=array('')){
-				$this->dodb['order'] = $order;
-			}
-		} elseif($order!='') {
-			$this->dodb['order'][] = $order;
-		}
-		return $this;
-	}
-	
-	public function limit($limit=''){
-		if($limit==''){
-			$this->dodb['limit'] = '';
-		} else {
-			$this->dodb['limit'] = ' LIMIT '.$limit;
-		}
-		return $this;
-	}
-	
-	public function do_db(){
-		$qry = '';
-		if($this->dodb['what']=='EXECUTE'){
-			$qry .= 'EXECUTE '.$this->dodb['prepare'].' USING ';
-			if(is_array($this->dodb['using'])){
-				$qry .= implode(',',$this->dodb['using']);
-			} else {
-				$qry .= $this->dodb['using'];
-			}
-			$qry .= ';';
-		} else {
-			if($this->dodb['prepare']!=''){
-				$qry .= 'PREPARE '.$this->dodb['prepare'].' FROM ';
-			}
-			$qry .= $this->dodb['what'];
-			if($this->dodb['what'] =='INSERT' || $this->dodb['what'] =='REPLACE'){
-				$qry .= ' INTO ';
-			}elseif($this->dodb['what'] =='SELECT'){
-				if(is_array($this->dodb['fields']) && !empty($this->dodb['fields'])) {
-					$qry .= ' '.implode(', ',$this->dodb['fields']);
-				} else {
-					$qry .= ' * ';
-				}
-				$qry .= ' FROM ';
-			}elseif($this->dodb['what'] =='DELETE'){
-				$qry .= ' FROM ';
-			}
-			if($this->dodb['ttable']!=''){
-				$qry .= ' `'.$this->dodb['ttable'].'` ';
-			} else {
-				$qry .= ' `'.$this->dodb['table'].'` ';
-			}
-			if($this->dodb['join'] != array()){
-				if($this->dodb['what'] =='SELECT' || $this->dodb['what'] =='DELETE'){
-					foreach($this->dodb['join'] as $join){
-						switch($join['direction']){
-							case 'right':
-							case 'left':
-							case 'inner':
-								$qry .= ' '.strtoupper($join['direction']);
-								break;
-							default:
-								$qry .= ' LEFT';
-						}
-						$qry .= ' JOIN '.$join['table'].' ON '.$join['how'];
-					}
-				}
-			}
-			if($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'INSERT' || $this->dodb['what'] =='REPLACE'){
-				$aqry = array();
-				foreach($this->dodb['set'] as $field=>$value){
-					if($value[1] || ((string)(float)$value[0] === (string)$value[0])){	//MySQL function or number
-						$valueDb = $value[0];
-					} else {
-						$valueDb = '"'.$this->db->real_escape_string($value[0]).'"';
-					}
-					$aqry[] = '`'.$field.'` = '.$valueDb.' ';
-					$this->current[$field] = $value;
-				}
-				$qry .= ' SET '.implode(', ',$aqry);
-			}
-			if($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'SELECT' || $this->dodb['what'] == 'DELETE'){
-				if($this->dodb['where'] == array() && ($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'DELETE')){
-					$this->where(array('field'=>'id', 'operator'=>'=', 'value'=>$this->get('id')));
-				}
-	
-				if($this->dodb['where'] != array()){
-					$qry .= ' WHERE '.$this->_process_where($this->dodb['where']);
-				}
-			}
-			if($this->dodb['what'] != 'INSERT' || $this->dodb['what'] !='REPLACE'){
-				if(isset($this->dodb['group']) && is_array($this->dodb['group']) && !empty($this->dodb['group'])) {
-					$qry .= ' GROUP BY '.implode(', ',$this->dodb['group']);
-				}
-				if(isset($this->dodb['order']) && is_array($this->dodb['order']) && !empty($this->dodb['order'])) {
-					$qry .= ' ORDER BY '.implode(', ',$this->dodb['order']);
-				}
-			}
-			if(isset($this->dodb['limit'])){
-				$qry .= $this->dodb['limit'].';';
-			}
-		}
-		//echo $qry.'<br />'."\n";
-		//mail('fabrizio at bibivu.com','qry',$qry);
-		$this->last_result = $this->db->query($qry);
-		if($this->dodb['what'] =='INSERT' || $this->dodb['what'] =='REPLACE'){
-			$this->select($this->db->insert_id());
-		}
-		$this->_reset_dodb();
-		return $this;
-	}
-
-	private function _get_info($id, $from='', $prepare = false){
-		$this->current = $this->select()->from($from)->fields()->where(array(array('field'=>'id', 'operator'=>'=', 'value'=>$id)))->do_db()->db->fetch_array($this->last_result);
-		return $this;
-	}
-	
-	public function get_record($how=NULL, $from=''){
-		if(is_array($how)){
-			$this->current = $this->select()->from($from)->fields()->where($how)->do_db()->db->fetch_array($this->last_result);
-		}elseif(is_numeric($how) || $from!=''){
-			$this->_get_info($how, $from);
-		} elseif(!isset($this->current['id']) || $this->current['id']<=0){
-						
-		}
-		return $this;
-	}
-
-	public function get($field='', $default=NULL){
-		if($field=='' || is_array($field)){
-			return $this->get_info();
-		}
-		return isset($this->current[$field])?$this->current[$field]:$default;
-	}
-
-	public function get_info(){
-		return $this->current?$this->current:array();
-	}
-	
-	/*
-	* this function returns every records
-	* and pass it throu the get_info function
-	*/
-	public function get_all($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
-		$records = array();
-		$this->select()->fields(array_merge(array('`'.($from!=''?$from:$this->dodb['table']).'`.`id`'), $xtrfields))->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
-		$tmps = $this->last_result;
-		while($record = $this->db->fetch_array($tmps)){
-			$records[] = $this->get_record($record['id'], $from)->get_info();
-		}
-		return $records;
-	}
-
-	/*
-	* this function returns every records
-	* and pass it throu the get_info function
-	*/
-	public function get_count($where=array(), $group=array(), $from='', $join = array()){
-		$records = array();
-		$this->select()->fields('COUNT(*) tot')->from($from)->qjoin($join)->where($where)->group($group)->limit(1)->do_db();
-		$tmps = $this->last_result;
-		$record = $this->db->fetch_array($tmps);
-		return $record['tot']+0;
-	}
-	
-	/*
-	* this function returns ONLY what is in the records
-	* without pass the records from get_info
-	*/
-	public function get_raw($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
-		$records = array();
-		$this->select()->fields($xtrfields)->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
-		$tmps = $this->last_result;
-		while($record = $this->db->fetch_array($tmps)){
-			$records[] = $record;
-		}
-		return $records;
-	}
-	
-	/*
-	* this function returns ONLY the ID field
-	*/
-	public function get_ids($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
-		$records = array();
-		$this->select()->fields(array_merge($xtrfields,array('`'.($from!=''?$from:$this->dodb['table']).'`.`id`')))->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
-		$tmps = $this->last_result;
-		while($record = $this->db->fetch_array($tmps)){
-			$records[] = $record;
-		}
-		return $records;
-	}
-	
-	private function _process_where($where){
-		static $recur = 0;
-		$recur++;
-		$appends = array();
-		if(!is_array($where)) return 1;
-		foreach ($where as $fil) {
-			if(count($fil)!=3 || !isset($fil['field'])){
-				$appends[] = $this->_process_where($fil);
-			} else {
-				$appends[] = $this->_process_where_single($fil);
-			}
-		}
-		$append = '('.implode($recur==1?' AND ':' OR ', $appends).')';
-		$recur--;
-		return $append;
-	}
-	
-	private function _process_where_single($where){
-		if(!isset($where['field']) || !isset($where['operator']) || !isset($where['value'])){
-			return '';
-		}
-		$append		= '';
-		$field		= $where['field'];
-		$operator	= strtoupper($where['operator']);
-		$value		= $where['value'];
-
-		switch($operator){
-			case 'IN':
-				$value = '('.$where['value'].')';
-				break;
-			case 'IS':
-			case 'IS NOT':
-				$value = $where['value'];
-				break;
-			//all of those are falling into the default
-/*
-			case 'REGEXP':
-			case 'LIKE':
-			case '=':
-			case '!=':
-			case '>':
-			case '<':
-			case '=>':
-			case '=<':
-*/
-			default:
-				$value = '"'.$this->db->real_escape_string($where['value']).'"';
-		}
-		return $field.' '.$operator.' '.$value;
-	}
-
-	/*********************************************
-	 *  MANAGEMENT CONNECTION AND DISPLAY PART
-	 ********************************************/
-	public function get_related_tables(){
-		$this->related=array();
-		return $this->related;
-	}
-
-	public function get_bulk_actions(){
-		$this->bulk_actions=array();
-		return $this->bulk_actions;
-	}
-
-	public function get_fields(){
-		$this->fields=array();
-		return $this->fields;
-	}
-
-	public function add($tt_post){
-		if(is_array($tt_post)){
-			$filters = array();
-			$fields = $this->get_fields();
-			foreach($fields as $key=>$value){
-				if($value['post']==true && (isset($tt_post[$key]) || !is_null($value['default']))){
-					if(isset($tt_post[$key])){
-						$$key = $tt_post[$key];
-					} else {
-						$$key = $value['default'];
-					}
-					$this->set($key,$$key);
-				}
-			}
-			$ret = $this->insert()->do_db();
-		} else {
-			$ret = false;
-		}
-		return $ret;
-	}
-	
-	public function edit($id, $tt_post){
-		if($id>0 && is_array($tt_post)){
-			$filters = array();
-			$fields = $this->get_fields();
-			foreach($fields as $key=>$value){
-				if($value['post']==true && (isset($tt_post[$key]) || !is_null($value['default']))){
-					if(isset($tt_post[$key])){
-						$this->set($key,$tt_post[$key]);
-					}
-				}
-			}
-			$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
-			$ret = $this->update()->where($filters)->do_db();
-		} else {
-			$ret = false;
-		}
-		return $ret;
-	}
-	
-	public function edit_bulk($tt_post){
-		$ret = false;
-		if(is_array($tt_post)){
-			$fields = $this->get_fields();
-			foreach($tt_post as $key=>$value){
-				if(isset($fields[$key]) && is_array($value) && $fields[$key]['post']==true){
-					foreach($value as $id=>$vv){
-						$filters = array(array('field'=>'id', 'operator'=>'=', 'value'=>$id));
-						$ret = $this->update()->set($key,$vv)->where($filters)->do_db();
-					}
-				}
-			}
-		} else {
-			$ret = false;
-		}
-		return $ret;
-	}
-	
-	public function remove($id){
-		$filters = array();
-		$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
-		return  $this->delete()->where($filters)->do_db();
-	}
-
-	public function change($field, $value, $id=NULL, $from=''){
-		if(!is_numeric($id)){
-			$id = $this->get('id',0);
-		}
-		if($id>0 && trim($field)!=''){
-			$filters = array();
-			$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
-			$ret = $this->update()->from($from)->set($field,$value)->where($filters)->limit(1)->do_db();
-		} else {
-			$ret = false;
-		}
-		return $ret;
-	}
-}
+<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
+// ------------------------------------------------------------------------
+
+class table_prototype{
+	protected $current = array();
+	protected $dodb = array();
+	protected $output = array();
+	protected $last_result;
+	protected $fields = array();
+	protected $related = array();
+	protected $bulk_actions = array();
+	public $db;
+	
+//	public function init(){
+//		parent::init();
+//	}
+	
+	public function __clone(){
+		$this->_reset_dodb();
+	}
+	
+	public function __construct(){
+//		parent::__construct();
+		$this->db =& load_library('db');
+		$this->_reset_dodb();
+		$this->dodb['table'] = '';
+	}
+	
+	private function _reset_dodb(){
+		$this->dodb['what']		= '';
+		$this->dodb['prepare']	= '';
+		$this->dodb['using']	= '';
+		$this->dodb['fields']	= array();
+		$this->dodb['set']		= array();
+		$this->dodb['join']		= array();
+//		$this->dodb['table']	= get_class($this);
+		$this->dodb['ttable']	= '';
+		$this->dodb['where']	= array();
+		$this->dodb['group']	= array();
+		$this->dodb['order']	= array();
+		$this->dodb['limit']	= '';
+	}
+	public function last_result(){
+		return $this->last_result;
+	}
+	
+	public function set_table_name($table){
+		$this->dodb['table'] = $table;
+		return $this;
+	}
+	
+	public function execute($prepare, $using){
+		$this->dodb['what'] = 'EXECUTE';
+		$this->dodb['prepare'] = $prepare;
+		$this->dodb['using'] = $using;
+		return $this;
+	}
+	
+	public function prepare($prepare){
+		$this->dodb['prepare'] = $prepare;
+		return $this;
+	}
+	public function insert(){
+		$this->dodb['what'] = 'INSERT';
+		return $this;
+	}
+	
+	public function replace(){
+		$this->dodb['what'] = 'REPLACE';
+		return $this;
+	}
+	
+	public function select(){
+		$this->dodb['what'] = 'SELECT';
+		return $this;
+	}
+	
+	public function update(){
+		$this->dodb['what'] = 'UPDATE';
+		return $this;
+	}
+
+	public function delete(){
+		$this->dodb['what'] = 'DELETE';
+		return $this;
+	}
+	
+	public function from($table){
+		$this->dodb['ttable'] = $table;
+		return $this;
+	}
+	
+	public function qjoin($join){
+		if(isset($join['table']) && isset($join['how'])){
+			if(!isset($join['direction'])) $join['direction'] = 'left';
+			$this->dodb['join'][] = array('table'=>$join['table'], 'how'=>$join['how'], 'direction'=>$join['direction']);
+		}
+		return $this;
+	}
+	
+	public function field($field){
+		return $this->fields($field);
+	}
+
+	public function fields($fields=array()){
+		if(is_array($fields)){
+			$this->dodb['fields'] = $fields;
+		} else {
+			$this->dodb['fields'][] = $fields;
+		}
+		return $this;
+	}
+
+	public function set($field, $value, $SQL_value=false){
+		//use $SQL_value=true if this is a SQL function
+		$this->dodb['set'][$field] = array($value,$SQL_value);
+		return $this;
+	}
+
+	public function where($where=array()){
+		if(isset($where['field'])){
+			$this->dodb['where'][] = $where;
+		} else {
+			$this->dodb['where'] = $where;
+		}
+		return $this;
+	}
+	
+	public function group($group=array()){
+		if(is_array($group)){
+			if(!empty($group) && $group!=array('')){
+				$this->dodb['group'] = $group;
+			}
+		} elseif($group!='') {
+			$this->dodb['group'][] = $group;
+		}
+		return $this;
+	}
+
+	public function order($order=array()){
+		if(is_array($order)){
+			if(!empty($order) && $order!=array('')){
+				$this->dodb['order'] = $order;
+			}
+		} elseif($order!='') {
+			$this->dodb['order'][] = $order;
+		}
+		return $this;
+	}
+	
+	public function limit($limit=''){
+		if($limit==''){
+			$this->dodb['limit'] = '';
+		} else {
+			$this->dodb['limit'] = ' LIMIT '.$limit;
+		}
+		return $this;
+	}
+	
+	public function do_db(){
+		$qry = '';
+		if($this->dodb['what']=='EXECUTE'){
+			$qry .= 'EXECUTE '.$this->dodb['prepare'].' USING ';
+			if(is_array($this->dodb['using'])){
+				$qry .= implode(',',$this->dodb['using']);
+			} else {
+				$qry .= $this->dodb['using'];
+			}
+			$qry .= ';';
+		} else {
+			if($this->dodb['prepare']!=''){
+				$qry .= 'PREPARE '.$this->dodb['prepare'].' FROM ';
+			}
+			$qry .= $this->dodb['what'];
+			if($this->dodb['what'] =='INSERT' || $this->dodb['what'] =='REPLACE'){
+				$qry .= ' INTO ';
+			}elseif($this->dodb['what'] =='SELECT'){
+				if(is_array($this->dodb['fields']) && !empty($this->dodb['fields'])) {
+					$qry .= ' '.implode(', ',$this->dodb['fields']);
+				} else {
+					$qry .= ' * ';
+				}
+				$qry .= ' FROM ';
+			}elseif($this->dodb['what'] =='DELETE'){
+				$qry .= ' FROM ';
+			}
+			if($this->dodb['ttable']!=''){
+				$qry .= ' `'.$this->dodb['ttable'].'` ';
+			} else {
+				$qry .= ' `'.$this->dodb['table'].'` ';
+			}
+			if($this->dodb['join'] != array()){
+				if($this->dodb['what'] =='SELECT' || $this->dodb['what'] =='DELETE'){
+					foreach($this->dodb['join'] as $join){
+						switch($join['direction']){
+							case 'right':
+							case 'left':
+							case 'inner':
+								$qry .= ' '.strtoupper($join['direction']);
+								break;
+							default:
+								$qry .= ' LEFT';
+						}
+						$qry .= ' JOIN '.$join['table'].' ON '.$join['how'];
+					}
+				}
+			}
+			if($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'INSERT' || $this->dodb['what'] =='REPLACE'){
+				$aqry = array();
+				foreach($this->dodb['set'] as $field=>$value){
+					if($value[1] || ((string)(float)$value[0] === (string)$value[0])){	//MySQL function or number
+						$valueDb = $value[0];
+					} else {
+						$valueDb = '"'.$this->db->real_escape_string($value[0]).'"';
+					}
+					$aqry[] = '`'.$field.'` = '.$valueDb.' ';
+					$this->current[$field] = $value;
+				}
+				$qry .= ' SET '.implode(', ',$aqry);
+			}
+			if($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'SELECT' || $this->dodb['what'] == 'DELETE'){
+				if($this->dodb['where'] == array() && ($this->dodb['what'] == 'UPDATE' || $this->dodb['what'] == 'DELETE')){
+					$this->where(array('field'=>'id', 'operator'=>'=', 'value'=>$this->get('id')));
+				}
+	
+				if($this->dodb['where'] != array()){
+					$qry .= ' WHERE '.$this->_process_where($this->dodb['where']);
+				}
+			}
+			if($this->dodb['what'] != 'INSERT' || $this->dodb['what'] !='REPLACE'){
+				if(isset($this->dodb['group']) && is_array($this->dodb['group']) && !empty($this->dodb['group'])) {
+					$qry .= ' GROUP BY '.implode(', ',$this->dodb['group']);
+				}
+				if(isset($this->dodb['order']) && is_array($this->dodb['order']) && !empty($this->dodb['order'])) {
+					$qry .= ' ORDER BY '.implode(', ',$this->dodb['order']);
+				}
+			}
+			if(isset($this->dodb['limit'])){
+				$qry .= $this->dodb['limit'].';';
+			}
+		}
+		//echo $qry.'<br />'."\n";
+		//mail('fabrizio at bibivu.com','qry',$qry);
+		$this->last_result = $this->db->query($qry);
+		if($this->dodb['what'] =='INSERT' || $this->dodb['what'] =='REPLACE'){
+			$this->select($this->db->insert_id());
+		}
+		$this->_reset_dodb();
+		return $this;
+	}
+
+	private function _get_info($id, $from='', $prepare = false){
+		$this->current = $this->select()->from($from)->fields()->where(array(array('field'=>'id', 'operator'=>'=', 'value'=>$id)))->do_db()->db->fetch_array($this->last_result);
+		return $this;
+	}
+	
+	public function get_record($how=NULL, $from=''){
+		if(is_array($how)){
+			$this->current = $this->select()->from($from)->fields()->where($how)->do_db()->db->fetch_array($this->last_result);
+		}elseif(is_numeric($how) || $from!=''){
+			$this->_get_info($how, $from);
+		} elseif(!isset($this->current['id']) || $this->current['id']<=0){
+						
+		}
+		return $this;
+	}
+
+	public function get($field='', $default=NULL){
+		if($field=='' || is_array($field)){
+			return $this->get_info();
+		}
+		return isset($this->current[$field])?$this->current[$field]:$default;
+	}
+
+	public function get_info(){
+		return $this->current?$this->current:array();
+	}
+	
+	/*
+	* this function returns every records
+	* and pass it throu the get_info function
+	*/
+	public function get_all($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
+		$records = array();
+		$this->select()->fields(array_merge(array('`'.($from!=''?$from:$this->dodb['table']).'`.`id`'), $xtrfields))->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
+		$tmps = $this->last_result;
+		while($record = $this->db->fetch_array($tmps)){
+			$records[] = $this->get_record($record['id'], $from)->get_info();
+		}
+		return $records;
+	}
+
+	/*
+	* this function returns every records
+	* and pass it throu the get_info function
+	*/
+	public function get_count($where=array(), $group=array(), $from='', $join = array()){
+		$records = array();
+		$this->select()->fields('COUNT(*) tot')->from($from)->qjoin($join)->where($where)->group($group)->limit(1)->do_db();
+		$tmps = $this->last_result;
+		$record = $this->db->fetch_array($tmps);
+		return $record['tot']+0;
+	}
+	
+	/*
+	* this function returns ONLY what is in the records
+	* without pass the records from get_info
+	*/
+	public function get_raw($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
+		$records = array();
+		$this->select()->fields($xtrfields)->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
+		$tmps = $this->last_result;
+		while($record = $this->db->fetch_array($tmps)){
+			$records[] = $record;
+		}
+		return $records;
+	}
+	
+	/*
+	* this function returns ONLY the ID field
+	*/
+	public function get_ids($where=array(), $orders=array(), $group=array(), $limit='', $from='', $join = array(), $xtrfields = array()){
+		$records = array();
+		$this->select()->fields(array_merge($xtrfields,array('`'.($from!=''?$from:$this->dodb['table']).'`.`id`')))->from($from)->qjoin($join)->where($where)->group($group)->order($orders)->limit($limit)->do_db();
+		$tmps = $this->last_result;
+		while($record = $this->db->fetch_array($tmps)){
+			$records[] = $record;
+		}
+		return $records;
+	}
+	
+	private function _process_where($where){
+		static $recur = 0;
+		$recur++;
+		$appends = array();
+		if(!is_array($where)) return 1;
+		foreach ($where as $fil) {
+			if(count($fil)!=3 || !isset($fil['field'])){
+				$appends[] = $this->_process_where($fil);
+			} else {
+				$appends[] = $this->_process_where_single($fil);
+			}
+		}
+		$append = '('.implode($recur==1?' AND ':' OR ', $appends).')';
+		$recur--;
+		return $append;
+	}
+	
+	private function _process_where_single($where){
+		if(!isset($where['field']) || !isset($where['operator']) || !isset($where['value'])){
+			return '';
+		}
+		$append		= '';
+		$field		= $where['field'];
+		$operator	= strtoupper($where['operator']);
+		$value		= $where['value'];
+
+		switch($operator){
+			case 'IN':
+				$value = '('.$where['value'].')';
+				break;
+			case 'IS':
+			case 'IS NOT':
+				$value = $where['value'];
+				break;
+			//all of those are falling into the default
+/*
+			case 'REGEXP':
+			case 'LIKE':
+			case '=':
+			case '!=':
+			case '>':
+			case '<':
+			case '=>':
+			case '=<':
+*/
+			default:
+				$value = '"'.$this->db->real_escape_string($where['value']).'"';
+		}
+		return $field.' '.$operator.' '.$value;
+	}
+
+	/*********************************************
+	 *  MANAGEMENT CONNECTION AND DISPLAY PART
+	 ********************************************/
+	public function get_related_tables(){
+		$this->related=array();
+		return $this->related;
+	}
+
+	public function get_bulk_actions(){
+		$this->bulk_actions=array();
+		return $this->bulk_actions;
+	}
+
+	public function get_fields(){
+		$this->fields=array();
+		return $this->fields;
+	}
+
+	public function add($tt_post){
+		if(is_array($tt_post)){
+			$filters = array();
+			$fields = $this->get_fields();
+			foreach($fields as $key=>$value){
+				if($value['post']==true && (isset($tt_post[$key]) || !is_null($value['default']))){
+					if(isset($tt_post[$key])){
+						$$key = $tt_post[$key];
+					} else {
+						$$key = $value['default'];
+					}
+					$this->set($key,$$key);
+				}
+			}
+			$ret = $this->insert()->do_db();
+		} else {
+			$ret = false;
+		}
+		return $ret;
+	}
+	
+	public function edit($id, $tt_post){
+		if($id>0 && is_array($tt_post)){
+			$filters = array();
+			$fields = $this->get_fields();
+			foreach($fields as $key=>$value){
+				if($value['post']==true && (isset($tt_post[$key]) || !is_null($value['default']))){
+					if(isset($tt_post[$key])){
+						$this->set($key,$tt_post[$key]);
+					}
+				}
+			}
+			$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
+			$ret = $this->update()->where($filters)->do_db();
+		} else {
+			$ret = false;
+		}
+		return $ret;
+	}
+	
+	public function edit_bulk($tt_post){
+		$ret = false;
+		if(is_array($tt_post)){
+			$fields = $this->get_fields();
+			foreach($tt_post as $key=>$value){
+				if(isset($fields[$key]) && is_array($value) && $fields[$key]['post']==true){
+					foreach($value as $id=>$vv){
+						$filters = array(array('field'=>'id', 'operator'=>'=', 'value'=>$id));
+						$ret = $this->update()->set($key,$vv)->where($filters)->do_db();
+					}
+				}
+			}
+		} else {
+			$ret = false;
+		}
+		return $ret;
+	}
+	
+	public function remove($id){
+		$filters = array();
+		$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
+		return  $this->delete()->where($filters)->do_db();
+	}
+
+	public function change($field, $value, $id=NULL, $from=''){
+		if(!is_numeric($id)){
+			$id = $this->get('id',0);
+		}
+		if($id>0 && trim($field)!=''){
+			$filters = array();
+			$filters[] = array('field'=>'id', 'operator'=>'=', 'value'=>$id);
+			$ret = $this->update()->from($from)->set($field,$value)->where($filters)->limit(1)->do_db();
+		} else {
+			$ret = false;
+		}
+		return $ret;
+	}
+}
 ?>
\ No newline at end of file

Modified: websites/pbidir.com/bibivu/tpl/pbidir/admin/form.php
===================================================================
--- websites/pbidir.com/bibivu/tpl/pbidir/admin/form.php	2008-04-15 16:55:10 UTC (rev 1730)
+++ websites/pbidir.com/bibivu/tpl/pbidir/admin/form.php	2008-04-16 02:37:45 UTC (rev 1731)
@@ -1,16 +1,12 @@
-<div class="form"><h1><?=$title_form?></h1>
-<?php
-if(isset($owner) && is_array($owner) && !empty($owner) && isset($owner['field']) && $owner['field']!='' && !load_library('permissions')->can($what,'write')){
-	//I need to set as FORCE the $owner['field']
-	$fields[$owner['field']]['force'] = load_library('permissions')->get('id');
-}
-foreach($fields as $field_name=>$field_values){
-	include(dirname(__FILE__).'/field.php');
-}
-?>
-</div>
-<input type="button" value="  Save  " onclick="postForm('form','<?=$save_uri?>')" class="button save" /><?php
-if($can_delete){
-	?><input type="button" value="  Delete  " onclick="postForm('form','<?=$delete_uri?>')" class="button delete" /><?php
-} ?>
-<input type="button" value="  Cancel  " onclick="divBox_remove()" class="button cancel" />
\ No newline at end of file
+<div class="form"><h1><?=$title_form?></h1>
+<?php
+if(isset($owner) && is_array($owner) && !empty($owner) && isset($owner['field']) && $owner['field']!='' && !load_library('permissions')->can($what,'write')){
+	//I need to set as FORCE the $owner['field']
+	$fields[$owner['field']]['force'] = load_library('permissions')->get('id');
+}
+foreach($fields as $field_name=>$field_values){
+	include(dirname(__FILE__).'/field.php');
+}
+?>
+</div>
+<input type="button" value="  Save  " onclick="postForm('form','<?=$save_uri?>')" class="button save" /><input type="button" value="  Cancel  " onclick="divBox_remove()" class="button cancel" />
\ No newline at end of file

Modified: websites/pbidir.com/bibivu/tpl/pbidir/admin/view.php
===================================================================
--- websites/pbidir.com/bibivu/tpl/pbidir/admin/view.php	2008-04-15 16:55:10 UTC (rev 1730)
+++ websites/pbidir.com/bibivu/tpl/pbidir/admin/view.php	2008-04-16 02:37:45 UTC (rev 1731)
@@ -103,8 +103,9 @@
 <?php
 if($can_write){
 	?><a href="<?=load_class('uri')->create_uri(array('edit'=>'','ajax'=>'', 'id'=>$record['id']))?>" class="divBox"><input type="button" value="  Edit  " class="button edit divBox" /></a><?php
+	?><a href="<?=load_class('uri')->create_uri(array('clone'=>'','ajax'=>'', 'id'=>$record['id']))?>" class="divBox"><input type="button" value="  Clone  " class="button clone divBox" /><?php
 }
-if($can_delete){
+if($can_delete && isset($record['id']) && $record['id']>0){
 	?><input type="button" value="  Delete  " onclick="postForm('form','<?=$delete_uri?>')" class="button delete" /><?php
 } ?>
 <input type="button" value="  Cancel  " onclick="divBox_remove()" class="button cancel" />
\ No newline at end of file



More information about the Commits mailing list