array( 'file' => 'stormteam.theme.inc', 'arguments' => array('header', 'teams'), ), 'stormteam_view' => array( 'file' => 'stormteam.theme.inc', 'arguments' => array('node', 'teaser', 'page'), ), ); } /** * @function * Implementation of hook_views_api */ function stormteam_views_api() { return array( 'api' => 2, 'path' => drupal_get_path('module', 'stormteam'), ); } /** * @function * Implementation of hook_node_info */ function stormteam_node_info() { return array( 'stormteam' => array( 'name' => t('Team'), 'module' => 'stormteam', 'description' => t('A team for use within the Storm project management application'), 'title_label' => t('Team Name'), 'body_label' => t('Description'), ) ); } /** * @function * Implementation of hook_content_extra_fields */ function stormteam_content_extra_fields($type_name) { if ($type_name == 'stormteam') { return array( 'group1' => array('label' => 'Team members', 'weight' => -20), ); } } /** * @function * Implementation of hook_access */ function stormteam_access($op, $node, $account = NULL) { if (empty($account)) { global $user; $account = $user; } if (is_numeric($node)) $node = node_load($node); switch ($op) { case 'create': return user_access('Storm team: add'); case 'view': if (user_access('Storm team: view all')) { return TRUE; } else if (user_access('Storm team: view own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm team: view belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormperson_nid)) { return TRUE; } else if (user_access('Storm team: view belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormorganization_nid)) { return TRUE; } break; case 'update': if (user_access('Storm team: edit all')) { return TRUE; } else if (user_access('Storm team: edit own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm team: edit belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormperson_nid)) { return TRUE; } else if (user_access('Storm team: edit belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormorganization_nid)) { return TRUE; } break; case 'delete': if (user_access('Storm team: delete all')) { return TRUE; } else if (user_access('Storm team: delete own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm team: delete belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormperson_nid)) { return TRUE; } else if (user_access('Storm team: delete belonged') && stormteam_user_belongs_to_team($node->nid, $account->stormorganization_nid)) { return TRUE; } break; } return FALSE; } /** * @function * Function to add node access rules onto database queries */ function stormteam_access_sql($sql, $where = array()) { if (user_access('Storm team: view all')) { $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } global $user; $cond = ''; if (user_access('Storm team: view own')) { $cond .= 'n.uid='. $user->uid; } if (user_access('Storm team: view belonged')) { if (!empty($cond)) $cond .= ' OR '; $cond .= "ste.members LIKE '%i:". $user->stormperson_nid .";%'"; } if (empty($cond)) $cond = '0=1'; $where[] = $cond; $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } /** * @function * Implementation of hook_storm_rewrite_where_sql */ function stormteam_storm_rewrite_where_sql($query, $primary_table, $account) { static $conds = array(); if ($conds[$primary_table][$account->uid]) { return $conds[$primary_table][$account->uid]; } if (preg_match("/'storm_access'='storm_access'/", $query)) { $cond = ''; } else { if (user_access('Storm team: view all', $account)) { return ''; } $cond = ''; if (user_access('Storm team: view own', $account)) { $cond .= " ${primary_table}.uid=". $account->uid; } if (user_access('Storm team: view belonged', $account)) { if ($cond) { $cond .= ' OR '; } $cond .= "ste1.members LIKE '%i:". $user->stormperson_nid .";%'"; } if ($cond) { $cond = " WHEN 'stormteam' THEN (SELECT IF($cond,1,0) FROM {stormteam} ste1 WHERE ste1.vid=${primary_table}.vid) "; } else { $cond = " WHEN 'stormteam' THEN 0 "; } } $conds[$primary_table][$account->uid] = $cond; return $cond; } /** * @function * Implementation of hook_form */ function stormteam_form(&$node) { $breadcrumb = array( l('Storm', 'storm'), l('Teams', 'storm/teams'), ); drupal_set_breadcrumb($breadcrumb); $type = node_get_types('type', $node); $form['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -21, ); $form['group1'] = array( '#type' => 'markup', '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group1') : -20, ); $options = array(0 => '-'); $s_per = "SELECT n.nid, n.title FROM {node} n INNER JOIN {stormperson} spe ON n.vid=spe.vid WHERE n.type='stormperson' ORDER BY n.title"; $s_per = stormperson_access_sql($s_per); $s_per = db_rewrite_sql($s_per); $r_per = db_query($s_per); $people = array(); while ($person = db_fetch_object($r_per)) { $people[$person->nid] = $person->title; } $options = $options + array(-1 => t('-PEOPLE-')) + $people; $s_org = "SELECT n.nid, n.title FROM {node} n WHERE n.type='stormorganization' ORDER BY n.title"; $s_org = stormorganization_access_sql($s_org); $s_org = db_rewrite_sql($s_org); $r_org = db_query($s_org); $organizations = array(); while ($organization = db_fetch_object($r_org)) { $organizations[$organization->nid] = $organization->title; } $options = $options + array(-2 => t('-ORGANIZATIONS-')) + $organizations; $i = 1; $variable = 'members_array_'. $i; // Add rows for existing team members if (isset($node->members_array) && is_array($node->members_array)) { foreach ($node->members_array as $node->$variable => $name) { $form['group1'][$variable] = array( '#type' => 'select', '#title' => 'Team Member '. $i, '#options' => $options, '#default_value' => $node->$variable, ); $i++; $variable = 'members_array_'. $i; } } // Add placeholder rows for ($j = 0; $j < 3; $j++) { $variable = 'members_array_'. $i; $form['group1'][$variable] = array( '#type' => 'select', '#title' => 'Team Member '. $i, '#options' => $options, '#default_value' => $node->$variable, ); $i++; } if ($type->has_body) { $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count); } $form['title_old'] = array( '#type' => 'hidden', '#default_value' => $node->title_old, ); return $form; } /** * @function * Implementation of hook_load */ function stormteam_load($node) { $additions = db_fetch_object(db_query('SELECT * FROM {stormteam} WHERE vid = %d', $node->vid)); $additions->members_array = unserialize($additions->members); $additions->title_old = $node->title; return $additions; } /** * @function * Implementation of hook_view */ function stormteam_view($node) { $breadcrumb = array( l('Storm', 'storm'), l('Teams', 'storm/teams'), ); drupal_set_breadcrumb($breadcrumb); return theme('stormteam_view', $node, $teaser, $page); } /** * @function * Function to be called internally before saving Storm Team nodes */ function _stormteam_beforesave(&$node) { $i = 1; $variable = 'members_array_'. $i; while (isset($node->$variable)) { $title = db_fetch_object(db_query('SELECT n.title FROM {node} AS n WHERE nid = %d', $node->$variable)); $node->members_array[$node->$variable] = $title->title; $i++; $variable = 'members_array_'. $i; } if (array_key_exists(0, $node->members_array)) { unset($node->members_array[0]); } $node->members = serialize($node->members_array); } /** * @function * Implementation of hook_insert */ function stormteam_insert($node) { _stormteam_beforesave($node); db_query("INSERT INTO {stormteam} (vid, nid, members) VALUES (%d, %d, '%s')", $node->vid, $node->nid, $node->members ); } /** * @function * Implementation of hook_update */ function stormteam_update($node) { _stormteam_beforesave($node); if ($node->revision) { stormexpense_insert($node); } else { db_query("UPDATE {stormteam} SET members = '%s' WHERE vid = %d", $node->members, $node->vid ); // Invokes hook_stormteam_change so that if title has changed, other modules that have cached the title can update it. if ($node->title != $node->title_old) { module_invoke_all('stormteam_change', $node->nid, $node->title); } } } /** * @function * Implementation of hook_delete */ function stormteam_delete($node) { // Notice that we're matching all revision, by using the node's nid. db_query('DELETE FROM {stormteam} WHERE nid = %d', $node->nid); } /** * @function * Implementation of hook_nodeapi */ function stormteam_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { case 'prepare': // Place for setting parts of the $node object prior to hook_form being called break; case 'delete revision': // Notice that we're matching a single revision based on the node's vid. db_query('DELETE FROM {stormorganization} WHERE vid = %d', $node->vid); break; } } /** * @function * Function to return an autocomplete list for Storm Teams */ function stormteam_autocomplete($string = '') { $matches = array(); if ($string) { $s = "SELECT title FROM {node} AS n WHERE n.type='stormteam' AND LOWER(title) LIKE LOWER('%s%%')"; $s = stormteam_access_sql($s); $s = db_rewrite_sql($s); $result = db_query_range($s, $string, 0, 10); while ($team = db_fetch_object($result)) { $matches[$team->title] = check_plain($team->title); } } drupal_json($matches); } /** * @function * Function to return an autocomplete list for Storm Teams and People */ function stormteam_autocomplete_combo($string = '') { $matches = array(); if ($string) { // TO BE COMPLETED $s = "SELECT title FROM {node} AS n WHERE n.type='stormteam' AND LOWER(title) LIKE LOWER('%s%%')"; $s = stormteam_access_sql($s); $s = db_rewrite_sql($s); $result = db_query_range($s, $string, 0, 10); while ($obj = db_fetch_object($result)) { $matches[$obj->title] = check_plain($obj->title); } } drupal_json($matches); } /** * @function * Function to return whether a user is present in a Storm team based on team $node and user $account */ function stormteam_user_belongs_to_team($team, $person_or_organization_nid) { $node = node_load($team); // Check for person_or_organization_nid in the team members array and return TRUE / FALSE if (is_array($node->members_array)) { return array_key_exists($person_or_organization_nid, $node->members_array); } else { return FALSE; } } /** * @function * Function to return the teams that a user is a member of */ function stormteam_user_return_teams($account = NULL) { if (empty($account)) { global $user; $account = $user; } $s = "SELECT ste.nid FROM {stormteam} ste WHERE (ste.members LIKE '%%i:%d;%%' OR ste.members LIKE '%%i:%d;%%')"; $r = db_query($s, $account->stormperson_nid, $account->stormorganization_nid); $teams = array(); while ($team = db_fetch_object($r)) { $teams[] = $team->nid; } return $teams; }