You are here

function mailhandler_node_process_message_commands in Mailhandler 6

Same name and namespace in other branches
  1. 7 mailhandler.module \mailhandler_node_process_message_commands()
2 calls to mailhandler_node_process_message_commands()
MailhandlerProcessingTestCase::testMailhandlerCommandsParse in ./mailhandler.test
Verify the commands parsing functionality.
mailhandler_node_prepare_message in ./mailhandler.module
Append default commands. Separate commands from body. Strip signature. Return a node object.

File

./mailhandler.module, line 1034
Mailhandler module code.

Code

function mailhandler_node_process_message_commands(&$node, $commands) {
  foreach ($commands as $data) {

    // TODO: allow for nested arrays in commands ... Possibly trim() values after explode().
    // If needed, turn this command value into an array
    if (substr($data[1], 0, 1) == '[' && substr($data[1], -1, 1) == ']') {
      $data[1] = rtrim(ltrim($data[1], '['), ']');

      //strip brackets
      $data[1] = explode(",", $data[1]);

      // allow for key value pairs
      foreach ($data[1] as $key => $value) {
        $data_tmp = explode(":", $value, 2);
        if (isset($data_tmp[1])) {

          // is it a key value pair?
          // remove old, add as key value pair
          unset($data[1][$key]);
          $data_tmp[0] = trim($data_tmp[0]);
          $data[1][$data_tmp[0]] = $data_tmp[1];
        }
      }
    }
    $data[0] = strtolower(str_replace(' ', '_', $data[0]));

    // Taxonomy specific command handling.
    if (($data[0] == 'taxonomy' || $data[0] == 'taxonomy') && module_exists('taxonomy')) {
      $vocabs = taxonomy_get_vocabularies($node->type);
      $node->taxonomy = array();

      // if needed, map term names into IDs. this should move to taxonomy_mailhandler()
      if ($data[0] == 'taxonomy' && !is_numeric($data[1][0])) {
        array_walk($data[1], 'mailhandler_term_map');

        // Only add term if node type can use that term's vocab
        $term = taxonomy_get_term($data[1][0]);
        if (array_key_exists($term->vid, $vocabs)) {
          $node->taxonomy = array_merge($node->taxonomy, $data[1]);
        }
        unset($data[0]);
      }
      else {
        if (substr($data[0], 0, 9) == 'taxonomy[' && substr($data[0], -1, 1) == ']') {

          // make sure a valid vid is passed in:
          $vid = substr($data[0], 9, -1);
          $vocabulary = taxonomy_vocabulary_load($vid);

          // if the vocabulary is not activated for that node type, unset $data[0], so the command will be ommited from $node
          // TODO: add an error message
          if (!in_array($node->type, $vocabulary->nodes)) {
            unset($data[0]);
          }
          else {
            if (!$vocabulary->tags) {
              array_walk($data[1], 'mailhandler_term_map', $vid);
              $node->taxonomy = array_merge($node->taxonomy, $data[1]);
              unset($data[0]);
            }
            else {
              if ($vocabulary->tags) {

                // for freetagging vocabularies, we just pass the list of terms
                $node->taxonomy['tags'][$vid] = implode(',', $data[1]);
                unset($data[0]);

                // unset, so it won't be included when populating the node object
              }
            }
          }
        }
      }
    }
    if (!empty($data[0])) {
      $node->{$data}[0] = $data[1];
    }
  }
}