Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bizonoff_discount_category_edit_form($form, &$form_state) {
- // сохраним ключи родительских элементов
- $form['#tree'] = TRUE;
- //создадим кнопку для пересчета скидки
- $form['calculate_discounts'] = array(
- '#type' => 'button',
- '#value' => t('Пересчитать скидки'),
- '#ajax' => array(
- 'callback' => '',
- 'wrapper' => '',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- //создадим контейнеры для данных
- $form['active_discounts'] = array(
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#title' => 'Действующие скидки',
- '#prefix' => '<div id="active-discounts">',
- '#suffix' => '</div>',
- '#theme' => 'bizonoff_discount_category_active_discounts_table',
- );
- $form['disabled_discounts'] = array(
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#title' => 'Отключенные скидки',
- '#prefix' => '<div id="disabled-discounts">',
- '#suffix' => '</div>',
- '#theme' => 'bizonoff_discount_category_active_discounts_table',
- );
- //создадим шапки для наших таблиц
- $form['active_discounts']['header'] = array(
- '#type' => 'value',
- '#value' => array('ID', 'Приоритет', 'Размер, %', 'Начало действия', 'Конец действия', 'Управление товарами', 'Удаление'),
- );
- $form['disabled_discounts']['header'] = array(
- '#type' => 'value',
- '#value' => array('ID', 'Приоритет', 'Размер, %', 'Начало действия', 'Конец действия', 'Управление товарами', 'Удаление'),
- );
- //получим данные о скидках
- $discounts = db_select('bizonoff_categories_form', 'n')
- ->fields('n', array('id', 'priority', 'value', 'begin', 'end', 'product_id'))
- ->execute()
- ->fetchAll();
- //разделим скидки на активные и пассивные
- $active_discounts = array();
- $disabled_discounts = array();
- foreach ($discounts as $discount_key => $discount) {
- $current_time = time();
- if ($current_time > $discount->begin && $current_time < $discount->end) {
- $active_discounts[] = $discount;
- }
- else {
- $disabled_discounts[] = $discount;
- }
- }
- //сгенерируем поля для действующих скидок
- foreach ($active_discounts as $discount_key => $discount) {
- $form['active_discounts']['data']['id_' . $discount->id]['id'] = array(
- '#type' => 'item',
- '#markup' => $discount->id,
- );
- $form['active_discounts']['data']['id_' . $discount->id]['priority'] = array(
- '#type' => 'textfield',
- '#default_value' => $discount->priority,
- '#size' => 10,
- '#maxlength' => 10,
- );
- $form['active_discounts']['data']['id_' . $discount->id]['value'] = array(
- '#type' => 'textfield',
- '#default_value' => $discount->value,
- '#size' => 10,
- '#maxlength' => 10,
- );
- //получим текущую дату
- $date_start = array(
- 'day' => format_date($discount->begin, 'custom', 'j'),
- 'month' => format_date($discount->begin, 'custom', 'n'),
- 'year' => format_date($discount->begin, 'custom', 'Y'),
- );
- $date_end = array(
- 'day' => format_date($discount->end, 'custom', 'j'),
- 'month' => format_date($discount->end, 'custom', 'n'),
- 'year' => format_date($discount->end, 'custom', 'Y'),
- );
- $form['active_discounts']['data']['id_' . $discount->id]['start'] = array(
- '#type' => 'date',
- '#default_value' => $date_start,
- );
- $form['active_discounts']['data']['id_' . $discount->id]['end'] = array(
- '#type' => 'date',
- '#default_value' => $date_end,
- );
- $form['active_discounts']['data']['id_' . $discount->id]['change_products'] = array(
- '#type' => 'button',
- '#value' => t('Управление товарами'),
- '#ajax' => array(
- 'callback' => '',
- 'wrapper' => '',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- $form['active_discounts']['data']['id_' . $discount->id]['delete'] = array(
- '#type' => 'button',
- '#value' => t('Удалить'),
- '#ajax' => array(
- 'callback' => '',
- 'wrapper' => '',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- }
- //сгенерируем поля для не действующих скидок
- foreach ($disabled_discounts as $discount_key => $discount) {
- $form['disabled_discounts']['data']['id_' . $discount->id]['id'] = array(
- '#type' => 'item',
- '#markup' => $discount->id,
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['priority'] = array(
- '#type' => 'textfield',
- '#default_value' => $discount->priority,
- '#size' => 10,
- '#maxlength' => 10,
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['value'] = array(
- '#type' => 'textfield',
- '#default_value' => $discount->value,
- '#size' => 10,
- '#maxlength' => 10,
- );
- //получим текущую дату
- $date_start = array(
- 'day' => format_date($discount->begin, 'custom', 'j'),
- 'month' => format_date($discount->begin, 'custom', 'n'),
- 'year' => format_date($discount->begin, 'custom', 'Y'),
- );
- $date_end = array(
- 'day' => format_date($discount->end, 'custom', 'j'),
- 'month' => format_date($discount->end, 'custom', 'n'),
- 'year' => format_date($discount->end, 'custom', 'Y'),
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['start'] = array(
- '#type' => 'date',
- '#default_value' => $date_start,
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['end'] = array(
- '#type' => 'date',
- '#default_value' => $date_end,
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['change_products'] = array(
- '#type' => 'button',
- '#value' => t('Управление товарами'),
- '#ajax' => array(
- 'callback' => '',
- 'wrapper' => '',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- $form['disabled_discounts']['data']['id_' . $discount->id]['delete'] = array(
- '#type' => 'button',
- '#value' => t('Удалить'),
- '#ajax' => array(
- 'callback' => '',
- 'wrapper' => '',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- }
- return $form;
- }
- function bizonoff_discount_theme() {
- return array(
- 'bizonoff_discount_category_active_discounts_table' => array(
- 'render element' => 'form',
- ),
- );
- }
- function theme_bizonoff_discount_category_active_discounts_table($vars) {
- $form = $vars['form'];
- $rows = array();
- foreach (element_children($form['data']) as $key) {
- foreach (element_children($form['data'][$key]) as $name) {
- $rows[$key][] = drupal_render($form['data'][$key][$name]);
- }
- }
- return theme('table', array(
- 'header' => $form['header']['#value'],
- 'rows' => $rows,
- ));
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement