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