PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/magehelp/application/models/acl/role.php

https://bitbucket.org/jit_bec/shopifine
PHP | 251 lines | 178 code | 58 blank | 15 comment | 23 complexity | 4105b635811ff9355f6e3b4954cad611 MD5 | raw file
  1<?php
  2
  3/*
  4 * To change this template, choose Tools | Templates
  5 * and open the template in the editor.
  6 */
  7
  8/**
  9 * Description of role
 10 *
 11 * @author abhijit
 12 */
 13
 14class Role extends CI_Model {	
 15	
 16        function insert($role_data,$parents = array())
 17	{
 18            //$this->db->insert('invoice',$invoice_data);
 19            //return $this->db->insert('roles',$role_data);
 20            $this->db->trans_start();
 21            $this->db->insert('acl_roles',$role_data);
 22            $id = $this->db->insert_id();
 23            if (!empty($parents)){
 24                foreach ($parents as $parent){
 25                    $parent_name = $this->getName($parent);
 26                    if (!empty($parent_name)){
 27                        $role_inherit_data = array('role_id'=>$id,'role_name'=>$role_data['role_name'],
 28                            'parent_role_id'=>$parent,'parent_role_name'=>$parent_name);
 29                        $this->db->insert('acl_role_inheritance_mapping',$role_inherit_data);
 30                    }
 31                }
 32            }
 33            $this->db->trans_complete();
 34            if ($this->db->trans_status() === FALSE)
 35            {
 36                //echo $this->db->_error_message();
 37                die( 'Shipping  Failed.Please check log ');
 38            }
 39            else {
 40                $success = true;
 41            }
 42	}
 43        
 44        
 45        function totalNoOfRoles () {
 46            
 47            $this->db->from('acl_roles');
 48            return $this->db->count_all_results() ;
 49            
 50            
 51        }
 52        
 53        function getAllParentsMapping($csv = false,$whereClause=null,$order_limit_clause=array(),$like_fields_array=null,$or_where_clause_array=null){
 54            $orderBy = 'id';
 55            $orderDir= 'desc';
 56            $startLimit = 0;
 57            $limit = 1000;
 58            
 59            if (!empty($order_limit_clause['orderBy'])){
 60                $orderBy = $order_limit_clause['orderBy'];
 61            }
 62            if (!empty($order_limit_clause['orderDir'])){
 63                $orderDir = $order_limit_clause['orderDir'];
 64            }
 65            if (!empty($order_limit_clause['startLimit'])){
 66                $startLimit = $order_limit_clause['startLimit'];
 67            }
 68            if (!empty($order_limit_clause['limit'])){
 69                $limit = $order_limit_clause['limit'];
 70            }
 71        
 72            $this->load->dbutil();  
 73            $this->db->select('*');
 74            if (!empty($whereClause)){
 75                $this->db->where($whereClause);
 76            }
 77            
 78            if (!empty($or_where_clause_array)){
 79                $this->db->or_where($or_where_clause_array);
 80            }
 81            
 82            if (!empty($like_fields_array)){
 83                $this->db->like($like_fields_array);
 84            }
 85            $this->db->order_by($orderBy,$orderDir);
 86            $this->db->limit($limit,$startLimit);
 87            $query = $this->db->get('acl_role_inheritance_mapping');
 88            
 89            if ($csv){
 90                return $this->dbutil->csv_from_result($query);
 91            }
 92            return $query->result_array();
 93        }
 94        
 95        function totalNoOfRowsInParentMapping ($where_clause_array=null,$like_fields_array=null,$or_where_clause_array=null) {
 96            if (!empty($where_clause_array)){
 97                $this->db->where($where_clause_array);
 98            }
 99            
100            if (!empty($or_where_clause_array)){
101                $this->db->or_where($or_where_clause_array);
102            }
103            
104            if (!empty($like_fields_array)){
105                $this->db->like($like_fields_array);
106            }
107            
108            $this->db->from('acl_role_inheritance_mapping');
109            return $this->db->count_all_results() ;
110            
111            
112        }
113        function getAll(){
114            $this->db->select('*');
115            $query = $this->db->get('acl_roles');
116            return $query->result_array();
117        }
118        
119	
120        function getAllParents($id){
121            $this->db->select('parent_role_name');
122            $this->db->where('role_id',$id);
123            $this->db->order_by('relative_order_parent','asc');
124            $query = $this->db->get('acl_role_inheritance_mapping');
125            //log_message('debug','parent roles '.$this->db->last_query());
126            return $query->result_array();
127        }
128        
129        function getId($role_name){
130            $this->db->select('id');
131            $this->db->where('role_name',$role_name);
132            $query = $this->db->get('acl_roles');
133            if ($query->num_rows() > 0)
134            {
135                $row = $query->row_array(); 
136
137                return  $row['id'];
138            
139            } 
140            return null;
141        }
142        
143        function getName($id){
144            $this->db->select('role_name');
145            $this->db->where('id',$id);
146            $query = $this->db->get('acl_roles');
147            if ($query->num_rows() > 0)
148            {
149                $row = $query->row_array(); 
150
151                return  $row['role_name'];
152            
153            } 
154            return null;
155        }
156        
157        function parentExists($roleid,$parentroleid) {
158            
159            $this->db->from('acl_role_inheritance_mapping');
160            $this->db->where('role_id',$roleid);
161            $this->db->where('parent_role_id',$parentroleid);
162            
163            
164            $query = $this->db->get();
165            return ($query->num_rows()==1);
166            
167        }
168        
169        function parentExistsById($id) {
170            
171            $this->db->from('acl_role_inheritance_mapping');
172            $this->db->where('id',$id);
173            
174            
175            $query = $this->db->get();
176            return ($query->num_rows()==1);
177            
178        }
179        
180        function roleExists($id) {
181            
182            $this->db->from('acl_roles');
183            $this->db->where('id',$id);
184            
185            
186            $query = $this->db->get();
187            return ($query->num_rows()==1);
188            
189        }
190        
191        function saveParent($parent_data,$id=false){
192            $success=false;
193            //Run these queries as a transaction, we want to make sure we do all or nothing
194            if (!empty($parent_data)){
195                try {
196                    if (!$id or !$this->parentExistsById($id)){
197                        $success = $this->db->insert('acl_role_inheritance_mapping',$parent_data);
198                    }
199                    else{
200
201                        $this->db->where('id', $id);
202                        $success = $this->db->update('acl_role_inheritance_mapping',$parent_data);
203
204                    }
205                    if ($success){
206                        log_message('debug','Parent Role Suceesfully Modified');
207                    }
208                }
209                catch (Exception $e){
210                    log_message('Parent Role Creation Failed '.$this->db->_error_message() );
211                    throw new Exception('Parent Role Creation Failed' );
212                }
213                
214            }
215            
216            
217            return $success;
218        }
219        
220        function save($role_data,$id=false){
221            $success=false;
222            //Run these queries as a transaction, we want to make sure we do all or nothing
223            if (!empty($role_data)){
224                try {
225                    if (!$id or !$this->roleExists($id)){
226                        $success = $this->db->insert('acl_roles',$role_data);
227                    }
228                    else{
229
230                        $this->db->where('id', $id);
231                        $success = $this->db->update('acl_roles',$role_data);
232
233                    }
234                    if ($success){
235                        log_message('debug','Role Suceesfully Modified');
236                    }
237                }
238                catch (Exception $e){
239                    log_message('Parent Role Creation Failed '.$this->db->_error_message() );
240                    throw new Exception('Parent Role Creation Failed' );
241                }
242                
243            }
244            
245            
246            return $success;
247        }
248	
249}
250
251?>