|
| 1 | +<?php |
| 2 | + |
| 3 | +function ding_path_alias_library_bookpc_page_pathauto_bulk_update_batch_process(&$context) { |
| 4 | + _ding_path_alias_bulk_update_batch_process($context, 'bookpc', 'bookpc'); |
| 5 | +} |
| 6 | + |
| 7 | +function ding_path_alias_library_facilities_page_pathauto_bulk_update_batch_process(&$context) { |
| 8 | + _ding_path_alias_bulk_update_batch_process($context, 'facilities', 'faciliteter'); |
| 9 | +} |
| 10 | + |
| 11 | +function ding_path_alias_library_local_page_pathauto_bulk_update_batch_process(&$context) { |
| 12 | + _ding_path_alias_bulk_update_batch_process($context, 'local', 'lokalt'); |
| 13 | +} |
| 14 | + |
| 15 | +function ding_path_alias_library_about_page_pathauto_bulk_update_batch_process(&$context) { |
| 16 | + _ding_path_alias_bulk_update_batch_process($context, 'about', 'om'); |
| 17 | +} |
| 18 | + |
| 19 | +function ding_path_alias_library_blog_page_pathauto_bulk_update_batch_process(&$context) { |
| 20 | + _ding_path_alias_bulk_update_batch_process($context, 'blog', 'blog'); |
| 21 | +} |
| 22 | + |
| 23 | +function ding_path_alias_library_event_page_pathauto_bulk_update_batch_process(&$context) { |
| 24 | + _ding_path_alias_bulk_update_batch_process($context, 'event', 'arrangementer'); |
| 25 | +} |
| 26 | + |
| 27 | +function _ding_path_alias_bulk_update_batch_process(&$context, $name, $url_suffix) { |
| 28 | + if (!isset($context['sandbox']['current'])) { |
| 29 | + $context['sandbox']['count'] = 0; |
| 30 | + $context['sandbox']['current'] = 0; |
| 31 | + } |
| 32 | + |
| 33 | + $concat = _pathauto_sql_concat("'node/'", 'n.nid', "'/$url_suffix'"); |
| 34 | + |
| 35 | + $sql = " |
| 36 | + SELECT n.nid FROM {node} AS n |
| 37 | + LEFT JOIN {url_alias} AS ua ON $concat = ua.src |
| 38 | + WHERE ua.src IS NULL AND n.type = 'library' AND n.nid > %d |
| 39 | + ORDER BY n.nid |
| 40 | + "; |
| 41 | + |
| 42 | + $args = array($context['sandbox']['current']); |
| 43 | + |
| 44 | + // Get the total amount of items to process. |
| 45 | + if (!isset($context['sandbox']['total'])) { |
| 46 | + $context['sandbox']['total'] = db_result(db_query(_pathauto_sql_count($sql), $args)); |
| 47 | + |
| 48 | + // If there are no nodes to update, the stop immediately. |
| 49 | + if (empty($context['sandbox']['total'])) { |
| 50 | + $context['finished'] = 1; |
| 51 | + return; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + $query = db_query_range($sql, $args, 0, 25); |
| 56 | + $nids = array(); |
| 57 | + while ($nid = db_result($query)) { |
| 58 | + $nids[] = $nid; |
| 59 | + } |
| 60 | + |
| 61 | + // Update the alias for each selected node. |
| 62 | + ding_path_alias_update_alias_multiple($nids, 'bulkupdate', array( |
| 63 | + 'name' => $name, |
| 64 | + 'url_suffix' => $url_suffix, |
| 65 | + )); |
| 66 | + |
| 67 | + $context['sandbox']['count'] += count($nids); |
| 68 | + $context['sandbox']['current'] = max($nids); |
| 69 | + $context['message'] = t('Updated @type alias for library @nid.', array('@type' => $url_suffix, '@nid' => end($nids))); |
| 70 | + |
| 71 | + if ($context['sandbox']['count'] != $context['sandbox']['total']) { |
| 72 | + $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total']; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Update the URL aliases for an individual node. |
| 78 | + * |
| 79 | + * @param $node |
| 80 | + * A node object. |
| 81 | + * @param $op |
| 82 | + * Operation being performed on the node ('insert', 'update' or 'bulkupdate'). |
| 83 | + * @param $options |
| 84 | + * An optional array of additional options. |
| 85 | + */ |
| 86 | +function ding_path_alias_update_alias($node, $op, $options = array()) { |
| 87 | + // Skip processing if the user has disabled pathauto for the node. |
| 88 | + if (isset($node->pathauto_perform_alias) && empty($node->pathauto_perform_alias)) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $options += array( |
| 93 | + 'language' => isset($node->language) ? $node->language : '', |
| 94 | + ); |
| 95 | + |
| 96 | + // Skip processing if the node has no pattern. |
| 97 | + if (!pathauto_pattern_load_by_entity('library_' . $options['name'] . '_page', '', $options['language'])) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + module_load_include('inc', 'pathauto'); |
| 102 | + if ($alias = pathauto_create_alias('library_' . $options['name'] . '_page', $op, 'node/' . $node->nid . '/' . $options['url_suffix'], array('node' => $node), $node->nid, $node->type, $options['language'])) { |
| 103 | + $node->path = $alias; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Update the URL aliases for multiple nodes. |
| 109 | + * |
| 110 | + * @param $nids |
| 111 | + * An array of node IDs. |
| 112 | + * @param $op |
| 113 | + * Operation being performed on the nodes ('insert', 'update' or |
| 114 | + * 'bulkupdate'). |
| 115 | + * @param $options |
| 116 | + * An optional array of additional options. |
| 117 | + */ |
| 118 | +function ding_path_alias_update_alias_multiple($nids, $op, $options = array()) { |
| 119 | + $options += array('message' => FALSE); |
| 120 | + |
| 121 | + foreach ($nids as $nid) { |
| 122 | + if ($node = node_load($nid, NULL, TRUE)) { |
| 123 | + ding_path_alias_update_alias($node, $op, $options); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (!empty($options['message'])) { |
| 128 | + drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.')); |
| 129 | + } |
| 130 | +} |
| 131 | + |
0 commit comments