| 17 |
/** |
/** |
| 18 |
* Replacement callback for nodetitle plugin |
* Replacement callback for nodetitle plugin |
| 19 |
*/ |
*/ |
| 20 |
function freelinking_nodetitle_callback($target) { // resolve $target into a link to the node or a create link |
function freelinking_nodetitle_callback($target) { |
| 21 |
$sql = 'SELECT nid FROM {node} WHERE title = "%s" '; |
// Identify and remove language argument (de:Pagename) |
| 22 |
if (variable_get('freelinking_nodetitle_searchcontenttype', 'none') != 'none') { // restrict title search to a content type |
if (preg_match('/^(\w\w):/', $target['dest'], $matches)) { |
| 23 |
$sql .= 'AND type ="%s" '; |
$language = $matches[1]; |
| 24 |
$result = db_query($sql, $target['dest'], variable_get('freelinking_nodetitle_searchcontenttype'), 'none'); |
$target['dest'] = substr($target['dest'], 3); |
| 25 |
} // endif title search restriction |
} |
| 26 |
else { |
// get the nid and best guess at language for the title. |
| 27 |
$result = db_query($sql, $target['dest']); |
list($nid, $language) = |
| 28 |
} // endifelse no title search restriction |
_freelinking_nodetitle_get_nid_by_lang($target['dest'], $language); |
| 29 |
while ($node = db_fetch_object($result)) { // should be only one |
|
| 30 |
$nid = $node->nid; |
// if no node was found, identify proceed with configured failover |
| 31 |
} // endwhile looping through (one) node |
if (!$nid) { |
| 32 |
|
return _freelinking_nodetitle_failure($target); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
// construct values for link |
| 36 |
$title = $target['text'] ? $target['text'] : $target['dest']; |
$title = $target['text'] ? $target['text'] : $target['dest']; |
| 37 |
if ($nid) { // I got your freelink right here |
if (!$target['tooltip']) { |
| 38 |
$replacement = array($title, 'node/' . $nid); |
$target['tooltip'] = freelinking_internal_tooltip('node', $nid); |
| 39 |
} // endif a node was found matching the target |
} |
| 40 |
else { // Didn't find one. Present a link to create/do a search |
|
| 41 |
$notfoundaction = variable_get('freelinking_nodetitle_notfoundaction', 'create'); |
// return link structure |
| 42 |
if ($notfoundaction == 'create' or ($notfoundaction == 'noaccesssearch' && node_access('create', variable_get('freelinking_nodetitle_newcontenttype', 'story')))) { |
return array($title, 'node/' . $nid, array( |
| 43 |
$replacement = array($target['dest'], 'node/add/' . variable_get('freelinking_nodetitle_newcontenttype', 'story') , array( |
'attributes' => array('title' => $tooltip), |
| 44 |
'attributes' => array( |
'language' => $language, |
| 45 |
'class' => 'notfound', |
)); |
|
'title' => 'Create content titled "' . $target['dest'] . '"', |
|
|
), |
|
|
'query' => array('edit[title]' => $target['dest']), |
|
|
)); |
|
|
} // endif create |
|
|
else { // either no create action or no access rights, so search |
|
|
return array('plugin' => 'search'); |
|
|
} // endifelse search |
|
|
} // endifelse no node found |
|
|
return $replacement; |
|
| 46 |
} |
} |
| 47 |
|
|
| 48 |
/** |
/** |
| 49 |
* Nodetitle Settings Callback |
* Nodetitle Settings Callback |
| 50 |
*/ |
*/ |
| 51 |
function freelinking_nodetitle_settings() { // settings for the nodetitle plugin |
function freelinking_nodetitle_settings() { |
| 52 |
$form['nodetitle']['freelinking_nodetitle_newcontenttype'] = array( |
// Restrict nodetitle plugin to search specified content type |
|
'#title' => t('Default for new content'), |
|
|
'#type' => 'select', |
|
|
'#options' => node_get_types('names'), |
|
|
'#default_value' => variable_get('freelinking_nodetitle_newcontenttype', 'story'), |
|
|
'#description' => t('Content that is to be created from a freelink should be of this type.'), |
|
|
); |
|
| 53 |
$form['nodetitle']['freelinking_nodetitle_searchcontenttype'] = array( |
$form['nodetitle']['freelinking_nodetitle_searchcontenttype'] = array( |
| 54 |
'#title' => t('Restrict freelinks to this content type'), |
'#title' => t('Restrict freelinks to this content type'), |
| 55 |
'#type' => 'select', |
'#type' => 'select', |
| 58 |
'#description' => t('Lookup by title to find a freelink will be restricted to this content type only.'), |
'#description' => t('Lookup by title to find a freelink will be restricted to this content type only.'), |
| 59 |
); |
); |
| 60 |
|
|
| 61 |
$failover_option['create'] = t('Add a link to create content. (Without Permission: Access Denied)'); |
$failover_option['none'] = t('Do nothing'); |
| 62 |
if (module_exists('search') && user_access('search content')) { |
|
| 63 |
$failover_option['noaccesssearch'] = t('Add a link to create content. (Without Permission: Search Content)'); |
// if Create Node plugin is available, it's an option! |
| 64 |
$failover_option['search'] = t('Add a link to Search Content.'); |
if (module_exists('freelinking_create')) { |
| 65 |
} |
$failover_option['create'] = t('Add a link to create content. (Without Permission: Access Denied)'); |
| 66 |
else if ($search_plugin = variable_get('freelinking_search_failover', 'error') != 'error') { |
} |
| 67 |
$failover_option['noaccesssearch'] = |
else { |
| 68 |
t('Add a link to create content. (Without Permission: %search Search Content)', |
$extra_description ='<br /><strong>' |
| 69 |
array('%search' => ucfirst($search_plugin))); |
. t('Note: Enable %module for Content Creation option.', array('%module' => 'Freelinking Create')) |
| 70 |
$failover_option['search'] = t('Add a link to %search Search Content.', |
. '</strong>'; |
|
array('%search' => ucfirst($search_plugin))); |
|
| 71 |
} |
} |
| 72 |
|
|
| 73 |
|
// if search is available, have a search failover |
| 74 |
|
if (module_exists('search')) { |
| 75 |
|
$failover_option['search'] = t('Add a link to Search Content'); |
| 76 |
|
} |
| 77 |
|
else { |
| 78 |
|
$extra_description .= '<br /><strong>' |
| 79 |
|
. t('Note: Enable the %module module for internal search option.', array('%module' => 'Search')) |
| 80 |
|
. '</strong>'; |
| 81 |
|
// if search is unavailable offer it's own (applicable)search fallback. [Google, etc] |
| 82 |
|
if (($search_plugin = variable_get('freelinking_search_failover', 'error')) != 'error') { |
| 83 |
|
$failover_option['search'] = t('Add a link to %search Search Content.', |
| 84 |
|
array('%search' => ucfirst($search_plugin))); |
| 85 |
|
} |
| 86 |
|
} |
| 87 |
|
$failover_option['error'] = t('Insert an error message'); |
| 88 |
|
|
| 89 |
$form['nodetitle']['freelinking_nodetitle_notfoundaction'] = array( |
$form['nodetitle']['freelinking_nodetitle_notfoundaction'] = array( |
| 90 |
'#title' => t('If a suitable content is not found'), |
'#title' => t('If a suitable content is not found'), |
| 91 |
'#type' => 'select', |
'#type' => 'select', |
| 92 |
'#options' => $failover_option, |
'#options' => $failover_option, |
| 93 |
'#default_value' => variable_get('freelinking_nodetitle_notfoundaction', 'create'), |
'#default_value' => variable_get('freelinking_nodetitle_notfoundaction', |
| 94 |
'#description' => t('What should freelinking do when content to link to is not found?'), |
_freelinking_nodetitle_default_failover()), |
| 95 |
|
'#description' => t('What should freelinking do when the page is not found?') |
| 96 |
|
. $extra_description, |
| 97 |
); |
); |
| 98 |
return $form; |
return $form; |
| 99 |
} // endfunction freelinking_freelinking_settings() |
} // endfunction freelinking_freelinking_settings() |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Grab the nid associated with the title. |
| 103 |
|
* Attempt some degree of language sensibility |
| 104 |
|
*/ |
| 105 |
|
function _freelinking_nodetitle_get_nid_by_lang($title, $node_lang = NULL) { |
| 106 |
|
global $language; |
| 107 |
|
|
| 108 |
|
// Run a language-sensitive query |
| 109 |
|
if (module_exists('locale')) { |
| 110 |
|
// if a language is specified, ignore the global |
| 111 |
|
if ($node_lang) { |
| 112 |
|
$language = $node_lang; |
| 113 |
|
} |
| 114 |
|
$sql = "SELECT nid, language FROM {node} WHERE title = '%s' and language = '%s'"; |
| 115 |
|
if (variable_get('freelinking_nodetitle_searchcontenttype', 'none') != 'none') { |
| 116 |
|
$sql .= " AND type = '%s'"; |
| 117 |
|
$result = db_query($sql, $title, $language, |
| 118 |
|
variable_get('freelinking_nodetitle_searchcontenttype'), 'none'); |
| 119 |
|
} |
| 120 |
|
else { |
| 121 |
|
$result = db_query($sql, $title, $language); |
| 122 |
|
} |
| 123 |
|
} |
| 124 |
|
// if locale does not exist or it provided an empty result, get anything. |
| 125 |
|
if (empty($result)) { |
| 126 |
|
$sql = "SELECT nid FROM {node} WHERE title = '%s'"; |
| 127 |
|
if (variable_get('freelinking_nodetitle_searchcontenttype', 'none') != 'none') { |
| 128 |
|
$sql .= " AND type = '%s'"; |
| 129 |
|
$result = db_query($sql, $title, |
| 130 |
|
variable_get('freelinking_nodetitle_searchcontenttype'), 'none'); |
| 131 |
|
} |
| 132 |
|
else { |
| 133 |
|
$result = db_query($sql, $title); |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
$node = db_fetch_object($result); |
| 137 |
|
// get the nid and it's language |
| 138 |
|
if(isset($node)) { |
| 139 |
|
return array($node->nid, $node->language); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
return; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
/** |
| 146 |
|
* Determining the proper failure response per plugin configuration |
| 147 |
|
*/ |
| 148 |
|
function _freelinking_nodetitle_failure($target) { |
| 149 |
|
$failover = variable_get('freelinking_nodetitle_notfoundaction', |
| 150 |
|
_freelinking_nodetitle_default_failover()); |
| 151 |
|
|
| 152 |
|
switch($failover) { |
| 153 |
|
case 'create': |
| 154 |
|
return array('plugin' => 'create node'); |
| 155 |
|
case 'search': |
| 156 |
|
return array('plugin' => 'search'); |
| 157 |
|
case 'error': |
| 158 |
|
return array('error' => t('%title not found', |
| 159 |
|
array('%title' => $target['dest']))); |
| 160 |
|
} |
| 161 |
|
// do nothing |
| 162 |
|
return FALSE; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
/** |
| 166 |
|
* Find the best default failover |
| 167 |
|
*/ |
| 168 |
|
function _freelinking_nodetitle_default_failover() { |
| 169 |
|
if (module_exists('freelinking_create')) { |
| 170 |
|
return 'create node'; |
| 171 |
|
} |
| 172 |
|
else if (module_exists('search')) { |
| 173 |
|
return 'search'; |
| 174 |
|
} |
| 175 |
|
return 'none'; |
| 176 |
|
} |
| 177 |
|
|
| 178 |
// vim: tw=300 nowrap syn=php |
// vim: tw=300 nowrap syn=php |