Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. if (!class_exists('WP_List_Table'))
  2.  
  3. public function no_items()
  4. {
  5. _e('Unfortunately there are no ingredients available :(', 'wp-my-recipe');
  6. }
  7. /**
  8. * Prepare the items for the table to process
  9. *
  10. * @return Void
  11. */
  12. public function prepare_items($search = NULL)
  13. {
  14. $data = $this->table_data($search);
  15. $columns = $this->get_columns();
  16. $hidden = $this->get_hidden_columns();
  17. $sortable = $this->get_sortable_columns();
  18. $perPage = 1;
  19. $currentPage = $this->get_pagenum();
  20. if (!is_null($data))
  21. {
  22. $totalItems = count($data);
  23. $data = array_slice($data, (($currentPage - 1) * $perPage) , $perPage);
  24. }
  25. else
  26. {
  27. $totalItems = 0;
  28. }
  29. $this->set_pagination_args(array(
  30. 'total_items' => $totalItems,
  31. 'per_page' => $perPage
  32. ));
  33. $this->_column_headers = array(
  34. $columns,
  35. $hidden,
  36. $sortable
  37. );
  38. $this->items = $this->table_data($search);
  39. }
  40. /**
  41. * Override the parent columns method. Defines the columns to use in your listing table
  42. *
  43. * @return Array
  44. */
  45. public function get_columns()
  46. {
  47. $columns = array(
  48. 'cb' => '<input type="checkbox" />', // to display the checkbox.
  49. 'id' => 'ID',
  50. 'name' => __('Ingredient name', 'wp-my-recipe') ,
  51. 'kcalories' => __('Kilocalories per 100 ml/mg', 'wp-my-recipe') ,
  52. 'reference' => __('Reference value', 'wp-my-recipe')
  53. );
  54. return $columns;
  55. }
  56. /**
  57. * Define which columns are hidden
  58. *
  59. * @return Array
  60. */
  61. public function get_hidden_columns()
  62. {
  63. return array(
  64. 'id'
  65. );
  66. }
  67. /**
  68. * Define the sortable columns
  69. *
  70. * @return Array
  71. */
  72. public function get_sortable_columns()
  73. {
  74. return array(
  75. 'name' => array(
  76. 'name',
  77. false
  78. ) ,
  79. 'kcalories' => array(
  80. 'kcalories',
  81. false
  82. ) ,
  83. 'reference' => array(
  84. 'reference',
  85. false
  86. ) ,
  87. );
  88. }
  89. /**
  90. * Get the table data
  91. *
  92. * @return Array
  93. */
  94. function table_data($search = NULL)
  95. {
  96.  
  97. //todo add real search
  98. if ($search != NULL)
  99. {
  100. $search = trim($search);
  101. $data = array(
  102. ['id' => 'dummyData',
  103. 'name' => $search,
  104. 'kcalories' => 'dummyData',
  105. 'reference' => 'dummyData']
  106. );
  107. }
  108. else
  109. {
  110. $data = s_getAllIngredients();
  111. }
  112.  
  113. return $data;
  114. }
  115. /**
  116. * Define what data to show on each column of the table
  117. *
  118. * @param Array $item Data
  119. * @param String $column_name - Current column name
  120. *
  121. * @return Mixed
  122. */
  123. public function column_default($item, $column_name)
  124. {
  125. switch ($column_name)
  126. {
  127. case 'id':
  128. case 'name':
  129. case 'kcalories':
  130. case 'reference':
  131. return $item[$column_name];
  132. default:
  133. return print_r($item, true);
  134. }
  135. }
  136. // /**
  137. // * Allows you to sort the data by the variables set in the $_GET
  138. // *
  139. // * @return Mixed
  140. // */
  141. private function sort_data($a, $b)
  142. {
  143.  
  144. // Set defaults
  145. $orderby = 'name';
  146. $order = 'asc';
  147. // If orderby is set, use this as the sort column
  148. if (!empty($_GET['orderby']))
  149. {
  150. $orderby = $_GET['orderby'];
  151. }
  152. // If order is set use this as the order
  153. if (!empty($_GET['order']))
  154. {
  155. $order = $_GET['order'];
  156. }
  157. $result = strcmp($a[$orderby], $b[$orderby]);
  158. if ($order === 'asc')
  159. {
  160. return $result;
  161. }
  162. return -$result;
  163.  
  164. }
  165.  
  166. protected function column_cb($item)
  167. {
  168. return sprintf('<label class="screen-reader-text" for="ingredient_' . $item['id'] . '">' . sprintf(__('Select %s') , $item['name']) . '</label>' . "<input type='checkbox' name='ingredient[]' id='ingredient_{$item['id']}' value='{$item['id']}' />");
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement