phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] property/inc/class.soactor.inc.php, 1.1.2.1


From: nomail
Subject: [Phpgroupware-cvs] property/inc/class.soactor.inc.php, 1.1.2.1
Date: Tue, 25 May 2004 23:15:22 +0200

Update of /property/inc
Added Files:
        Branch: sigurd-2
          class.soactor.inc.php

date: 2004/05/25 21:15:22;  author: sigurdne;  state: Exp;  lines: +812 -0

Log Message:
no message
=====================================================================
<?php
        
/**************************************************************************\
        * phpGroupWare - property                                               
   *
        * http://www.phpgroupware.org                                           
   *
        *                                                                       
   *
        * Facilities Management                                                 
   *
        * Written by Sigurd Nes [sigurdne at online.no]                         
   *
        * 
------------------------------------------------------------------------ *
        * Copyright 2000 - 2003 Free Software Foundation, Inc                   
   *
        * This program is part of the GNU project, see http://www.gnu.org/      
   *
        * 
------------------------------------------------------------------------ *
        * This program is free software; you can redistribute it and/or modify 
it  *
        * under the terms of the GNU General Public License as published by the 
   *
        * Free Software Foundation; either version 2 of the License, or (at 
your   *
        * option) any later version.                                            
   *
        
\**************************************************************************/

        class soactor
        {
                var $role;

                function soactor()
                {
                        $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        $this->db               = $GLOBALS['phpgw']->db;
                        $this->db2              = $this->db;
                        $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
                        $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');

                        $this->join                     = $this->bocommon->join;
                        $this->left_join        = $this->bocommon->left_join;
                }

                function select_category_list()
                {
                        $table = 'fm_' . $this->role . '_category';
                        $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");

                        $i = 0;
                        while ($this->db->next_record())
                        {
                                $categories[$i]['id']                           
= $this->db->f('id');
                                $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
                                $i++;
                        }
                        return $categories;
                }

                function read($data)
                {
                        if(is_array($data))
                        {
                                $start  = 
(isset($data['start'])?$data['start']:0);
                                $filter = 
(isset($data['filter'])?$data['filter']:'none');
                                $query = 
(isset($data['query'])?$data['query']:'');
                                $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
                                $order = 
(isset($data['order'])?$data['order']:'');
                                $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
                                $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
                        }

                        $sql = $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup);

                        $entity_table = 'fm_' . $this->role;
                        $category_table = 'fm_' . $this->role . '_category';
                        $choice_table = 'fm_' . $this->role . '_choice';
                        if(!$sql)
                        {
                                $cols = $entity_table . 
".*,$category_table.descr as category";

                                $cols_return[]                          = 'id';
                                $uicols['input_type'][]         = 'text';
                                $uicols['name'][]                       = 'id';
                                $uicols['descr'][]                      = 
lang('ID');
                                $uicols['statustext'][]         = lang('ID');

                                $cols_return[]                          = 'id';
                                $uicols['input_type'][]         = 'hidden';
                                $uicols['name'][]                       = 'id';
                                $uicols['descr'][]                      = false;
                                $uicols['statustext'][]         = false;

                                $cols_return[]                          = 
'last_name';
                                $uicols['input_type'][]         = 'text';
                                $uicols['name'][]                       = 
'last_name';
                                $uicols['descr'][]                      = 
lang('last name');
                                $uicols['statustext'][]         = lang('last 
name');

                                $cols_return[]                          = 
'first_name';
                                $uicols['input_type'][]         = 'text';
                                $uicols['name'][]                       = 
'first_name';
                                $uicols['descr'][]                      = 
lang('first name');
                                $uicols['statustext'][]         = lang('first 
name');

                                $cols_return[]                          = 
'category';
                                $uicols['input_type'][]         = 'text';
                                $uicols['name'][]                       = 
'category';
                                $uicols['descr'][]                      = 
lang('category');
                                $uicols['statustext'][]         = 
lang('category');


                                $paranthesis .='(';

                                $joinmethod .= " $this->join  " . $entity_table 
. "_category ON ( $entity_table" . ".category =" .$entity_table . 
"_category.id))";

                                $from .= " FROM $paranthesis $entity_table ";

                                $sql = "SELECT $cols $from $joinmethod";

                                $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup,$sql);
                                $this->bocommon->fm_cache('uicols_actor_' . 
$this->role . $lookup,$uicols);
                                $this->bocommon->fm_cache('cols_return_actor_' 
. $this->role . $lookup,$cols_return);

                        }
                        else
                        {
                                $uicols                                         
        = $this->bocommon->fm_cache('uicols_actor_'. $this->role . $lookup);
                                $cols_return                                    
= $this->bocommon->fm_cache('cols_return_actor_' . $this->role . $lookup);
                                if($lookup)
                                {
                                        $admin_entity   = 
CreateObject($this->currentapp.'_soadmin_entity');
                                        $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
                                }

                        }

                        
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp][$this->role
 . '_columns'];

//html_print_r($user_columns);

                        if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
                        {
                                $i      = count($uicols['name']);
                                foreach($user_columns as $column_id)
                                {
                                        $this->db->query("SELECT * FROM 
$category_table WHERE id= $column_id");

                                        $this->db->next_record();
                                        $uicols['input_type'][]         = 
'text';
                                        $uicols['name'][]                       
= $this->db->f('column_name');
                                        $uicols['descr'][]                      
= $this->db->f('input_text');
                                        $uicols['statustext'][]         = 
$this->db->f('statustext');
                                        $uicols['datatype'][$i]         = 
$this->db->f('datatype');
                                        $cols_return_extra[]= array(
                                                'name'  => 
$this->db->f('column_name'),
                                                'datatype'      => 
$this->db->f('datatype'),
                                                'attrib_id'     => 
$this->db->f('id')
                                        );
                                        $i++;
                                }
                        }

                        $this->uicols   = $uicols;

//html_print_r($cols_return_extra);
                        if ($order)
                        {
                                $ordermethod = " order by $entity_table.$order 
$sort";
                        }
                        else
                        {
                                $ordermethod = " order by $entity_table.id 
DESC";
                        }

                        $where= 'WHERE';

/*                      if ($filter=='all')
                        {
                                if (is_array($grants))
                                {
                                        while (list($user) = each($grants))
                                        {
                                                $public_user_list[] = $user;
                                        }
                                        reset($public_user_list);
                                        $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";

                                        $where= 'AND';
                                }

                        }
                        else
                        {
                                $filtermethod = " $where 
$entity_table.user_id=$filter ";
                                $where= 'AND';
                        }
*/

                        if ($cat_id>0)
                        {
                                $filtermethod .= " $where 
$entity_table.category=$cat_id ";
                                $where= 'AND';
                        }

                        if ($status)
                        {
                                $filtermethod .= " $where 
$entity_table.status='$status' ";
                                $where= 'AND';
                        }


                        if($query)
                        {
                                $query = ereg_replace("'",'',$query);
                                $query = ereg_replace('"','',$query);

                                $querymethod = " $where ( 
$entity_table.first_name LIKE '%$query%' OR $entity_table.last_name LIKE 
'%$query%')";
                                $where = 'AND';
                        }

                        $sql .= " $filtermethod $querymethod";
//echo $sql;

                        $this->db2->query($sql,__LINE__,__FILE__);
                        $this->total_records = $this->db2->num_rows();
                        if(!$allrows)
                        {
                                $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
                        }
                        else
                        {
                                $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
                        }

                        $j=0;
                        $n=count($cols_return);
//html_print_r($cols_return);
                        while ($this->db->next_record())
                        {
                                for ($i=0;$i<$n;$i++)
                                {
                                        $actor_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
                                        $actor_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
                                }

                                for ($i=0;$i<count($cols_return_extra);$i++)
                                {
                                        $value='';
                                        
$value=$this->db->f($cols_return_extra[$i]['name']);

                                        
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
                                        {
                                                $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value;
                                                $this->db2->query($sql);
                                                $this->db2->next_record();
                                                
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
                                        }
                                        
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
                                        {
                                                $sql="SELECT org_name FROM 
phpgw_addressbook where id=$value";
                                                $this->db2->query($sql);
                                                $this->db2->next_record();
                                                
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
                                        }
                                        
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
                                        {
                                                $ch= unserialize($value);

                                                if (isset($ch) AND 
is_array($ch))
                                                {
                                                        for 
($k=0;$k<count($ch);$k++)
                                                        {
                                                                $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k];
                                                                
$this->db2->query($sql);
                                                                while 
($this->db2->next_record())
                                                                {
                                                                        
$ch_value[]=$this->db2->f('value');
                                                                }
                                                        }
                                                        
$actor_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
                                                        unset($ch_value);
                                                }
                                        }
                                        
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
                                        {
//html_print_r($value);

                                                
$actor_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_data']['prefs'][$this->currentapp
 . '.dateformat'],strtotime($value));
                                        }
                                        else:
                                        {
                                                
$actor_list[$j][$cols_return_extra[$i]['name']]=$value;
                                        }
                                        endif;
                                }

                                $j++;
                        }
//html_print_r($actor_list);
                        return $actor_list;
                }


                function read_single($data)
                {
                        $attribute_table = 'fm_' . $this->role . '_attribute';
                        $table = 'fm_' . $this->role;

                        $actor_id =$data['actor_id'];

                        $this->db->query("SELECT * FROM $attribute_table ORDER 
BY attrib_sort");

                        while ($this->db->next_record())
                        {
                                $actor['attributes'][] = array
                                (
                                        'attrib_id'             => 
$this->db->f('id'),
                                        'name'                  => 
$this->db->f('column_name'),
                                        'input_text'    => 
stripslashes($this->db->f('input_text')),
                                        'statustext'    => 
stripslashes($this->db->f('statustext')),
                                        'datatype'              => 
$this->db->f('datatype')
                                );
                        }

                        if($actor_id)
                        {
                                $this->db->query("SELECT * from $table where 
id='$actor_id'");

                                if($this->db->next_record())
                                {
                                        $actor['id']                    = 
(int)$this->db->f('id');
                                        $actor['last_name']             = 
stripslashes($this->db->f('last_name'));
                                        $actor['first_name']            = 
stripslashes($this->db->f('first_name'));
                                        $actor['contact_phone'] = 
$this->db->f('contact_phone');
                                        $actor['entry_date']            = 
$this->db->f('entry_date');
                                        $actor['cat_id']                        
= (int)$this->db->f('category');

                                        for 
($i=0;$i<count($actor['attributes']);$i++)
                                        {
                                                
$actor['attributes'][$i]['value']       = 
$this->db->f($actor['attributes'][$i]['name']);
                                                
$actor['attributes'][$i]['datatype_text']       = 
$this->bocommon->translate_datatype($actor['attributes'][$i]['datatype']);
                                        }

                                }
                        }

                        return $actor;
                }

                function add($actor,$values_attribute='')
                {
                        $table = 'fm_' . $this->role;
                        $actor['last_name'] = 
$this->db->db_addslashes($actor['last_name']);
                        $actor['first_name'] = 
$this->db->db_addslashes($actor['first_name']);

                        while (is_array($actor['extra']) && 
list($input_name,$value) = each($actor['extra']))
                        {
                                if($value)
                                {
                                        $cols[] = $input_name;
                                        $vals[] = $value;
                                }
                        }

                        if (isset($values_attribute) AND 
is_array($values_attribute))
                        {
                                foreach($values_attribute as $entry)
                                {
                                        if($entry['value'])
                                        {
                                                $cols[] = $entry['name'];
                                                $vals[] = $entry['value'];
                                        }
                                }
                        }

                        if($cols)
                        {
                                $cols   = "," . implode(",", $cols);
                                $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
                        }

                        $this->db->transaction_begin();

                        $this->db->query("INSERT INTO $table 
(entry_date,last_name,first_name,contact_phone,category $cols) "
                                . "VALUES ('" . time() . "','" . 
$actor['last_name'] . "','" . $actor['first_name'] . "','" . 
$actor['contact_phone'] . "','" . $actor['cat_id'] . "' $vals)");

                        $receipt['actor_id']= 
$this->db->get_last_insert_id($table,'id');

                        $receipt['message'][] = array('msg'=>lang('actor %1 has 
been saved',$receipt['actor_id']));

                        $this->db->transaction_commit();
                        return $receipt;
                }

                function edit($actor,$values_attribute='')
                {
//_debug_array($actor);
//_debug_array($values_attribute);
                        $table = 'fm_' . $this->role;
                        $actor['last_name'] = 
$this->db->db_addslashes($actor['last_name']);
                        $actor['first_name'] = 
$this->db->db_addslashes($actor['first_name']);

                        while (is_array($actor['extra']) && 
list($column,$value) = each($actor['extra']))
                        {
                                $value_set[$column]     = $value;
                        }

                        if (isset($values_attribute) AND 
is_array($values_attribute))
                        {
                                foreach($values_attribute as $entry)
                                {
                                        if($entry['datatype']!='AB')
                                        {
                                                $value_set[$entry['name']]      
= $entry['value'];
                                        }
                                }
                        }

                        if($value_set)
                        {
                                $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
                        }

                        $this->db->query("UPDATE $table set last_name='" . 
$actor['last_name'] . "', first_name='" . $actor['first_name'] . "', 
contact_phone='" . $actor['contact_phone'] . "', entry_date='" . time() . "', 
category='"
                                                        . $actor['cat_id'] . "' 
$value_set WHERE id=" . intval($actor['actor_id']));

                        $receipt['actor_id']= $actor['actor_id'];
                        $receipt['message'][] = array('msg'=>lang('actor %1 has 
been edited',$actor['actor_id']));
                        return $receipt;
                }

                function delete($actor_id)
                {
                        $table = 'fm_' . $this->role;
                        $this->db->query('DELETE FROM $table WHERE id=' . 
intval($actor_id));
                }

                function read_attrib($data)
                {
                        $attribute_table = 'fm_' . $this->role . '_attribute';
//html_print_r($data);
                        if(is_array($data))
                        {
                                if ($data['start'])
                                {
                                        $start=$data['start'];
                                }
                                else
                                {
                                        $start=0;
                                }
                                $query = 
(isset($data['query'])?$data['query']:'');
                                $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
                                $order = 
(isset($data['order'])?$data['order']:'');
                                $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
                        }

                        if ($order)
                        {
                                $ordermethod = " order by $order $sort";

                        }
                        else
                        {
                                $ordermethod = ' order by attrib_sort asc';
                        }

                        if($query)
                        {
                                $query = ereg_replace("'",'',$query);
                                $query = ereg_replace('"','',$query);

                                $querymethod = " WHERE 
($attribute_table.input_text LIKE '%$query%' or $attribute_table.column_name 
LIKE '%$query%')";
                        }

                        $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";


                        $this->db2->query($sql,__LINE__,__FILE__);
                        $this->total_records = $this->db2->num_rows();
                        if(!$allrows)
                        {
                                $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
                        }
                        else
                        {
                                $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
                        }

                        while ($this->db->next_record())
                        {
                                $attrib[] = array
                                (
                                        'id'                    => 
$this->db->f('id'),
                                        'attrib_sort'   => 
$this->db->f('attrib_sort'),
                                        'list'                  => 
$this->db->f('list'),
                                        'lookup_form'   => 
$this->db->f('lookup_form'),
                                        'column_name'   => 
$this->db->f('column_name'),
                                        'name'                  => 
$this->db->f('input_text'),
                                        'size'                  => 
$this->db->f('size'),
                                        'statustext'    => 
$this->db->f('statustext'),
                                        'input_text'    => 
$this->db->f('input_text'),
                                        'type_name'             => 
$this->db->f('type'),
                                        'datatype'              => 
$this->db->f('datatype')
                                );
                        }
                        return $attrib;
                }

                function read_single_attrib($id)
                {
                        $attribute_table = 'fm_' . $this->role . '_attribute';

                        $sql = "SELECT * FROM $attribute_table where id=$id";

                        $this->db->query($sql);

                        if($this->db->next_record())
                        {
                                $attrib['id']                                   
        = $this->db->f('id');
                                $attrib['column_name']                          
= $this->db->f('column_name');
                                $attrib['input_text']                           
= $this->db->f('input_text');
                                $attrib['statustext']                           
= $this->db->f('statustext');
                                $attrib['column_info']['precision']     = 
$this->db->f('precision_');
                                $attrib['column_info']['scale']         = 
$this->db->f('scale');
                                $attrib['column_info']['default']       = 
$this->db->f('default_value');
                                $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
                                $attrib['column_info']['type']          = 
$this->db->f('datatype');
                                $attrib['type_name']                            
= $this->db->f('type_name');
                                $attrib['lookup_form']                          
= $this->db->f('lookup_form');
                                $attrib['list']                                 
        = $this->db->f('list');
                                if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
                                {
                                        $attrib['choice'] = 
$this->read_attrib_choice($id);
                                }

                                return $attrib;
                        }
                }

                function read_attrib_choice($attrib_id)
                {
                        $choice_table = 'fm_' . $this->role . '_choice';
                        $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id";
                        $this->db->query($sql);

                        while ($this->db->next_record())
                        {
                                $choice[] = array
                                (
                                        'id'    => $this->db->f('id'),
                                        'value' => $this->db->f('value')
                                );

                        }
                        return $choice;
                }

                function add_attrib($attrib)
                {
                        $table = 'fm_' . $this->role;
                        $attribute_table = 'fm_' . $this->role . '_attribute';
                        $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
                        $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
                        $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
                        $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
                        $attrib['id'] = 
$this->bocommon->next_id($attribute_table);

                        $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
                        $this->db->query($sql);
                        $this->db->next_record();
                        $attrib_sort    = $this->db->f('max_sort')+1;

                        $values= array(
                                $attrib['id'],
                                $attrib['column_name'],
                                $attrib['input_text'],
                                $attrib['statustext'],
                                $attrib['lookup_form'],
                                $attrib['list'],
                                $attrib_sort,
                                $attrib['column_info']['type'],
                                $attrib['column_info']['precision'],
                                $attrib['column_info']['scale'],
                                $attrib['column_info']['default'],
                                $attrib['column_info']['nullable']
                                );

                        $values = $this->bocommon->validate_db_insert($values);

                        $this->db->transaction_begin();

                        $this->db->query("INSERT INTO $attribute_table 
(id,column_name, input_text, 
statustext,lookup_form,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
                                . "VALUES ($values)");

                        $receipt['id']= $attrib['id'];

                        $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);

                        if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
                        {
                                $attrib['column_info']['precision']=4;
                        }

                        if(!$attrib['column_info']['default'])
                        {
                                unset($attrib['column_info']['default']);
                        }

                        $this->init_process();

                        
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
                        {
                                $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
                                $this->db->transaction_commit();

                        }
                        else
                        {
                                $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
                                if($this->db->Transaction)
                                {
                                        $GLOBALS['phpgw']->db->rollbacktrans();
                                }
                                else
                                {
                                        $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
                                        unset($receipt['id']);

                                }
                        }

                        return $receipt;
                }

                function init_process()
                {
                        $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
                        $this->oProc->m_odb                                     
= $this->db;
                        $this->oProc->m_odb->Halt_On_Error      = 'report';
                }

                function edit_attrib($attrib)
                {
                        $attribute_table = 'fm_' . $this->role . '_attribute';
                        $table = 'fm_' . $this->role;

                        $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
                        $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
                        $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
                        $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);

                        $choice_table = 'fm_' . $this->role . '_choice';

                        $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "'");
                        $this->db->next_record();
                        $OldColumnName          = $this->db->f('column_name');

                        $this->db->transaction_begin();

                        $attrib_type=$attrib['column_info']['type'];
                        if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
                        {
                                $value_set=array(
                                        'column_name'           => 
$attrib['column_name'],
                                        'input_text'            => 
$attrib['input_text'],
                                        'statustext'            => 
$attrib['statustext'],
                                        'lookup_form'           => 
$attrib['lookup_form'],
                                        'list'                          => 
$attrib['list'],
                                        'datatype'                      => 
$attrib['column_info']['type'],
                                        'precision_'            => 
$attrib['column_info']['precision'],
                                        'scale'                         => 
$attrib['column_info']['scale'],
                                        'default_value'         => 
$attrib['column_info']['default'],
                                        'nullable'                      => 
$attrib['column_info']['nullable']
                                        );

                                $value_set      = 
$this->bocommon->validate_db_update($value_set);

                                $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id']);

                                $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);

                                if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
                                {
                                        $attrib['column_info']['precision']=4;
                                }

                                if(!$attrib['column_info']['default'])
                                {
                                        
unset($attrib['column_info']['default']);
                                }

                                $this->init_process();

                                
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);

                        }


                        if($attrib['new_choice'])
                        {
                                $choice_id = 
$this->bocommon->next_id($choice_table ,array('attrib_id'=>$attrib['id']));

                                $values= array(
                                        $attrib['id'],
                                        $choice_id,
                                        $attrib['new_choice']
                                        );

                                $values = 
$this->bocommon->validate_db_insert($values);

                                $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,value) "
                                . "VALUES ($values)");
                        }


                        if($attrib['delete_choice'])
                        {
                                for 
($i=0;$i<count($attrib['delete_choice']);$i++)
                                {
                                        $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i]);
                                }
                        }

//html_print_r($attrib_type);
                        if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
                        {
                                $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id']);
                        }


                        $this->db->transaction_commit();

                        $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));


                        return $receipt;

                }
                function resort_attrib($data)
                {
//html_print_r($data);
                        $attribute_table = 'fm_' . $this->role . '_attribute';
                        if(is_array($data))
                        {
                                $resort = 
(isset($data['resort'])?$data['resort']:'up');
                                $id = (isset($data['id'])?$data['id']:'');
                        }

                        $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
                        $this->db->query($sql);
                        $this->db->next_record();
                        $attrib_sort    = $this->db->f('attrib_sort');
                        $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
                        $this->db->query($sql);
                        $this->db->next_record();
                        $max_sort       = $this->db->f('max_sort');
                        switch($resort)
                        {
                                case 'up':
                                        if($attrib_sort>1)
                                        {
                                                $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
                                                $this->db->query($sql);
                                                $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
                                                $this->db->query($sql);
                                        }
                                        break;
                                case 'down':
                                        if($max_sort > $attrib_sort)
                                        {
                                                $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
                                                $this->db->query($sql);
                                                $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
                                                $this->db->query($sql);
                                        }
                                        break;
                                default:
                                        return;
                                        break;
                        }
                }
                function delete_attrib($attrib_id)
                {
                        $table = 'fm_' . $this->role;
                        $attribute_table = 'fm_' . $this->role . '_attribute';
                        $this->init_process();

                        $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";

                        $this->db->query($sql);
                        $this->db->next_record();
                        $ColumnName             = $this->db->f('column_name');

                        if($this->oProc->DropColumn($table,'', $ColumnName))
                        {
                                $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
                                $this->db->query($sql);
                                $this->db->next_record();
                                $attrib_sort    = $this->db->f('attrib_sort');
                                $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
                                $this->db->query($sql2);
                                $this->db->next_record();
                                $max_sort       = $this->db->f('max_sort');
                                if($max_sort>$attrib_sort)
                                {
                                        $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
                                        $this->db->query($sql);
                                }

                                $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
                        }
                        else
                        {
                                $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
                        }

                        return $receipt;
                }
        }
?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]