You are here

function support_reference_block_form_submit in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support_reference/support_reference.module \support_reference_block_form_submit()

File

support_reference/support_reference.module, line 127
Support ticket references.

Code

function support_reference_block_form_submit($form, &$form_state) {
  $nid = $form['#nid'];
  $node = node_load($nid);
  $client = support_client_load($node->client);

  // Make sure the user is allowed to do this.
  if (user_access('edit support ticket references') && support_access_clients($client)) {
    $match = array();
    if (preg_match_all('/\\[#(\\d+)\\]/', $form_state['values']['reference'], $match)) {
      foreach ($match[1] as $rnid) {
        $rnode = node_load($rnid);
        $rclient = support_client_load($rnode->client);

        // Additional access control check to ignore messing with any references
        // to tickets belonging to clients that the user doesn't have access to.
        if (support_access_clients($rclient)) {

          // If it already exists, delete instead.
          if (db_query('SELECT 1 FROM {support_reference} WHERE nid = :nid AND rnid = :rnid', array(
            ':nid' => $nid,
            ':rnid' => $rnid,
          ))
            ->fetchField()) {
            db_delete('support_reference')
              ->condition('nid', $nid)
              ->condition('rnid', $rnid)
              ->execute();
            db_delete('support_reference')
              ->condition('nid', $rnid)
              ->condition('rnid', $nid)
              ->execute();
          }
          else {
            db_insert('support_reference')
              ->fields(array(
              'nid' => $nid,
              'rnid' => $rnid,
            ))
              ->execute();
            db_insert('support_reference')
              ->fields(array(
              'nid' => $rnid,
              'rnid' => $nid,
            ))
              ->execute();
          }
        }
      }
    }
  }
}