You are here

function support_pm_user_week_submit in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support_pm/support_pm.module \support_pm_user_week_submit()

Save user's week plan.

File

support_pm/support_pm.module, line 629
Support Project Management. @author Jeremy Andrews <jeremy@tag1consulting.com> @package Support

Code

function support_pm_user_week_submit($form, &$form_state) {
  $week = isset($_GET['week']) ? _support_pm_first_day((int) $_GET['week']) : _support_pm_first_day(time());
  $dates = _support_pm_dates($week);
  $dates['totals'] = t('Totals');
  $user = user_load(array(
    'uid' => $form_state['values']['uid'],
  ));
  $clients = _support_available_clients($user);
  foreach ($form_state['values'] as $key => $value) {
    $key = explode(':', $key);
    $clid = $key[0];
    $day = $key[1];
    if (!empty($day)) {
      if (isset($clients[$clid]) && $day != 'totals') {
        if ($day == 'comment') {
          db_query("UPDATE {support_plan} SET comment = '%s' WHERE clid = '%s' AND uid = %d AND day = %d", $value, $clid, $user->uid, $week);
        }
        else {
          db_query("UPDATE {support_plan} SET hours = %f WHERE clid = '%s' AND uid = %d AND day = %d", $value, $clid, $user->uid, $day);
        }

        // If we affected 0 rows, we are probably creating a new plan
        if (!db_affected_rows()) {

          // We must create new rows to store the plan.
          if ($day == 'comment') {

            // We may not have changed this row, but it may already exist
            $exists = db_result(db_query("SELECT clid FROM {support_plan} WHERE clid = '%s' AND uid = %d AND day = %d", $clid, $user->uid, $week));
            if (!$exists) {
              db_query("INSERT INTO {support_plan} (comment, clid, uid, day) VALUES ('%s', '%s', %d, %d)", $value, $clid, $user->uid, $week);
            }
          }
          else {

            // We may not have changed this row, but it may already exist
            $exists = db_result(db_query("SELECT clid FROM {support_plan} WHERE clid = '%s' AND uid = %d AND day = %d", $clid, $user->uid, $day));
            if (!$exists) {
              db_query("INSERT INTO {support_plan} (hours, clid, uid, day) VALUES (%f, '%s', %d, %d)", $value, $clid, $user->uid, $day);
            }
          }
        }
      }
    }
  }
  drupal_goto("user/{$user->uid}/support_pm", "week={$week}");
}