web/drupal/modules/forum/forum.admin.inc
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: forum.admin.inc,v 1.8.2.1 2009/05/26 08:13:00 goba Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Administrative page callbacks for the forum module.
       
     7  */
       
     8 
       
     9 function forum_form_main($type, $edit = array()) {
       
    10   if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
       
    11     return drupal_get_form('forum_confirm_delete', $edit['tid']);
       
    12   }
       
    13   switch ($type) {
       
    14     case 'forum':
       
    15       return drupal_get_form('forum_form_forum', $edit);
       
    16       break;
       
    17     case 'container':
       
    18       return drupal_get_form('forum_form_container', $edit);
       
    19       break;
       
    20   }
       
    21 }
       
    22 
       
    23 /**
       
    24  * Returns a form for adding a forum to the forum vocabulary
       
    25  *
       
    26  * @param $edit Associative array containing a forum term to be added or edited.
       
    27  * @ingroup forms
       
    28  * @see forum_form_submit()
       
    29  */
       
    30 function forum_form_forum(&$form_state, $edit = array()) {
       
    31   $edit += array(
       
    32     'name' => '',
       
    33     'description' => '',
       
    34     'tid' => NULL,
       
    35     'weight' => 0,
       
    36   );
       
    37   $form['name'] = array('#type' => 'textfield',
       
    38     '#title' => t('Forum name'),
       
    39     '#default_value' => $edit['name'],
       
    40     '#maxlength' => 255,
       
    41     '#description' => t('Short but meaningful name for this collection of threaded discussions.'),
       
    42     '#required' => TRUE,
       
    43   );
       
    44   $form['description'] = array('#type' => 'textarea',
       
    45     '#title' => t('Description'),
       
    46     '#default_value' => $edit['description'],
       
    47     '#description' => t('Description and guidelines for discussions within this forum.'),
       
    48   );
       
    49   $form['parent']['#tree'] = TRUE;
       
    50   $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'forum');
       
    51   $form['weight'] = array('#type' => 'weight',
       
    52     '#title' => t('Weight'),
       
    53     '#default_value' => $edit['weight'],
       
    54     '#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
       
    55   );
       
    56 
       
    57   $form['vid'] = array('#type' => 'hidden', '#value' => variable_get('forum_nav_vocabulary', ''));
       
    58   $form['submit' ] = array('#type' => 'submit', '#value' => t('Save'));
       
    59   if ($edit['tid']) {
       
    60     $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
       
    61     $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
       
    62   }
       
    63   $form['#submit'][] = 'forum_form_submit';
       
    64   $form['#theme'] = 'forum_form';
       
    65 
       
    66   return $form;
       
    67 }
       
    68 
       
    69 /**
       
    70  * Process forum form and container form submissions.
       
    71  */
       
    72 function forum_form_submit($form, &$form_state) {
       
    73   if ($form['form_id']['#value'] == 'forum_form_container') {
       
    74     $container = TRUE;
       
    75     $type = t('forum container');
       
    76   }
       
    77   else {
       
    78     $container = FALSE;
       
    79     $type = t('forum');
       
    80   }
       
    81 
       
    82   $status = taxonomy_save_term($form_state['values']);
       
    83   switch ($status) {
       
    84     case SAVED_NEW:
       
    85       if ($container) {
       
    86         $containers = variable_get('forum_containers', array());
       
    87         $containers[] = $form_state['values']['tid'];
       
    88         variable_set('forum_containers', $containers);
       
    89       }
       
    90       drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));
       
    91       break;
       
    92     case SAVED_UPDATED:
       
    93       drupal_set_message(t('The @type %term has been updated.', array('%term' => $form_state['values']['name'], '@type' => $type)));
       
    94       break;
       
    95   }
       
    96   $form_state['redirect'] = 'admin/content/forum';
       
    97   return;
       
    98 }
       
    99 
       
   100 /**
       
   101  * Returns a form for adding a container to the forum vocabulary
       
   102  *
       
   103  * @param $edit Associative array containing a container term to be added or edited.
       
   104  * @ingroup forms
       
   105  * @see forum_form_submit()
       
   106  */
       
   107 function forum_form_container(&$form_state, $edit = array()) {
       
   108   $edit += array(
       
   109     'name' => '',
       
   110     'description' => '',
       
   111     'tid' => NULL,
       
   112     'weight' => 0,
       
   113   );
       
   114   // Handle a delete operation.
       
   115   $form['name'] = array(
       
   116     '#title' => t('Container name'),
       
   117     '#type' => 'textfield',
       
   118     '#default_value' => $edit['name'],
       
   119     '#maxlength' => 255,
       
   120     '#description' => t('Short but meaningful name for this collection of related forums.'),
       
   121     '#required' => TRUE
       
   122   );
       
   123 
       
   124   $form['description'] = array(
       
   125     '#type' => 'textarea',
       
   126     '#title' => t('Description'),
       
   127     '#default_value' => $edit['description'],
       
   128     '#description' => t('Description and guidelines for forums within this container.')
       
   129   );
       
   130   $form['parent']['#tree'] = TRUE;
       
   131   $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'container');
       
   132   $form['weight'] = array(
       
   133     '#type' => 'weight',
       
   134     '#title' => t('Weight'),
       
   135     '#default_value' => $edit['weight'],
       
   136     '#description' => t('Containers are displayed in ascending order by weight (containers with equal weights are displayed alphabetically).')
       
   137   );
       
   138 
       
   139   $form['vid'] = array(
       
   140     '#type' => 'hidden',
       
   141     '#value' => variable_get('forum_nav_vocabulary', ''),
       
   142   );
       
   143   $form['submit'] = array(
       
   144     '#type' => 'submit',
       
   145     '#value' => t('Save')
       
   146   );
       
   147   if ($edit['tid']) {
       
   148     $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
       
   149     $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
       
   150   }
       
   151   $form['#submit'][] = 'forum_form_submit';
       
   152   $form['#theme'] = 'forum_form';
       
   153 
       
   154   return $form;
       
   155 }
       
   156 
       
   157 /**
       
   158  * Returns a confirmation page for deleting a forum taxonomy term.
       
   159  *
       
   160  * @param $tid ID of the term to be deleted
       
   161  */
       
   162 function forum_confirm_delete(&$form_state, $tid) {
       
   163   $term = taxonomy_get_term($tid);
       
   164 
       
   165   $form['tid'] = array('#type' => 'value', '#value' => $tid);
       
   166   $form['name'] = array('#type' => 'value', '#value' => $term->name);
       
   167 
       
   168   return confirm_form($form, t('Are you sure you want to delete the forum %name?', array('%name' => $term->name)), 'admin/content/forum', t('Deleting a forum or container will also delete its sub-forums, if any. To delete posts in this forum, visit <a href="@content">content administration</a> first. This action cannot be undone.', array('@content' => url('admin/content/node'))), t('Delete'), t('Cancel'));
       
   169 }
       
   170 
       
   171 /**
       
   172  * Implementation of forms api _submit call. Deletes a forum after confirmation.
       
   173  */
       
   174 function forum_confirm_delete_submit($form, &$form_state) {
       
   175   taxonomy_del_term($form_state['values']['tid']);
       
   176   drupal_set_message(t('The forum %term and all sub-forums and associated posts have been deleted.', array('%term' => $form_state['values']['name'])));
       
   177   watchdog('content', 'forum: deleted %term and all its sub-forums and associated posts.', array('%term' => $form_state['values']['name']));
       
   178 
       
   179   $form_state['redirect'] = 'admin/content/forum';
       
   180   return;
       
   181 }
       
   182 
       
   183 /**
       
   184  * Form builder for the forum settings page.
       
   185  *
       
   186  * @see system_settings_form()
       
   187  */
       
   188 function forum_admin_settings() {
       
   189   $form = array();
       
   190   $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
       
   191   $form['forum_hot_topic'] = array('#type' => 'select',
       
   192     '#title' => t('Hot topic threshold'),
       
   193     '#default_value' => variable_get('forum_hot_topic', 15),
       
   194     '#options' => $number,
       
   195     '#description' => t('The number of posts a topic must have to be considered "hot".'),
       
   196   );
       
   197   $number = drupal_map_assoc(array(10, 25, 50, 75, 100));
       
   198   $form['forum_per_page'] = array('#type' => 'select',
       
   199     '#title' => t('Topics per page'),
       
   200     '#default_value' => variable_get('forum_per_page', 25),
       
   201     '#options' => $number,
       
   202     '#description' => t('Default number of forum topics displayed per page.'),
       
   203   );
       
   204   $forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active first'));
       
   205   $form['forum_order'] = array('#type' => 'radios',
       
   206     '#title' => t('Default order'),
       
   207     '#default_value' => variable_get('forum_order', '1'),
       
   208     '#options' => $forder,
       
   209     '#description' => t('Default display order for topics.'),
       
   210   );
       
   211   return system_settings_form($form);
       
   212 }
       
   213 
       
   214 /**
       
   215  * Returns an overview list of existing forums and containers
       
   216  */
       
   217 function forum_overview(&$form_state) {
       
   218   module_load_include('inc', 'taxonomy', 'taxonomy.admin');
       
   219 
       
   220   $vid = variable_get('forum_nav_vocabulary', '');
       
   221   $vocabulary = taxonomy_vocabulary_load($vid);
       
   222   $form = taxonomy_overview_terms($form_state, $vocabulary);
       
   223 
       
   224   foreach (element_children($form) as $key) {
       
   225     if (isset($form[$key]['#term'])) {
       
   226       $term = $form[$key]['#term'];
       
   227       $form[$key]['view']['#value'] = l($term['name'], 'forum/'. $term['tid']);
       
   228       if (in_array($form[$key]['#term']['tid'], variable_get('forum_containers', array()))) {
       
   229         $form[$key]['edit']['#value'] = l(t('edit container'), 'admin/content/forum/edit/container/'. $term['tid']);
       
   230       }
       
   231       else {
       
   232         $form[$key]['edit']['#value'] = l(t('edit forum'), 'admin/content/forum/edit/forum/'. $term['tid']);
       
   233       }
       
   234     }
       
   235   }
       
   236 
       
   237   // Remove the alphabetical reset.
       
   238   unset($form['reset_alphabetical']);
       
   239 
       
   240   // The form needs to have submit and validate handlers set explicitly.
       
   241   $form['#theme'] = 'taxonomy_overview_terms';
       
   242   $form['#submit'] = array('taxonomy_overview_terms_submit'); // Use the existing taxonomy overview submit handler.
       
   243   $form['#validate'] = array('taxonomy_overview_terms_validate');
       
   244   $form['#empty_text'] = '<em>'. t('There are no existing containers or forums. Containers and forums may be added using the <a href="@container">add container</a> and <a href="@forum">add forum</a> pages.', array('@container' => url('admin/content/forum/add/container'), '@forum' => url('admin/content/forum/add/forum'))) .'</em>';
       
   245   return $form;
       
   246 }
       
   247 
       
   248 /**
       
   249  * Returns a select box for available parent terms
       
   250  *
       
   251  * @param $tid ID of the term which is being added or edited
       
   252  * @param $title Title to display the select box with
       
   253  * @param $child_type Whether the child is forum or container
       
   254  */
       
   255 function _forum_parent_select($tid, $title, $child_type) {
       
   256 
       
   257   $parents = taxonomy_get_parents($tid);
       
   258   if ($parents) {
       
   259     $parent = array_shift($parents);
       
   260     $parent = $parent->tid;
       
   261   }
       
   262   else {
       
   263     $parent = 0;
       
   264   }
       
   265 
       
   266   $vid = variable_get('forum_nav_vocabulary', '');
       
   267   $children = taxonomy_get_tree($vid, $tid);
       
   268 
       
   269   // A term can't be the child of itself, nor of its children.
       
   270   foreach ($children as $child) {
       
   271     $exclude[] = $child->tid;
       
   272   }
       
   273   $exclude[] = $tid;
       
   274 
       
   275   $tree = taxonomy_get_tree($vid);
       
   276   $options[0] = '<'. t('root') .'>';
       
   277   if ($tree) {
       
   278     foreach ($tree as $term) {
       
   279       if (!in_array($term->tid, $exclude)) {
       
   280         $options[$term->tid] = str_repeat(' -- ', $term->depth) . $term->name;
       
   281       }
       
   282     }
       
   283   }
       
   284   if ($child_type == 'container') {
       
   285     $description = t('Containers are usually placed at the top (root) level, but may also be placed inside another container or forum.');
       
   286   }
       
   287   else if ($child_type == 'forum') {
       
   288     $description = t('Forums may be placed at the top (root) level, or inside another container or forum.');
       
   289   }
       
   290 
       
   291   return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
       
   292 }