| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
//////////////////////////////////////////////////////////////////////////////
|
| 5 |
// Node relationship tab
|
| 6 |
|
| 7 |
function relations_node_title_autocomplete($node1 = TRUE, $string = '') {
|
| 8 |
$matches = array();
|
| 9 |
if (!empty($string)) {
|
| 10 |
$result = is_object($node1) ?
|
| 11 |
db_query("SELECT nid, title FROM {node} WHERE nid != %d AND LOWER(title) LIKE LOWER('%s%%') ORDER BY title ASC", $node1->nid, $string) :
|
| 12 |
db_query("SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%s%%') ORDER BY title ASC", $string);
|
| 13 |
while (($node2 = db_fetch_object($result)) && count($matches) <= 15) {
|
| 14 |
// TODO: filter out already related nodes.
|
| 15 |
|
| 16 |
// Check whether the current user's access privileges permit the
|
| 17 |
// creation of this relationship:
|
| 18 |
if (relations_allowed($node1, $node2 = node_load($node2->nid))) {
|
| 19 |
|
| 20 |
// To prevent ambiguous behavior in cases where multiple nodes have
|
| 21 |
// identical titles, we return the node's full URL as the key:
|
| 22 |
$url = url($path = drupal_get_path_alias('node/' . $node2->nid), array('absolute' => TRUE));
|
| 23 |
$matches[$url] = check_plain($node2->title);
|
| 24 |
}
|
| 25 |
}
|
| 26 |
}
|
| 27 |
drupal_json($matches);
|
| 28 |
}
|
| 29 |
|
| 30 |
function relations_node_title_lookup($title) {
|
| 31 |
return node_load(array('title' => $title));
|
| 32 |
}
|
| 33 |
|
| 34 |
function relations_node_form($form_state, $node, $is_embedded = FALSE) {
|
| 35 |
$can_manage = user_access('create node relations') || user_access('delete node relations');
|
| 36 |
return array(
|
| 37 |
'#prefix' => '<div class="form-item" id="relations-table">', // class="clear-block"
|
| 38 |
'#suffix' => $can_manage ? '<div class="description">' . relations_help('node/%/relations#table') . '</div></div>' : '</div>',
|
| 39 |
'#theme' => 'relations_node_form',
|
| 40 |
'#tree' => TRUE,
|
| 41 |
|
| 42 |
'title' => array(
|
| 43 |
'#type' => 'textfield',
|
| 44 |
'#size' => 32,
|
| 45 |
'#maxlength' => 4096,
|
| 46 |
'#autocomplete_path' => module_exists('picker') ? '' : 'node/' . (!empty($node->nid) ? $node->nid : 'add') . '/relations/autocomplete',
|
| 47 |
),
|
| 48 |
'pick' => array(
|
| 49 |
'#type' => 'markup',
|
| 50 |
'#value' => !module_exists('picker') ? '' : picker_build_link(array(
|
| 51 |
'element' => $is_embedded ? 'edit-relations-table-title' : 'edit-title',
|
| 52 |
'name' => t('Select content'),
|
| 53 |
'content_type' => relations_get_node_types(),
|
| 54 |
'return' => 'fullnodeurl',
|
| 55 |
'seperator' => ' ',
|
| 56 |
'type' => 'button',
|
| 57 |
'view_fields' => array('title' => t('Title'), 'type' => t('Type'), 'name' => t('Author')),
|
| 58 |
'access_callback' => 'relations_picker_access', // @see relations.module
|
| 59 |
)),
|
| 60 |
),
|
| 61 |
'create' => !$is_embedded ? array('#type' => 'submit', '#value' => t('Add relationship')) : array(
|
| 62 |
'#type' => 'submit',
|
| 63 |
'#value' => t('Add relationship'),
|
| 64 |
'#weight' => 1,
|
| 65 |
'#ahah' => array(
|
| 66 |
'path' => 'node/add/relations/create',
|
| 67 |
'wrapper' => 'relations-table',
|
| 68 |
'method' => 'replace',
|
| 69 |
'effect' => 'fade',
|
| 70 |
),
|
| 71 |
'#description' => t(''),
|
| 72 |
),
|
| 73 |
'nid' => array(
|
| 74 |
'#type' => 'hidden',
|
| 75 |
'#value' => !empty($node->nid) ? $node->nid : '',
|
| 76 |
),
|
| 77 |
'embedded' => array(
|
| 78 |
'#type' => 'hidden',
|
| 79 |
'#value' => (string)$is_embedded,
|
| 80 |
),
|
| 81 |
);
|
| 82 |
}
|
| 83 |
|
| 84 |
function relations_node_form_validate($form, &$form_state) {
|
| 85 |
extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
|
| 86 |
|
| 87 |
if (empty($title)) {
|
| 88 |
return; // when editing a node and using the embedded relationships form
|
| 89 |
}
|
| 90 |
else if (preg_match('!^(http|https)://!', $title)) {
|
| 91 |
//$title = preg_replace('!https://!', '!http://!', $title);
|
| 92 |
foreach (explode(' ', trim($title)) as $url) {
|
| 93 |
if (!valid_url($url, TRUE)) {
|
| 94 |
form_set_error('title', t('Not a valid URL: %url.', array('%url' => $url)));
|
| 95 |
}
|
| 96 |
|
| 97 |
if (strpos($url, $GLOBALS['base_url']) !== 0) { // the URL should begin with $base_url
|
| 98 |
form_set_error('title', t('Not a valid local URL: %url.', array('%url' => $url)));
|
| 99 |
}
|
| 100 |
|
| 101 |
$path = drupal_get_normal_path(substr($url, strlen($GLOBALS['base_url']) + 1));
|
| 102 |
if (!preg_match('!node/(\d+)!', $path, $matches) || !($node2 = node_load((int)$matches[1]))) {
|
| 103 |
form_set_error('title', t('Not a valid content URL: %url.', array('%url' => $path)));
|
| 104 |
}
|
| 105 |
}
|
| 106 |
}
|
| 107 |
else if (preg_match('/^(\d+,?)+$/', $title, $matches)) { // (deprecated) special case for Picker module
|
| 108 |
$access = node_access('update', node_load($nid));
|
| 109 |
foreach (array_map('intval', explode(',', $title)) as $nid2) {
|
| 110 |
if (!$access && !node_access('update', node_load($nid2))) {
|
| 111 |
form_set_error('title', t('Your current access privileges prohibit the creation of this relationship.'));
|
| 112 |
}
|
| 113 |
}
|
| 114 |
}
|
| 115 |
else if (!($node2 = relations_node_title_lookup($title))) {
|
| 116 |
form_set_error('title', t('No matching content titled %title found.', array('%title' => $title)));
|
| 117 |
}
|
| 118 |
else if (!node_access('update', node_load($nid)) && !node_access('update', $node2)) {
|
| 119 |
form_set_error('title', t('Your current access privileges prohibit the creation of this relationship.'));
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
function relations_node_form_submit($form, &$form_state) {
|
| 124 |
extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
|
| 125 |
|
| 126 |
$nodes = array();
|
| 127 |
if (empty($title)) {
|
| 128 |
return; // when editing a node and using the embedded relationships form
|
| 129 |
}
|
| 130 |
else if (preg_match('!^(http|https)://!', $title)) {
|
| 131 |
foreach (explode(' ', trim($title)) as $url) {
|
| 132 |
$path = drupal_get_normal_path(substr($url, strlen($GLOBALS['base_url']) + 1));
|
| 133 |
if (preg_match('!node/(\d+)!', $path, $matches)) {
|
| 134 |
$nodes[] = node_load((int)$matches[1]);
|
| 135 |
}
|
| 136 |
}
|
| 137 |
}
|
| 138 |
else if (preg_match('/^(\d+,?)+$/', $title, $matches)) { // (deprecated) special case for Picker module
|
| 139 |
$nodes = array_map('node_load', array_map('intval', explode(',', $title)));
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$nodes[] = relations_node_title_lookup($title);
|
| 143 |
}
|
| 144 |
|
| 145 |
if (!empty($nodes)) {
|
| 146 |
foreach ($nodes as $node2) {
|
| 147 |
relations_api_create($nid, $node2->nid);
|
| 148 |
|
| 149 |
watchdog('relations', 'Created relationship from node #%nid1 to #%nid2.', array('%nid1' => $nid, '%nid2' => $node2->nid), WATCHDOG_NOTICE, l(t('view'), 'node/' . $nid . '/relations'));
|
| 150 |
drupal_set_message(t('The relationship to <a href="@url">%title</a> was created.', array('%title' => $node2->title, '@url' => url('node/'. $node2->nid))));
|
| 151 |
}
|
| 152 |
|
| 153 |
$form_state['redirect'] = 'node/' . $nid . '/relations';
|
| 154 |
}
|
| 155 |
}
|
| 156 |
|
| 157 |
function relations_node_delete($form_state, $node1, $node2) {
|
| 158 |
$form['nid1'] = array('#type' => 'value', '#value' => $node1->nid);
|
| 159 |
$form['nid2'] = array('#type' => 'value', '#value' => $node2->nid);
|
| 160 |
|
| 161 |
return confirm_form($form,
|
| 162 |
t('Are you sure you want to delete the relationship to <a href="@url">%title</a>?', array('%title' => $node2->title, '@url' => url('node/'. $node2->nid))),
|
| 163 |
isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node1->nid .'/relations',
|
| 164 |
t('This action cannot be undone.'));
|
| 165 |
}
|
| 166 |
|
| 167 |
function relations_node_delete_submit($form, &$form_state) {
|
| 168 |
extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
|
| 169 |
|
| 170 |
if ($form_state['values']['confirm']) {
|
| 171 |
relations_api_delete($nid1, $nid2);
|
| 172 |
|
| 173 |
watchdog('relations', 'Deleted relationship from node #%nid1 to #%nid2.', array('%nid1' => $nid1, '%nid2' => $nid2), WATCHDOG_NOTICE, l(t('view'), 'node/'. $nid1 .'/relations'));
|
| 174 |
$node2 = node_load($nid2);
|
| 175 |
drupal_set_message(t('The relationship to <a href="@url">%title</a> has been deleted.', array('%title' => ($node2 ? $node2->title : '#'. $nid2), '@url' => url('node/'. $nid2))));
|
| 176 |
}
|
| 177 |
|
| 178 |
$form_state['redirect'] = 'node/'. $nid1 .'/relations';
|
| 179 |
}
|
| 180 |
|
| 181 |
function relations_node_session_create() {
|
| 182 |
$node1 = node_load($_POST['relations_table']['nid']);
|
| 183 |
$title = $_POST['relations_table']['title'];
|
| 184 |
|
| 185 |
if (preg_match('/^(\d+,?)+$/', $title, $matches)) { // special case for Picker module
|
| 186 |
foreach (array_map('intval', explode(',', $title)) as $nid2) {
|
| 187 |
if (($node2 = node_load($nid2))) {
|
| 188 |
$_SESSION['relations']['create'][$nid2] = $node->title;
|
| 189 |
}
|
| 190 |
}
|
| 191 |
}
|
| 192 |
else if (preg_match('!^(http|https)://!', $title)) {
|
| 193 |
foreach (explode(' ', trim($title)) as $url) {
|
| 194 |
$path = drupal_get_normal_path(substr($url, strlen($GLOBALS['base_url']) + 1));
|
| 195 |
if (preg_match('!node/(\d+)!', $path, $matches)) {
|
| 196 |
if (($node2 = node_load($nid2 = (int)$matches[1]))) {
|
| 197 |
$_SESSION['relations']['create'][$nid2] = $node->title;
|
| 198 |
}
|
| 199 |
}
|
| 200 |
}
|
| 201 |
}
|
| 202 |
else if (($node2 = relations_node_title_lookup($title))) {
|
| 203 |
$_SESSION['relations']['create'][$node2->nid] = $title;
|
| 204 |
}
|
| 205 |
|
| 206 |
$form_state = array('submitted' => FALSE);
|
| 207 |
$form = array('#post' => $_POST, '#programmed' => FALSE, '#tree' => FALSE, '#parents' => array());
|
| 208 |
$form['relations']['relations_table'] = relations_node_form($form_state, $node1, TRUE);
|
| 209 |
$form = form_builder('relations_node_form', $form, $form_state);
|
| 210 |
die(drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_render($form))));
|
| 211 |
}
|
| 212 |
|
| 213 |
function relations_node_session_delete($node2) {
|
| 214 |
$node1 = node_load($_POST['relations_table']['nid']);
|
| 215 |
if (isset($_SESSION['relations']['create'][$node2->nid])) {
|
| 216 |
unset($_SESSION['relations']['create'][$node2->nid]);
|
| 217 |
}
|
| 218 |
else {
|
| 219 |
$_SESSION['relations']['delete'][$node2->nid] = $node2->title;
|
| 220 |
}
|
| 221 |
|
| 222 |
$form_state = array('submitted' => FALSE);
|
| 223 |
$form = array('#post' => $_POST, '#programmed' => FALSE, '#tree' => FALSE, '#parents' => array());
|
| 224 |
$form['relations']['relations_table'] = relations_node_form($form_state, $node1, TRUE);
|
| 225 |
$form = form_builder('relations_node_form', $form, $form_state);
|
| 226 |
die(drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_render($form))));
|
| 227 |
}
|
| 228 |
|
| 229 |
//////////////////////////////////////////////////////////////////////////////
|
| 230 |
// Node theming
|
| 231 |
|
| 232 |
function theme_relations_node_form($form) {
|
| 233 |
drupal_add_css(drupal_get_path('module', 'relations') . '/relations.css', 'module', 'all', FALSE);
|
| 234 |
drupal_add_js(drupal_get_path('module', 'relations') . '/relations.js');
|
| 235 |
|
| 236 |
return theme('relations_node_table', $form, FALSE);
|
| 237 |
}
|
| 238 |
|
| 239 |
function theme_relations_node_table($form) {
|
| 240 |
global $user;
|
| 241 |
$node_types = node_get_types('names');
|
| 242 |
$is_new_node = empty($form['nid']['#value']);
|
| 243 |
$is_embedded = !empty($form['embedded']['#value']);
|
| 244 |
|
| 245 |
$head = array(t('Title'), t('Type'), t('Author'), array('data' => t('Operations'), 'colspan' => 1));
|
| 246 |
$rows = array();
|
| 247 |
|
| 248 |
// Output relationships stored in the database
|
| 249 |
if (!$is_new_node) {
|
| 250 |
$options = array('user' => $user);
|
| 251 |
|
| 252 |
// Support a ?type=TYPE query argument for filtering the relations tab by content type:
|
| 253 |
if (!empty($_GET['type'])) {
|
| 254 |
$options['type'] = explode(',', $_GET['type']);
|
| 255 |
}
|
| 256 |
|
| 257 |
$node = node_load($form['nid']['#value']);
|
| 258 |
foreach (relations_api_lookup($node->nid, $options) as $related) {
|
| 259 |
// Hide any previously-stored relationships that have been marked as
|
| 260 |
// deleted in a session when editing a node:
|
| 261 |
if (!empty($_SESSION['relations']['delete']) && isset($_SESSION['relations']['delete'][$related->nid])) {
|
| 262 |
continue;
|
| 263 |
}
|
| 264 |
|
| 265 |
$can_delete = user_access('delete node relations') && (node_access('update', $node) || node_access('update', $related));
|
| 266 |
$rows[] = array(
|
| 267 |
l($related->title, 'node/' . $related->nid),
|
| 268 |
isset($node_types[$related->type]) ? t($node_types[$related->type]) : $related->type,
|
| 269 |
theme('username', $related),
|
| 270 |
!$can_delete ? '' : theme_relations_delete_link($node->nid, $related->nid, $is_embedded),
|
| 271 |
);
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|
| 275 |
// Output relationships stored in the session (when editing a node)
|
| 276 |
if (!empty($_SESSION['relations']['create'])) {
|
| 277 |
foreach ($_SESSION['relations']['create'] as $nid => $title) {
|
| 278 |
if (($related = node_load($nid))) {
|
| 279 |
$rows[] = array(
|
| 280 |
l($related->title, 'node/' . $related->nid, array('attributes' => array('target' => '_blank'))),
|
| 281 |
isset($node_types[$related->type]) ? $node_types[$related->type] : $related->type,
|
| 282 |
theme('username', $related),
|
| 283 |
theme_relations_delete_link(NULL, $related->nid, $is_embedded),
|
| 284 |
);
|
| 285 |
}
|
| 286 |
}
|
| 287 |
}
|
| 288 |
|
| 289 |
// Output the "Add relationship" textbox and button
|
| 290 |
if (user_access('create node relations')) {
|
| 291 |
$rows[] = array(
|
| 292 |
drupal_render($form['title']),
|
| 293 |
'',
|
| 294 |
drupal_render($form['pick']),
|
| 295 |
array('data' => drupal_render($form['create']), 'colspan' => 1),
|
| 296 |
);
|
| 297 |
}
|
| 298 |
else {
|
| 299 |
unset($form['title'], $form['create']);
|
| 300 |
}
|
| 301 |
|
| 302 |
return drupal_render($form) . theme('table', $head, $rows, array('class' => 'relations'));
|
| 303 |
}
|
| 304 |
|
| 305 |
function theme_relations_delete_link($nid1, $nid2, $is_embedded = TRUE) {
|
| 306 |
$path = 'node/' . (!$is_embedded ? $nid1 : 'add') . '/relations/delete/' . $nid2;
|
| 307 |
return l(t('delete relationship'), $path, !$is_embedded ? array() : array(
|
| 308 |
'attributes' => array(
|
| 309 |
'id' => 'edit-relations-delete-' . $nid2,
|
| 310 |
'onclick' => 'return Drupal.relationsTriggerAHAH(this);',
|
| 311 |
),
|
| 312 |
));
|
| 313 |
}
|
| 314 |
|
| 315 |
//////////////////////////////////////////////////////////////////////////////
|
| 316 |
// Block theming
|
| 317 |
|
| 318 |
function theme_relations_node_block($node, array $options = array()) {
|
| 319 |
global $user;
|
| 320 |
drupal_add_css(drupal_get_path('module', 'relations') .'/relations.css', 'module', 'all', FALSE);
|
| 321 |
|
| 322 |
return theme('relations_node_list', relations_api_lookup($node->nid, array_merge(array('user' => $user), $options)));
|
| 323 |
}
|
| 324 |
|
| 325 |
function theme_relations_node_list($nodes) {
|
| 326 |
if (!empty($nodes)) {
|
| 327 |
$output = '<div class="item-list">';
|
| 328 |
$output .= '<ul>';
|
| 329 |
foreach ($nodes as $node) {
|
| 330 |
$output .= '<li>'. l($node->title, 'node/'. $node->nid) .'</li>';
|
| 331 |
}
|
| 332 |
$output .= '</ul>';
|
| 333 |
$output .= '</div>';
|
| 334 |
return $output;
|
| 335 |
}
|
| 336 |
}
|