Index: og_titles.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/og_titles/og_titles.module,v retrieving revision 1.1.2.11 diff -u -p -r1.1.2.11 og_titles.module --- og_titles.module 2 Apr 2009 02:11:30 -0000 1.1.2.11 +++ og_titles.module 27 May 2009 13:46:00 -0000 @@ -83,7 +83,7 @@ ); // should i local task it to the group or the og/users/% path? - if (variable_get('og_titles_selection',0) == 1) + if (variable_get('og_titles_selection','og_titles_text_input') == 1) { $items['node/%node/titles'] = array( 'title' => 'Manage titles', @@ -141,12 +141,14 @@ { menu_rebuild(); // need to do this so that the menu disappears, sucks, but so does drupal + $options = module_invoke_all('ogt_type','info',$options); + $form['og_titles_selection'] = array( '#type' => 'radios', '#title' => t('Titles Selection Type'), '#description' => t('Select whether titles are text input or selection-based'), - '#options' => array(0 => 'Text Input', 1 => 'Select Field'), - '#default_value' => variable_get('og_titles_selection',0), + '#options' => $options, + '#default_value' => variable_get('og_titles_selection','og_titles_text_input'), ); return system_settings_form($form); @@ -154,6 +156,128 @@ /** + * Implementation of hook_ogt_type + * @param $op + * @return $array + */ + function og_titles_ogt_type($op = 'info', $options = array()) + { + switch ($op) + { + case 'info': + $options = array( + 'og_titles_text_input' => t('Text Input'), + 'og_titles_select_input' => t('Select Input'), + ); + return $options; + break; + } + } + + + /** + * og_titles_text_input form + * @param &$form + * @param &$form_state + * @param $gid + * @param $uid + */ + function og_titles_text_input(&$form, &$form_state, $gid, $uid = NULL) + { + $form['#type'] = 'textfield'; + $form['#size'] = 60; + $form['#maxlength'] = 64; + + if (isset($uid)) + { + $res = db_query("SELECT title FROM {og_uid_titles} WHERE nid = %d AND uid = %d", $gid, $uid); + $to = db_fetch_object($res); + } + + if (isset($form_state['values']['og_title'])) + $form['#default_value'] = $form_state['values']['og_title']; + else + $form['#default_value'] = isset($to->title) ? $to->title : ''; + } // function og_titles_text_input + + + /** + * og_titles_select_input_save callback + * @param $gid + * @param $uid + * @param $title + * @param &$form + * @param &$form_state + * @return $success + */ + function og_titles_text_input_save($gid, $uid, $title, &$form, &$form_state) + { + $to = db_fetch_object(db_query("SELECT * FROM {og_uid_titles} WHERE nid = %d AND uid = %d", $gid, $uid)); + + if (isset($to->title)) + return db_query("UPDATE {og_uid_titles} SET title = '%s', weight = 0 WHERE nid = %d AND uid = %d", $title, $gid, $uid); + else + return db_query("INSERT INTO {og_uid_titles} (nid, uid, title, weight) VALUES (%d, %d, '%s', 0)", $gid, $uid, $title); + + } // function og_titles_text_input_save + + + /** + * og_titles_select_input form + * + * @param &$form + * @param &$form_state + * @param $gid + * @param $uid + */ + function og_titles_select_input(&$form, &$form_state, $gid, $uid = NULL) + { + $form['#type'] = 'select'; + $titles = _og_titles_get_available_titles($gid); + $options['none'] = '<'.t('None').'>'; + foreach($titles as $title) + $options[$title->title] = $title->title; + $form['#options'] = $options; + + if (isset($uid)) + { + $res = db_query("SELECT title FROM {og_uid_titles} WHERE nid = %d AND uid = %d", $gid, $uid); + $to = db_fetch_object($res); + } + + if (isset($form_state['values']['og_title'])) + $form['#default_value'] = $form_state['values']['og_title']; + else + $form['#default_value'] = isset($to->title) ? $to->title : ''; + + } // function og_titles_select_input + + + /** + * og_titles_select_input_save callback + * + * @param $gid + * @param $uid + * @param $title + * @param &$form + * @param &$form_state + * @return $success + */ + function og_titles_select_input_save($gid, $uid, $title, &$form, &$form_state) + { + $tl = _og_titles_get_title(NULL, $gid, $uid); + $to = db_fetch_object(db_query("SELECT * FROM {og_uid_titles} WHERE nid = %d AND uid = %d", $gid, $uid)); + + if ($title == 'none') + $title = ''; + if (isset($to->title)) + return db_query("UPDATE {og_uid_titles} SET title = '%s', weight = %d WHERE nid = %d AND uid = %d", $title, $tl->weight, $gid, $uid); + else + return db_query("INSERT INTO {og_uid_titles} (nid, uid, title, weight) VALUES (%d, %d, '%s', %d)", $gid, $uid, $title, $tl->weight); + } // function og_titles_select_input_save + + + /** * manage a group's titles, draggable table form * @param $form_state * @param $node @@ -401,7 +525,7 @@ if (!isset($node)) return FALSE; - if ( (!user_access('manage own og title')) || (!og_is_group_member($node, TRUE, $user->uid)) ) + if ( (!user_access('manage own og title')) && (!og_is_group_member($node, TRUE, $user->uid)) ) return FALSE; return TRUE; } // function og_titles_manage_own_title @@ -421,42 +545,18 @@ drupal_set_message('Error: No node id present'); return; } - $gid = $node->nid; - $option = variable_get('og_titles_selection',0); - if ($option == 1) - { - $titles = _og_titles_get_available_titles($gid); - $titles['none'] = '<'.t('None').'>'; - foreach ($titles as $tl) - $titles[$tl->title] = $tl->title; - } - - $res = db_query("SELECT title FROM {og_uid_titles} WHERE nid = %d AND uid = %d",$gid,$user->uid); - - $to = db_fetch_object($res); + $gid = $node->nid; - $form['og_title'] = array( + $form['og_title'] = array( '#title' => t('Member Title'), '#description' => t('Please choose a member title for yourself for this group.'), - ); - - if (!isset($to->title)) - $form['og_title']['#default_value'] = ''; - else - $form['og_title']['#default_value'] = $to->title; - - if ($option == 1) - { - $form['og_title']['#type'] = 'select'; - $form['og_title']['#options'] = $titles; - } - else - { - $form['og_title']['#type'] = 'textfield'; - $form['og_title']['#size'] = 60; - $form['og_title']['#maxlength'] = 64; - } + ); + $option = variable_get('og_titles_selection', 'og_titles_text_input'); + if (function_exists($option)) + $option($form['og_title'], $form_state, $gid, $user->uid); + else + drupal_set_message('This title selection option does not exist.', 'error'); $form['nid'] = array( '#type' => 'value', @@ -469,7 +569,6 @@ ); return $form; - } // function og_titles_change_title @@ -482,26 +581,14 @@ { global $user; - $ogt_option = variable_get('og_titles_selection',0); + $option = variable_get('og_titles_selection','og_titles_text_input') . '_save'; - $res = db_query('SELECT title FROM {og_uid_titles} WHERE nid = %d AND uid = %d',$form_state['values']['nid'],$user->uid); - - $to = db_fetch_object($res); - - if ($ogt_option == 1) - $tl = _og_titles_get_title(NULL,$form_state['values']['nid'],$form_state['values']['og_title']); - else - $tl->weight = 0; - - if ($form_state['values']['og_title'] == 'none') - $form_state['values']['og_title'] = ''; - if (isset($to)) - db_query("UPDATE {og_uid_titles} SET title = '%s', weight = %d WHERE nid = %d AND uid = %d",$form_state['values']['og_title'],$tl->weight,$form_state['values']['nid'],$user->uid); - else - db_query("INSERT INTO {og_uid_titles} (nid,uid,title,weight) VALUES (%d,%d,'%s',%d)",$form_state['values']['nid'],$user->uid,$form_state['values']['og_title'],$tl->weight); - - drupal_set_message('Member title saved.'); + $success = $option($form_state['values']['nid'], $user->uid, $form_state['values']['og_title'], &$form, &$form_state); + if (!$success) + drupal_set_message('Could not save member title.', 'error'); + else + drupal_set_message('Member title saved'); } // function og_titles_change_title_submit @@ -526,42 +613,26 @@ else $gid = $node->nid; - $ogt_option = variable_get('og_titles_selection',0); + $option = variable_get('og_titles_selection','og_titles_text_input'); $form = array(); + $form['titles'] = array( + '#tree' => TRUE, + ); $qry = 'SELECT ogu.uid, u.name, ogt.title, ogu.nid FROM {og_uid} ogu LEFT OUTER JOIN {og_uid_titles} ogt ON (ogu.uid = ogt.uid AND ogu.nid = ogt.nid) INNER JOIN {users} u ON (u.uid = ogu.uid) WHERE ogu.nid = %d'; - - if ($ogt_option == 1) - { - $ogt_options = ""; - $titles = _og_titles_get_available_titles($gid); - $ogt_options['none'] = '<'.t('None').'>'; - foreach($titles as $title) - $ogt_options[$title->title] = $title->title; - } - - $res = db_query($qry,$gid); + $res = db_query($qry, $gid); $form['titles'] = array( '#tree' => TRUE ); while ( $titles = db_fetch_object($res) ) - { - $form['titles'][$titles->uid] = array( - '#type' => 'textfield', - '#title' => t($titles->name . '\'s title'), - '#default_value' => $titles->title, - '#size' => 60, - '#maxlength' => 64, - ); - - if ($ogt_option == 1) - { - $form['titles'][$titles->uid]['#type'] = 'select'; - $form['titles'][$titles->uid]['#options'] = $ogt_options; - $form['titles'][$titles->uid]['#size'] = 1; - } - } + { + $form['titles'][$titles->uid] = array( + '#title' => $titles->name, + '#description' => t('Please choose a member title for '.$titles->name), + ); + $option($form['titles'][$titles->uid], $form_state, $gid, $titles->uid); + } $form['nid'] = array( '#type' => 'value', @@ -588,21 +659,14 @@ $nid = $form_state['values']['nid']; $users = _og_titles_get_uids($nid); - $ogt_option = variable_get('og_titles_selection',0); + $option = variable_get('og_titles_selection','og_titles_text_input') . '_save'; foreach ( $users as $usr ) { - if ($ogt_option == 1) - $tl = _og_titles_get_title(NULL,$form_state['values']['nid'],$form_state['values']['titles'][$usr->uid]); - else - $tl->weight = 0; - - if ($form_state['values']['titles'][$usr->uid] == 'none') - $form_state['values']['titles'][$usr->uid] = ''; - if ( isset($usr->title)) - db_query("UPDATE {og_uid_titles} SET title = '%s', weight = %d WHERE uid = %d and nid = %d",$form_state['values']['titles'][$usr->uid],$tl->weight,$usr->uid,$nid); - else - db_query("INSERT INTO {og_uid_titles} (nid,uid,title,weight) VALUES (%d,%d,'%s',%d)",$nid,$usr->uid,$form_state['values']['titles'][$usr->uid],$tl->weight); + $success = $option($nid, $usr->uid, $form_state['values']['titles'][$usr->uid], $form, $form_state); + + if (!$success) + drupal_set_message('Could not save title for '.$usr->name, 'error'); } } // function og_titles_view_titles_submit