PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/table_torch.php

https://github.com/splitfeed/Table-Torch
PHP | 374 lines | 253 code | 118 blank | 3 comment | 31 complexity | 1c3fe0d8dd790f2c31d2095ac6bad1e0 MD5 | raw file
  1. <?php
  2. if (! defined('BASEPATH')) exit('No direct script access');
  3. class Table_torch {
  4. public $dilem = "::";
  5. public $org_path = '';
  6. public $settings = array();
  7. public $load_prefix = '';
  8. public $CI;
  9. public $url_vals = array( 'search_field'=>'',
  10. 'keyword'=>'',
  11. 'sort_field'=>'',
  12. 'sort_dir'=>'',
  13. 'table'=>'',
  14. 'action'=>'',
  15. 'key'=>'' );
  16. //php 5 constructor
  17. function __construct() {
  18. $this->CI = &get_instance();
  19. $this->CI->load->config( 'table_torch' );
  20. define( 'PARAM_DILEM', $this->dilem );
  21. $this->CI->load->library( array( 'table', 'pagination', 'security' ));
  22. $this->load_prefix = dirname(__DIR__).DIRECTORY_SEPARATOR;
  23. //'sparks/table_torch/'.$this->CI->config->item( 'table_torch_version' ) .'/';
  24. $this->org_path = $this->CI->load->_ci_view_path;
  25. $this->CI->load->database();
  26. $this->CI->table->set_template( $this->CI->config->item( 'table_torch_table_formatting') );
  27. }
  28. function route(){
  29. $this->_disect_url();
  30. $redirect = FALSE;
  31. if( empty( $this->url_vals[ 'table' ] )){
  32. $tables = $this->CI->config->item( 'table_torch_tables' );
  33. $this->url_vals[ 'table' ] = key( $tables );
  34. $this->url_vals[ 'action' ] = 'listing';
  35. $redirect = TRUE;
  36. }
  37. define( 'TORCH_METHOD', $this->url_vals[ 'action' ] );
  38. define( 'TORCH_TABLE', $this->url_vals[ 'table' ] );
  39. define( 'TORCH_KEY', $this->url_vals[ 'key' ] );
  40. $this->_check_table();
  41. $this->CI->table_torch_model->define_primary_key();
  42. if( $redirect ){
  43. redirect( torch_url( $this->url_vals, FALSE ));
  44. }else{
  45. $method = TORCH_METHOD;
  46. $this->$method();
  47. }
  48. }
  49. function listing(){
  50. $config = $this->CI->config->item( 'table_torch_pagination_settings' );
  51. $config['base_url'] = torch_url( $this->url_vals );
  52. $config['total_rows'] = $this->CI->table_torch_model->get_count( $this->url_vals );
  53. $config['uri_segment' ] = $this->CI->uri->total_segments();
  54. $this->CI->pagination->initialize($config);
  55. $data[ 'table' ] = TORCH_TABLE;
  56. $data[ 'total_count' ] = $config[ 'total_rows' ];
  57. $data[ 'rows' ] = $this->_table_data( $this->CI->uri->segment( $config[ 'uri_segment'] ) );
  58. $data[ 'tables' ] = $this->CI->config->item( 'table_torch_torch_tables' );
  59. $data[ 'url_params' ] = $this->url_vals;
  60. $data[ 'options' ] = $this->_field_options();
  61. $tbl_settings = $data[ 'tables' ][ TORCH_TABLE ];
  62. if( isset( $tbl_settings[ 'add' ] )){
  63. $data[ 'add' ] = $tbl_settings[ 'add' ];
  64. }else{
  65. $data[ 'add' ] = FALSE;
  66. }
  67. if( method_exists( $this->CI, TORCH_TABLE."_listing" )){
  68. $method = TORCH_TABLE."_listing";
  69. $this->CI->$method( $data );
  70. }else{
  71. $this->load_view( 'listing', $data, TRUE );
  72. }
  73. }
  74. function add(){
  75. $data[ 'tables' ] = $this->CI->config->item( 'table_torch_tables' );
  76. $data[ 'table' ] = TORCH_TABLE;
  77. $data[ 'desc' ] = $this->CI->table_torch_model->describe_table();
  78. $data[ 'row' ] = NULL;
  79. if( method_exists( $this->CI, TORCH_TABLE."_add" )){
  80. $method = TORCH_TABLE."_add";
  81. $this->CI->$method( $data );
  82. }else{
  83. $this->load_view( 'form', $data, TRUE );
  84. }
  85. }
  86. function edit(){
  87. $data[ 'desc' ] = $this->CI->table_torch_model->describe_table();
  88. $data[ 'table' ] = TORCH_TABLE;
  89. $data[ 'row' ] = $this->CI->table_torch_model->get_by_key();
  90. $data[ 'tables' ] = $this->CI->config->item( 'table_torch_tables' );
  91. if( method_exists( $this->CI, TORCH_TABLE."_edit" )){
  92. $method = TORCH_TABLE."_edit";
  93. $this->CI->$method( $data );
  94. }else{
  95. $this->load_view( 'form', $data, TRUE );
  96. }
  97. }
  98. /// FORM ACTIONS /////
  99. function insert(){
  100. $data = $this->CI->table_torch_model->prep_data();
  101. if( method_exists( $this->CI, 'before_insert' ) ){
  102. $data = $this->CI->before_insert( TORCH_TABLE, $data );
  103. }
  104. $data[ 'id' ] = $this->CI->table_torch_model->insert( $data );
  105. if( method_exists( $this->CI, 'after_insert' ) ){
  106. $this->CI->after_insert( TORCH_TABLE, $data );
  107. }
  108. if( isset($_POST[ 'refer'])){
  109. redirect( $_POST[ 'refer' ] );
  110. }else{
  111. redirect( torch_url( array( 'table'=>TORCH_TABLE, 'action'=>'listing' ), FALSE ) );
  112. }
  113. }
  114. function search(){
  115. foreach ($this->url_vals as $key => $value) {
  116. if( isset( $_POST[ $key ] )){
  117. $this->url_vals[ $key ] = $_POST[ $key ];
  118. }
  119. }
  120. redirect( torch_url( $this->url_vals, FALSE ) );
  121. }
  122. function delete(){
  123. if( method_exists( $this->CI, 'before_delete' ) ){
  124. $this->CI->before_delete( TORCH_TABLE, TORCH_KEY );
  125. }
  126. $this->CI->table_torch_model->delete();
  127. if( method_exists( $this->CI, 'after_delete' ) ){
  128. $this->CI->after_delete( TORCH_TABLE, TORCH_KEY );
  129. }
  130. redirect( torch_url( array( 'table'=>TORCH_TABLE, 'action'=>'listing' ), FALSE ) );
  131. }
  132. function update(){
  133. $data = $this->CI->table_torch_model->prep_data();
  134. if( method_exists( $this->CI, 'before_update' ) ){
  135. $d = $data;
  136. $d[ PRIMARY_KEY ] = $_POST[ PRIMARY_KEY ];
  137. $data = $this->CI->before_update( TORCH_TABLE, $d );
  138. unset( $data[ PRIMARY_KEY ] );
  139. }
  140. $this->CI->table_torch_model->update( $data );
  141. if( method_exists( $this->CI, 'after_update' ) ){
  142. $this->CI->after_update( TORCH_TABLE, $d );
  143. }
  144. if( isset($_POST[ 'refer'])){
  145. redirect( $_POST[ 'refer' ] );
  146. }else{
  147. redirect( torch_url( array( 'table'=>TORCH_TABLE, 'action'=>'listing' ), FALSE ) );
  148. }
  149. }
  150. public function load_view( $view_file, $data, $torch_view_dir=FALSE ){
  151. if( $torch_view_dir ){
  152. $this->CI->load->_ci_view_path = $this->load_prefix .'views/';
  153. }else{
  154. $this->CI->load->_ci_view_path = $this->org_path;
  155. }
  156. $data[ 'contents' ] = $this->CI->load->view( $view_file, $data, TRUE );
  157. $in_dir = $this->CI->config->item( 'table_torch_template_in_torch_dir' );
  158. if( $in_dir ){
  159. $this->CI->load->_ci_view_path = $this->load_prefix .'views/';
  160. }else{
  161. $this->CI->load->_ci_view_path = $this->org_path;
  162. }
  163. $this->CI->load->view( $this->CI->config->item( 'table_torch_template_file' ), $data );
  164. }
  165. protected function _check_table(){
  166. if( TORCH_TABLE == null ){
  167. show_error( $this->CI->lang->line( 'table_torch_table_not_specified' ) );
  168. }
  169. $tables = $this->CI->config->item( 'table_torch_tables' );
  170. if( !isset($tables[ TORCH_TABLE ])){
  171. show_error( $this->CI->lang->line( 'table_torch_table_not_in_config' ) );
  172. }
  173. }
  174. protected function _table_data( $offset=0 ){
  175. $tables = $this->CI->config->item( 'table_torch_tables' );
  176. $prefs = $tables[ TORCH_TABLE ];
  177. $humanize = $this->CI->config->item( 'table_torch_humanize_fields' );
  178. $paginate_prefs = $this->CI->config->item( 'table_torch_pagination_settings' );
  179. $limit = $paginate_prefs[ 'per_page' ];
  180. $funct = $this->CI->config->item( 'table_torch_function' );
  181. $rows = $this->CI->table_torch_model->get_listing( $this->url_vals, $limit, $offset );
  182. for ($i=0; $i < count( $rows ); $i++) {
  183. $row = $rows[ $i ];
  184. $actions = '';
  185. if( $prefs[ 'edit' ] ){
  186. $actions .= anchor( torch_url( array( 'action'=>'edit',
  187. 'table'=>TORCH_TABLE,
  188. 'key'=>$row[ PRIMARY_KEY ] ), FALSE ),
  189. 'Edit', array( 'class'=>'actionLink', 'id'=>'editLink') );
  190. }
  191. if( $prefs[ 'delete' ] ){
  192. $confirm = $this->CI->lang->line( 'table_torch_delete_confirm' );
  193. $actions .= anchor( torch_url( array( 'action'=>'delete',
  194. 'table'=>TORCH_TABLE,
  195. 'key'=>$row[ PRIMARY_KEY ]), FALSE ),
  196. 'Delete', array( 'onclick'=>"return confirm('$confirm')", 'class'=>'actionLink' ) );
  197. }
  198. $tmp[ 'actions' ] = $actions;
  199. foreach ($rows[ $i ] as $key => $value ){
  200. if( !empty( $prefs[ 'formats' ][ $key ])) {
  201. $value = $prefs[ 'formats' ][ $key ]( $value, $key, TORCH_TABLE );
  202. } elseif( !empty( $funct )){
  203. $value = $funct( $value );
  204. }
  205. $tmp[ $key ] = $value;
  206. }
  207. $rows[ $i ] = $tmp;
  208. }
  209. $headers[ 0 ] = $this->CI->lang->line( 'table_torch_actions' );
  210. $desc = $this->CI->table_torch_model->describe_table();
  211. $org_vals = $this->url_vals;
  212. foreach ($desc as $row ){
  213. $class = '';
  214. $prefix = '';
  215. if( $org_vals[ 'sort_field' ] == $row[ 'Field' ] ){
  216. if( $org_vals[ 'sort_dir' ] == 'ASC' ){
  217. $class = 'desc';
  218. $prefix = '&#9660;&nbsp;';
  219. $this->url_vals[ 'sort_dir' ] = 'DESC';
  220. }else{
  221. $class = 'asc';
  222. $prefix = '&#9650;&nbsp;';
  223. $this->url_vals[ 'sort_dir' ] = 'ASC';
  224. }
  225. } else{
  226. $this->url_vals[ 'sort_dir' ] = 'ASC';
  227. }
  228. $this->url_vals[ 'sort_field' ] = $row[ 'Field' ];
  229. $fieldname = $row[ 'Field' ];
  230. if( $humanize ){
  231. $fieldname = humanize( $fieldname );
  232. }
  233. array_push( $headers, anchor( torch_url( $this->url_vals, FALSE ), $prefix . $fieldname, array( 'class'=>$class )));
  234. }
  235. $this->url_vals = $org_vals;
  236. $this->CI->table->set_heading( $headers );
  237. return $rows;
  238. }
  239. protected function _field_options(){
  240. $fields = $this->CI->table_torch_model->describe_table();
  241. $options = array();
  242. foreach ($fields as $field ) {
  243. $options[ $field[ 'Field' ] ] = $field[ 'Field' ];
  244. }
  245. return $options;
  246. }
  247. protected function _disect_url(){
  248. $segs = $this->CI->uri->segment_array();
  249. foreach( $segs as $segment){
  250. if( strpos( $segment, PARAM_DILEM ) !== FALSE ){
  251. $arr = explode( PARAM_DILEM, $segment );
  252. $this->url_vals[ $arr[ 0 ] ] = $arr[ 1 ];
  253. }
  254. }
  255. }
  256. }