Welcome Guest, Not a member yet? Register   Sign In
Modelcode review
#1

[eluser]Unknown[/eluser]
Hi,
sorry for the topic, but i have no idea how i can describe it better Wink
So, i'm working on a friends project (shiftplans) and from time to time some entries in a shiftplan disappear.
The only idea i have is that the following two model-functions are responsible for that.
it's not my code, but i have to work with it ;(


Code:
function delete_employee($column,$plan_id,$affected_entry){
        log_event('löschen','shiftplan_model','delete_employee','column - '.$column,'plan_id - '.$plan_id,'affected_entry - '.$affected_entry);
        $this->db->where('id',$plan_id);
        $this->db->select('anzahl_mitarbeiter');
        $q = $this->db->get($this->tbl_shift);
        $this->db->where('id',$plan_id);
        $update_array = array(
                    'anzahl_mitarbeiter'=>($q->row()->anzahl_mitarbeiter)-1
        );    
        $this->db->update($this->tbl_shift,$update_array);
        unset($update_array);
        $this->db->where($column,$affected_entry);
        $this->db->delete($this->tbl_detail);
        $this->db->where('plan_id',$plan_id);
        $this->db->select($column);
        $query = $this->db->get($this->tbl_detail);
        $helper = array();
        foreach ($query->result() as $value){
            $helper[] = $value->$column;
        }
        $helper = array_values(array_unique($helper));
        $count_elements = count($helper);
        unset($helper);
        for ($i=1;$i<=$count_elements;$i++){
            $this->db->where($column,$affected_entry+1);
            $q = $this->db->get($this->tbl_detail);
            if ($q->num_rows() > 0){
                $this->db->where($column,$affected_entry+1);
                $update_array = array(
                            $column=>$affected_entry
                );
                $this->db->update($this->tbl_detail,$update_array);
                $affected_entry++;
            }
        }
        //update fields
        $this->db->where($column,$affected_entry);
        $this->db->delete($this->tbl_fields_value);
        $this->db->where('plan_id',$plan_id);
        $this->db->select($column);
        $query = $this->db->get($this->tbl_fields_value);
        $helper = array();
        foreach ($query->result() as $value){
            $helper[] = $value->$column;
        }
        $helper = array_values(array_unique($helper));
        $count_elements = count($helper);
        unset($helper);
        for ($i=1;$i<=$count_elements;$i++){
            $this->db->where($column,$affected_entry+1);
            $q = $this->db->get($this->tbl_fields_value);
            if ($q->num_rows() > 0){
                $this->db->where($column,$affected_entry+1);
                $update_array = array(
                            $column=>$affected_entry
                );
                $this->db->update($this->tbl_fields_value,$update_array);
                $affected_entry++;
            }
        }
        
        
    }
    
    function delete_empty_column($plan_id){
        log_event('löschen','shiftplan_model','delete_empty_column','plan_id - '.$plan_id);
        $this->db->where('id',$plan_id);
        $this->db->select('anzahl_mitarbeiter');
        $anzahl_mitarbeiter = $this->db->get($this->tbl_shift)->row()->anzahl_mitarbeiter;
        $this->db->where('plan_id',$plan_id);
        $this->db->select('sort_id');
        $entries = $this->db->get($this->tbl_detail)->result();
        $helper = array();
        foreach ($entries as $value){
            $helper[] = $value->sort_id;
        }
        $entries = array_values(array_unique($helper));
        $entries = count($entries);    
        $empty_columns = $anzahl_mitarbeiter-$entries;
        
        if ($empty_columns > 0){
            $this->db->where('id',$plan_id);
            $update_array = array('anzahl_mitarbeiter'=>$entries);
            $this->db->update($this->tbl_shift,$update_array);
            unset($update_array);
        }

        $data_sort = array();
        for ($i=1;$i<=$anzahl_mitarbeiter;$i++){
            $this->db->where('plan_id',$plan_id);
            $this->db->where('sort_id',$i);
            $this->db->order_by('sort_id','asc');
            $q = $this->db->get($this->tbl_detail);
            if ($q->num_rows() > 0){
                $data_sort[$i] = $q->result();    
            }
        }

        $this->db->where('plan_id',$plan_id);
        $this->db->delete($this->tbl_detail);
        $data_sort = array_values($data_sort);
                
        
        // $key+1 = sort_id
        foreach ($data_sort as $key=>$value){
            $sort_id = $key+1;
            foreach ($value as $dataset){
                $data_array = array(
                    'plan_id'=>$dataset->plan_id,
                    'sort_id'=>$sort_id,
                    'user_id'=>$dataset->user_id,
                    'tag'=>$dataset->tag,
                    'start'=>$dataset->start,
                    'ende'=>$dataset->ende    
                );        
                $this->db->insert($this->tbl_detail,$data_array);
                unset($data_array);
            }
        }
    }

The goal of this two functions is to delete a user from the shiftplan. in a plan the entries are saved with a sort_id (user 1 to 10 for example) and a user_id.
so the functions delete a user from a plan and are responsible that the sort_id is new indexed. 1.2.3.4.5 and so on.

I now the code is a mess and i hate it, but i have absolutly no idea why some entries gets deleted.i hope the 2 functions are responsible for the mess, otherwise .. uff.

maybee u have a idea or a hint. would be nice Wink thanks a lot.


and sorry for my grammar etc.. english ist not my native language.




Theme © iAndrew 2016 - Forum software by © MyBB