123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /**
- * @file
- * Stock administration menu items.
- */
- /**
- * Form builder for stock settings form.
- *
- * @ingroup forms
- */
- function uc_stock_settings_form($form, &$form_state) {
- $form['uc_stock_threshold_notification'] = array(
- '#type' => 'checkbox',
- '#title' => t('Send email notification when stock level reaches its threshold value'),
- '#default_value' => variable_get('uc_stock_threshold_notification', FALSE),
- );
- $form['uc_stock_threshold_notification_recipients'] = array(
- '#type' => 'textfield',
- '#title' => t('Notification recipients'),
- '#default_value' => variable_get('uc_stock_threshold_notification_recipients', uc_store_email()),
- '#description' => t('The list of comma-separated email addresses that will receive the notification.'),
- );
- $form['uc_stock_threshold_notification_subject'] = array(
- '#type' => 'textfield',
- '#title' => t('Message subject'),
- '#default_value' => variable_get('uc_stock_threshold_notification_subject', uc_get_message('uc_stock_threshold_notification_subject')),
- );
- $form['uc_stock_threshold_notification_message'] = array(
- '#type' => 'textarea',
- '#title' => t('Message text'),
- '#default_value' => variable_get('uc_stock_threshold_notification_message', uc_get_message('uc_stock_threshold_notification_message')),
- '#description' => t('The message the user receives when the stock level reaches its threshold value.'),
- '#rows' => 10,
- );
- if (module_exists('token')) {
- $form['token_tree'] = array(
- '#markup' => theme('token_tree', array('token_types' => array('uc_order', 'uc_stock', 'node', 'site', 'store'))),
- );
- }
- return system_settings_form($form);
- }
- /**
- * Displays a stock report for products with stock tracking enabled.
- */
- function uc_stock_report() {
- $page_size = (isset($_GET['nopage'])) ? UC_REPORTS_MAX_RECORDS : variable_get('uc_reports_table_size', 30);
- $csv_rows = array();
- $rows = array();
- $header = array(
- array('data' => t('SKU'), 'field' => 'sku', 'sort' => 'asc'),
- array('data' => t('Product'), 'field' => 'title'),
- array('data' => t('Stock'), 'field' => 'stock'),
- array('data' => t('Threshold'), 'field' => 'threshold'),
- array('data' => t('Operations')),
- );
- $csv_rows[] = array(t('SKU'), t('Product'), t('Stock'), t('Threshold'));
- $query = db_select('uc_product_stock', 's')->extend('PagerDefault')->extend('TableSort')
- ->orderByHeader($header)
- ->limit($page_size)
- ->fields('s', array(
- 'nid',
- 'sku',
- 'stock',
- 'threshold',
- ));
- $query->leftJoin('node', 'n', 's.nid = n.nid');
- $query->addField('n', 'title');
- $query->condition('active', 1)
- ->condition('title', '', '<>');
- if (arg(4) == 'threshold') {
- $query->where('threshold >= stock');
- }
- $result = $query->execute();
- foreach ($result as $stock) {
- $op = array();
- if (user_access('administer product stock')) {
- $op[] = l(t('edit'), 'node/' . $stock->nid . '/edit/stock', $options = array('query' => array('destination' => 'admin/store/reports/stock')));
- }
- // Add the data to a table row for display.
- $rows[] = array(
- 'data' => array(
- array('data' => $stock->sku),
- array('data' => l($stock->title, 'node/' . $stock->nid)),
- array('data' => $stock->stock),
- array('data' => $stock->threshold),
- array('data' => implode(' ', $op)),
- ),
- 'class' => array(($stock->threshold >= $stock->stock) ? 'uc-stock-below-threshold' : 'uc-stock-above-threshold'),
- );
- // Add the data to the CSV contents for export.
- $csv_rows[] = array($stock->sku, $stock->title, $stock->stock, $stock->threshold);
- }
- module_load_include('inc', 'uc_reports', 'uc_reports.admin');
- $csv_data = uc_reports_store_csv('uc_stock', $csv_rows);
- $build['form'] = drupal_get_form('uc_stock_report_form');
- $build['report'] = array(
- '#theme' => 'table',
- '#header' => $header,
- '#rows' => $rows,
- '#attributes' => array('width' => '100%', 'class' => array('uc-stock-table')),
- );
- $build['pager'] = array(
- '#theme' => 'pager',
- );
- $build['links'] = array(
- '#prefix' => '<div class="uc-reports-links">',
- '#suffix' => '</div>',
- );
- $build['links']['export_csv'] = array(
- '#markup' => l(t('Export to CSV file'), 'admin/store/reports/getcsv/' . $csv_data['report'] . '/' . $csv_data['user']),
- '#suffix' => ' ',
- );
- if (isset($_GET['nopage'])) {
- $build['links']['toggle_pager'] = array(
- '#markup' => l(t('Show paged records'), 'admin/store/reports/stock'),
- );
- }
- else {
- $build['links']['toggle_pager'] = array(
- '#markup' => l(t('Show all records'), 'admin/store/reports/stock', array('query' => array('nopage' => '1'))),
- );
- }
- return $build;
- }
- /**
- * Form builder for stock report threshold filter.
- *
- * @see uc_stock_report_form_submit()
- * @ingroup forms
- */
- function uc_stock_report_form($form, &$form_state) {
- $form['threshold'] = array(
- '#type' => 'checkbox',
- '#title' => t('Only show SKUs that are below their threshold.'),
- '#default_value' => arg(4) == 'threshold' ? TRUE : FALSE,
- '#attributes' => array('onchange' => 'this.form.submit();'),
- );
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Update'),
- '#attributes' => array('style' => "display:none;"),
- );
- return $form;
- }
- /**
- * Form submission handler for uc_stock_report_form().
- *
- * @see uc_stock_report_form()
- */
- function uc_stock_report_form_submit($form, &$form_state) {
- if ($form_state['values']['threshold']) {
- drupal_goto('admin/store/reports/stock/threshold');
- }
- else {
- drupal_goto('admin/store/reports/stock');
- }
- }
- /**
- * Form builder for product stock edit form.
- *
- * @see uc_stock_edit_form_submit()
- * @see theme_uc_stock_edit_form()
- * @ingroup forms
- */
- function uc_stock_edit_form($form, &$form_state, $node) {
- drupal_set_title($node->title);
- $form['stock'] = array('#tree' => TRUE);
- $skus = uc_product_get_models($node->nid);
- // Remove 'Any'.
- unset($skus[NULL]);
- if (!$skus) {
- drupal_set_message(t('No SKU found.'), 'error');
- }
- else {
- foreach (array_values($skus) as $id => $sku) {
- $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchAssoc();
- $form['stock'][$id]['sku'] = array(
- '#type' => 'value',
- '#value' => $sku,
- );
- // Checkbox to mark this as active.
- $form['stock'][$id]['active'] = array(
- '#type' => 'checkbox',
- '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
- );
- // Sanitized version of the SKU for display.
- $form['stock'][$id]['display_sku'] = array(
- '#markup' => check_plain($sku),
- );
- // Textfield for entering the stock level.
- $form['stock'][$id]['stock'] = array(
- '#type' => 'textfield',
- '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0,
- '#maxlength' => 9,
- '#size' => 9,
- );
- // Textfield for entering the threshold level.
- $form['stock'][$id]['threshold'] = array(
- '#type' => 'textfield',
- '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0,
- '#maxlength' => 9,
- '#size' => 9,
- );
- }
- }
- $form['nid'] = array(
- '#type' => 'value',
- '#value' => $node->nid,
- );
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['save'] = array(
- '#type' => 'submit',
- '#value' => t('Save changes'),
- );
- return $form;
- }
- /**
- * Form submission handler for uc_stock_edit_form().
- *
- * @see uc_stock_edit_form()
- * @see theme_uc_stock_edit_form()
- */
- function uc_stock_edit_form_submit($form, &$form_state) {
- foreach (element_children($form_state['values']['stock']) as $id) {
- $stock = $form_state['values']['stock'][$id];
- db_merge('uc_product_stock')
- ->key(array('sku' => $stock['sku']))
- ->updateFields(array(
- 'active' => $stock['active'],
- 'stock' => $stock['stock'],
- 'threshold' => $stock['threshold'],
- ))
- ->insertFields(array(
- 'sku' => $stock['sku'],
- 'active' => $stock['active'],
- 'stock' => $stock['stock'],
- 'threshold' => $stock['threshold'],
- 'nid' => $form_state['values']['nid'],
- ))
- ->execute();
- }
- drupal_set_message(t('Stock settings saved.'));
- }
- /**
- * Returns HTML for uc_stock_edit_form().
- *
- * @param $variables
- * An associative array containing:
- * - form: A render element representing the form.
- *
- * @see uc_stock_edit_form()
- * @see uc_stock_edit_form_submit()
- * @ingroup themeable
- */
- function theme_uc_stock_edit_form($variables) {
- $form = $variables['form'];
- drupal_add_js('misc/tableselect.js');
- $header = array(
- array('data' => ' ' . t('Active'), 'class' => array('select-all')),
- array('data' => t('SKU')),
- array('data' => t('Stock')),
- array('data' => t('Threshold')),
- );
- $rows = array();
- foreach (element_children($form['stock']) as $id) {
- $rows[] = array(
- array('data' => drupal_render($form['stock'][$id]['active'])),
- array('data' => drupal_render($form['stock'][$id]['display_sku'])),
- array('data' => drupal_render($form['stock'][$id]['stock'])),
- array('data' => drupal_render($form['stock'][$id]['threshold'])),
- );
- }
- return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($form);
- }
|