| 1 |
<?php
|
| 2 |
|
| 3 |
function topichubs_admin_overview() {
|
| 4 |
$header = array(
|
| 5 |
t('Title'),
|
| 6 |
t('Commands'),
|
| 7 |
);
|
| 8 |
$rows = array();
|
| 9 |
$result = pager_query("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'topichub'", 50, 0);
|
| 10 |
while ($topichub = db_fetch_object($result)) {
|
| 11 |
$commands = array(
|
| 12 |
l(t('Configure'), 'node/'. $topichub->nid .'/topichub'),
|
| 13 |
l(t('Delete'), 'node/'. $topichub->nid .'/delete'),
|
| 14 |
);
|
| 15 |
$rows[] = array(
|
| 16 |
l($topichub->title, "node/$topichub->nid"),
|
| 17 |
theme('item_list', $commands)
|
| 18 |
);
|
| 19 |
}
|
| 20 |
$output .= theme('table', $header, $rows);
|
| 21 |
$output .= theme('pager', 0, 50);
|
| 22 |
return $output;
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Listing page for hot topics
|
| 27 |
*/
|
| 28 |
function topichubs_hot_topics_page() {
|
| 29 |
$header = array(
|
| 30 |
t('Term'),
|
| 31 |
t('Count'),
|
| 32 |
t('Hubs with Term'),
|
| 33 |
t('Commands'),
|
| 34 |
);
|
| 35 |
$rows = array();
|
| 36 |
|
| 37 |
$rows = _get_terms_by_node_count(variable_get('hot_topic_ignore', NULL));
|
| 38 |
|
| 39 |
$output .= theme('table', $header, $rows);
|
| 40 |
$output .= theme('pager', 0, 50);
|
| 41 |
return $output;
|
| 42 |
}
|
| 43 |
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Page to display button to update from Panel 2 to 3
|
| 47 |
*/
|
| 48 |
function topichubs_update_page() {
|
| 49 |
if (variable_get('topichubs_panel_update_run', 0)) {
|
| 50 |
return t('Your Topic Hubs have been updated.');
|
| 51 |
}
|
| 52 |
else {
|
| 53 |
return drupal_get_form('topichubs_update_form');
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* This form is just a button to click to start the update process
|
| 59 |
*/
|
| 60 |
function topichubs_update_form() {
|
| 61 |
$form = array();
|
| 62 |
$form['intro'] = array(
|
| 63 |
'#value' => '<p>'.t('Clicking the button below will run a database update to update your Topic Hubs from Panels 2 to 3. As with any database update, please backup your database before updating.').'</p>',
|
| 64 |
);
|
| 65 |
$form['submit'] = array(
|
| 66 |
'#type' => 'submit',
|
| 67 |
'#value' => 'Run Update',
|
| 68 |
);
|
| 69 |
return $form;
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Handle the update form submission and call the update function
|
| 74 |
*/
|
| 75 |
function topichubs_update_form_submit() {
|
| 76 |
if (!variable_get('topichubs_panel_update_run', 0)) {
|
| 77 |
$success = _update_panel_pane_type();
|
| 78 |
if ($success) {
|
| 79 |
return t('Your Topic Hubs have been updated.');
|
| 80 |
}
|
| 81 |
else {
|
| 82 |
return t('There was a problem with the update, please check the log for errors.');
|
| 83 |
}
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
return t('You have already run the update.');
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* Get all taxonomy terms ordered by frequency used.
|
| 92 |
*
|
| 93 |
* @param $ignore
|
| 94 |
* An array of vocabulary ids to not include in this list.
|
| 95 |
* @param $per_page
|
| 96 |
* The number of terms to show on each page
|
| 97 |
*/
|
| 98 |
function _get_terms_by_node_count($ignore = NULL, $per_page = 50) {
|
| 99 |
$sql = "SELECT td.name, t.tid, COUNT(n.nid) AS count ";
|
| 100 |
$sql .= "FROM {term_node} t ";
|
| 101 |
$sql .= "INNER JOIN {node} n ON t.vid = n.vid ";
|
| 102 |
$sql .= "INNER JOIN {term_data} td ON t.tid = td.tid ";
|
| 103 |
$sql .= "WHERE n.status = 1 ";
|
| 104 |
if(!empty($ignore))
|
| 105 |
$sql .= "AND td.vid NOT IN (" . implode(",",$ignore) . ") ";
|
| 106 |
|
| 107 |
$sql .= "GROUP BY t.tid ORDER BY count DESC";
|
| 108 |
|
| 109 |
|
| 110 |
$count_query = "SELECT COUNT(DISTINCT(t.tid)) FROM {term_node} t ";
|
| 111 |
if(!empty($ignore)) {
|
| 112 |
$count_query .= "JOIN {term_data} td ON td.tid = t.tid ";
|
| 113 |
$count_query .= "WHERE td.vid NOT IN (" . implode(",",$ignore) . ") ";
|
| 114 |
}
|
| 115 |
|
| 116 |
$result = pager_query($sql, $per_page, 0, $count_query);
|
| 117 |
|
| 118 |
$rows = array();
|
| 119 |
while($item = db_fetch_array($result)) {
|
| 120 |
$count = _topichub_count_by_tid($item['tid']);
|
| 121 |
$item['hub_count'] = $count ? format_plural($count, '1 Hub', '@count Hubs') : '';
|
| 122 |
$item['commands'] = _get_topichub_commands($item['tid']);
|
| 123 |
unset($item['tid']);
|
| 124 |
$rows[] = $item;
|
| 125 |
}
|
| 126 |
|
| 127 |
return $rows;
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Get the various commands that can be done on the hot topics page.
|
| 132 |
*/
|
| 133 |
function _get_topichub_commands($tid) {
|
| 134 |
$commands = array();
|
| 135 |
$commands[] = l("Create a Topic Hub", "node/add/topichub/topichubs", array('query' => array('tid' => $tid)));
|
| 136 |
return implode(', ', $commands);
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Returns the number of topic hubs that exist already with the provided term id
|
| 141 |
* @return int
|
| 142 |
* The count
|
| 143 |
*/
|
| 144 |
function _topichub_count_by_tid($tid) {
|
| 145 |
return db_result(db_query("SELECT count(distinct(nid)) FROM {topichub_condition} WHERE tid = %d", $tid));
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Update 'panels_mini' to 'topichub' pane type for exiting topic hubs.
|
| 150 |
* This is necessary after a panels update from version 2 to 3.
|
| 151 |
* @return bool
|
| 152 |
* success
|
| 153 |
*/
|
| 154 |
function _update_panel_pane_type() {
|
| 155 |
$success = true;
|
| 156 |
$result = db_query("SELECT pp.did FROM {panels_pane} pp INNER JOIN {panels_node} pn ON pn.did = pp.did INNER JOIN {node} n ON n.nid = pn.nid WHERE n.type= 'topichub' AND pp.type = 'panels_mini'");
|
| 157 |
|
| 158 |
while($item = db_fetch_array($result)) {
|
| 159 |
$updated = db_query("UPDATE {panels_pane} SET type = 'topichubs' WHERE did = %d", $item['did']);
|
| 160 |
if (!$updated) {
|
| 161 |
$success = false;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
if ($success) {
|
| 166 |
variable_set('topichubs_panel_update_run', 1);
|
| 167 |
watchdog(t('Topic Hubs'), t("Topic Hubs updated from Panels 2 to 3 successfully!"), NULL, WATCHDOG_INFO);
|
| 168 |
}
|
| 169 |
else {
|
| 170 |
drupal_set_message(t('Topic Hubs update failed! Check the log for any errors.'));
|
| 171 |
watchdog(t('Topic Hubs'), t("Topic Hubs update from Panels 2 to 3 failed!"), NULL, WATCHDOG_ERROR);
|
| 172 |
}
|
| 173 |
|
| 174 |
return $success;
|
| 175 |
}
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Global settings for Topichubs.
|
| 179 |
*/
|
| 180 |
function topichubs_settings(&$form_state) {
|
| 181 |
drupal_add_css(drupal_get_path('module', 'topichubs') . "/topichubs-admin.css");
|
| 182 |
$form = array();
|
| 183 |
|
| 184 |
$form['topic_hub_settings'] = array(
|
| 185 |
'#type' => 'fieldset',
|
| 186 |
'#title' => t('Global Settings'),
|
| 187 |
);
|
| 188 |
|
| 189 |
$vocabs = taxonomy_get_vocabularies();
|
| 190 |
$options = array();
|
| 191 |
foreach($vocabs as $vocab) {
|
| 192 |
$options["$vocab->vid"] = $vocab->name;
|
| 193 |
}
|
| 194 |
|
| 195 |
$form['topic_hub_settings']['topic_hub_vocab_ignore'] = array(
|
| 196 |
'#prefix' => '<div class="topic-hub-settings">',
|
| 197 |
'#suffix' => '</div>',
|
| 198 |
'#type' => 'select',
|
| 199 |
'#title' => t("Disregarded Vocabularies"),
|
| 200 |
'#description' => t('Any vocabulary selected will not be listed under the Hot Topics section'),
|
| 201 |
'#multiple' => true,
|
| 202 |
'#options' => $options,
|
| 203 |
'#default_value' => variable_get('hot_topic_ignore', NULL),
|
| 204 |
);
|
| 205 |
|
| 206 |
$types = node_get_types();
|
| 207 |
$options = array();
|
| 208 |
foreach($types as $type => $info) {
|
| 209 |
$options[$type] = $info->name;
|
| 210 |
}
|
| 211 |
|
| 212 |
$form['topic_hub_settings']['topic_hub_plugin_type_default'] = array(
|
| 213 |
'#prefix' => '<div class="topic-hub-settings">',
|
| 214 |
'#suffix' => '</div>',
|
| 215 |
'#type' => 'checkboxes',
|
| 216 |
'#title' => 'Global Plugin Type Defaults',
|
| 217 |
'#description' => t('If a plugin is allowed to pick which content types it can work with, then the types selected here are used as the default setting.'),
|
| 218 |
'#options' => $options,
|
| 219 |
'#default_value' => variable_get("topic_hub_plugin_type_default", array_keys($options)),
|
| 220 |
);
|
| 221 |
|
| 222 |
$parent = 'topic_hub_plugins';
|
| 223 |
|
| 224 |
$form[$parent] = array(
|
| 225 |
'#type' => 'fieldset',
|
| 226 |
'#title' => t("Plugin Settings"),
|
| 227 |
);
|
| 228 |
|
| 229 |
$plugins = topichubs_discover_plugins();
|
| 230 |
foreach($plugins as $type => $def) {
|
| 231 |
$impl = _topichubs_new_handler_class($def);
|
| 232 |
if($impl) {
|
| 233 |
$impl->init($type, $def, $node, $node->topichub->config[$type]);
|
| 234 |
$impl->build_form('settings', $form[$parent], $form_state);
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
return system_settings_form($form);
|
| 239 |
}
|
| 240 |
|