Advertisement
Guest User

Untitled

a guest
May 11th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. function bizonoff_discount_category_edit_form($form, &$form_state) {
  2.  
  3. // сохраним ключи родительских элементов
  4. $form['#tree'] = TRUE;
  5.  
  6.  
  7. //создадим кнопку для пересчета скидки
  8. $form['calculate_discounts'] = array(
  9. '#type' => 'button',
  10. '#value' => t('Пересчитать скидки'),
  11. '#ajax' => array(
  12. 'callback' => '',
  13. 'wrapper' => '',
  14. 'method' => 'replace',
  15. 'effect' => 'fade',
  16. ),
  17. );
  18.  
  19. //создадим контейнеры для данных
  20. $form['active_discounts'] = array(
  21. '#type' => 'fieldset',
  22. '#collapsible' => TRUE,
  23. '#title' => 'Действующие скидки',
  24. '#prefix' => '<div id="active-discounts">',
  25. '#suffix' => '</div>',
  26. '#theme' => 'bizonoff_discount_category_active_discounts_table',
  27. );
  28.  
  29. $form['disabled_discounts'] = array(
  30. '#type' => 'fieldset',
  31. '#collapsible' => TRUE,
  32. '#title' => 'Отключенные скидки',
  33. '#prefix' => '<div id="disabled-discounts">',
  34. '#suffix' => '</div>',
  35. '#theme' => 'bizonoff_discount_category_active_discounts_table',
  36. );
  37.  
  38. //создадим шапки для наших таблиц
  39. $form['active_discounts']['header'] = array(
  40. '#type' => 'value',
  41. '#value' => array('ID', 'Приоритет', 'Размер, %', 'Начало действия', 'Конец действия', 'Управление товарами', 'Удаление'),
  42. );
  43. $form['disabled_discounts']['header'] = array(
  44. '#type' => 'value',
  45. '#value' => array('ID', 'Приоритет', 'Размер, %', 'Начало действия', 'Конец действия', 'Управление товарами', 'Удаление'),
  46. );
  47.  
  48. //получим данные о скидках
  49. $discounts = db_select('bizonoff_categories_form', 'n')
  50. ->fields('n', array('id', 'priority', 'value', 'begin', 'end', 'product_id'))
  51. ->execute()
  52. ->fetchAll();
  53.  
  54. //разделим скидки на активные и пассивные
  55. $active_discounts = array();
  56. $disabled_discounts = array();
  57. foreach ($discounts as $discount_key => $discount) {
  58. $current_time = time();
  59. if ($current_time > $discount->begin && $current_time < $discount->end) {
  60. $active_discounts[] = $discount;
  61. }
  62. else {
  63. $disabled_discounts[] = $discount;
  64. }
  65. }
  66.  
  67. //сгенерируем поля для действующих скидок
  68. foreach ($active_discounts as $discount_key => $discount) {
  69.  
  70. $form['active_discounts']['data']['id_' . $discount->id]['id'] = array(
  71. '#type' => 'item',
  72. '#markup' => $discount->id,
  73. );
  74. $form['active_discounts']['data']['id_' . $discount->id]['priority'] = array(
  75. '#type' => 'textfield',
  76. '#default_value' => $discount->priority,
  77. '#size' => 10,
  78. '#maxlength' => 10,
  79. );
  80. $form['active_discounts']['data']['id_' . $discount->id]['value'] = array(
  81. '#type' => 'textfield',
  82. '#default_value' => $discount->value,
  83. '#size' => 10,
  84. '#maxlength' => 10,
  85. );
  86.  
  87. //получим текущую дату
  88. $date_start = array(
  89. 'day' => format_date($discount->begin, 'custom', 'j'),
  90. 'month' => format_date($discount->begin, 'custom', 'n'),
  91. 'year' => format_date($discount->begin, 'custom', 'Y'),
  92. );
  93. $date_end = array(
  94. 'day' => format_date($discount->end, 'custom', 'j'),
  95. 'month' => format_date($discount->end, 'custom', 'n'),
  96. 'year' => format_date($discount->end, 'custom', 'Y'),
  97. );
  98.  
  99. $form['active_discounts']['data']['id_' . $discount->id]['start'] = array(
  100. '#type' => 'date',
  101. '#default_value' => $date_start,
  102. );
  103.  
  104. $form['active_discounts']['data']['id_' . $discount->id]['end'] = array(
  105. '#type' => 'date',
  106. '#default_value' => $date_end,
  107. );
  108.  
  109. $form['active_discounts']['data']['id_' . $discount->id]['change_products'] = array(
  110. '#type' => 'button',
  111. '#value' => t('Управление товарами'),
  112. '#ajax' => array(
  113. 'callback' => '',
  114. 'wrapper' => '',
  115. 'method' => 'replace',
  116. 'effect' => 'fade',
  117. ),
  118. );
  119. $form['active_discounts']['data']['id_' . $discount->id]['delete'] = array(
  120. '#type' => 'button',
  121. '#value' => t('Удалить'),
  122. '#ajax' => array(
  123. 'callback' => '',
  124. 'wrapper' => '',
  125. 'method' => 'replace',
  126. 'effect' => 'fade',
  127. ),
  128. );
  129. }
  130.  
  131. //сгенерируем поля для не действующих скидок
  132. foreach ($disabled_discounts as $discount_key => $discount) {
  133.  
  134. $form['disabled_discounts']['data']['id_' . $discount->id]['id'] = array(
  135. '#type' => 'item',
  136. '#markup' => $discount->id,
  137. );
  138. $form['disabled_discounts']['data']['id_' . $discount->id]['priority'] = array(
  139. '#type' => 'textfield',
  140. '#default_value' => $discount->priority,
  141. '#size' => 10,
  142. '#maxlength' => 10,
  143. );
  144. $form['disabled_discounts']['data']['id_' . $discount->id]['value'] = array(
  145. '#type' => 'textfield',
  146. '#default_value' => $discount->value,
  147. '#size' => 10,
  148. '#maxlength' => 10,
  149. );
  150.  
  151. //получим текущую дату
  152. $date_start = array(
  153. 'day' => format_date($discount->begin, 'custom', 'j'),
  154. 'month' => format_date($discount->begin, 'custom', 'n'),
  155. 'year' => format_date($discount->begin, 'custom', 'Y'),
  156. );
  157. $date_end = array(
  158. 'day' => format_date($discount->end, 'custom', 'j'),
  159. 'month' => format_date($discount->end, 'custom', 'n'),
  160. 'year' => format_date($discount->end, 'custom', 'Y'),
  161. );
  162.  
  163. $form['disabled_discounts']['data']['id_' . $discount->id]['start'] = array(
  164. '#type' => 'date',
  165. '#default_value' => $date_start,
  166. );
  167.  
  168. $form['disabled_discounts']['data']['id_' . $discount->id]['end'] = array(
  169. '#type' => 'date',
  170. '#default_value' => $date_end,
  171. );
  172.  
  173. $form['disabled_discounts']['data']['id_' . $discount->id]['change_products'] = array(
  174. '#type' => 'button',
  175. '#value' => t('Управление товарами'),
  176. '#ajax' => array(
  177. 'callback' => '',
  178. 'wrapper' => '',
  179. 'method' => 'replace',
  180. 'effect' => 'fade',
  181. ),
  182. );
  183. $form['disabled_discounts']['data']['id_' . $discount->id]['delete'] = array(
  184. '#type' => 'button',
  185. '#value' => t('Удалить'),
  186. '#ajax' => array(
  187. 'callback' => '',
  188. 'wrapper' => '',
  189. 'method' => 'replace',
  190. 'effect' => 'fade',
  191. ),
  192. );
  193. }
  194.  
  195. return $form;
  196. }
  197.  
  198. function bizonoff_discount_theme() {
  199. return array(
  200. 'bizonoff_discount_category_active_discounts_table' => array(
  201. 'render element' => 'form',
  202. ),
  203. );
  204. }
  205.  
  206. function theme_bizonoff_discount_category_active_discounts_table($vars) {
  207. $form = $vars['form'];
  208. $rows = array();
  209.  
  210. foreach (element_children($form['data']) as $key) {
  211. foreach (element_children($form['data'][$key]) as $name) {
  212. $rows[$key][] = drupal_render($form['data'][$key][$name]);
  213. }
  214. }
  215.  
  216. return theme('table', array(
  217. 'header' => $form['header']['#value'],
  218. 'rows' => $rows,
  219. ));
  220. return;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement