| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: formattedtitle.module,v 1.1 2009/03/28 04:09:48 develCuy Exp $ |
|
|
|
|
define('FORMATTEDTITLE_BOLD', 0x0001); |
|
|
define('FORMATTEDTITLE_ITALIC', 0x0002); |
|
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 6 |
*/ |
*/ |
| 7 |
function formattedtitle_form_alter(&$form, $form_state, $form_id) { |
function formattedtitle_form_alter(&$form, $form_state, $form_id) { |
| 8 |
if ($form['#node']->type .'_node_form' == $form_id) { |
if ($form['#node']->type .'_node_form' == $form_id) { |
| 9 |
$form['title_format'] = array( |
if (!isset($form['title']['#description'])) { |
| 10 |
'#type' => 'fieldset', |
$form['title']['#description'] = ''; |
| 11 |
'#title' => t('Title format'), |
} |
| 12 |
'#collapsible' => TRUE, |
$form['title']['#description'] .= '<strong>Formatted title:</strong> use **word** for <strong>bold</strong> and __word__ for <i>italic</i>'; |
|
'#access' => TRUE, |
|
|
'#weight' => $form['title']['#weight'] + 0.001, |
|
|
'#tree' => TRUE, |
|
|
'bold' => array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('bold'), |
|
|
'#default_value' => formattedtitle_get_format('bold', $form['#node']->nid), |
|
|
), |
|
|
'italic' => array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('italic'), |
|
|
'#default_value' => formattedtitle_get_format('italic', $form['#node']->nid), |
|
|
), |
|
|
); |
|
| 13 |
} |
} |
| 14 |
} |
} |
| 15 |
|
|
| 16 |
function formattedtitle_get_format($format, $node_id) { |
function formattedtitle_get($node_id) { |
| 17 |
$properties = db_result(db_query('SELECT properties FROM {formattedtitle} WHERE nid = %d', $node_id)); |
return db_result(db_query('SELECT title FROM {formattedtitle} WHERE nid = %d', $node_id)); |
| 18 |
switch ($format) { |
} |
| 19 |
case 'bold'; |
|
| 20 |
if ($properties & FORMATTEDTITLE_BOLD) { |
function formattedtitle_set($title, $node_id) { |
| 21 |
return TRUE; |
db_query('DELETE FROM {formattedtitle} WHERE nid = %d', $node_id); |
| 22 |
} |
if (strlen($title)) { |
| 23 |
break; |
db_query('INSERT INTO {formattedtitle}(nid, title) VALUES(%d, "%s")', $node_id, $title); |
|
case 'italic': |
|
|
if ($properties & FORMATTEDTITLE_ITALIC) { |
|
|
return TRUE; |
|
|
} |
|
|
break; |
|
| 24 |
} |
} |
| 25 |
} |
} |
| 26 |
|
|
| 27 |
function formattedtitle_set_format($format, $node_id) { |
function formattedtitle_check($title) { |
| 28 |
if (is_array($format)) { |
return !(strpos($title, '__') === FALSE) || !(strpos($title, '**') === FALSE); |
| 29 |
$properties = 0; |
} |
| 30 |
if ($format['bold']) { |
|
| 31 |
$properties = $properties | FORMATTEDTITLE_BOLD; |
function formattedtitle_parse($title) { |
| 32 |
} |
$rules = array( |
| 33 |
if ($format['italic']) { |
'/\*\*([^(\*\*)]*)\*\*/' => '<strong>${1}</strong>', |
| 34 |
$properties = $properties | FORMATTEDTITLE_ITALIC; |
'/__([^(__)]*)__/' => '<i>${1}</i>', |
| 35 |
} |
); |
| 36 |
db_query('DELETE FROM {formattedtitle} WHERE nid = %d', $node_id); |
foreach ($rules as $r => $f) { |
| 37 |
if ($properties) { |
$title = preg_replace($r, $f, $title); |
|
db_query('INSERT INTO {formattedtitle}(nid, properties) VALUES(%d, %d)', $node_id, $properties); |
|
|
} |
|
| 38 |
} |
} |
| 39 |
|
return $title; |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
/** |
/** |
| 45 |
function formattedtitle_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
function formattedtitle_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { |
| 46 |
switch ($op) { |
switch ($op) { |
| 47 |
case 'presave': |
case 'presave': |
| 48 |
if (isset($node->title_format)) { |
if (formattedtitle_check($node->title)) { |
| 49 |
formattedtitle_set_format($node->title_format, $node->nid); |
formattedtitle_set(formattedtitle_parse($node->title), $node->nid); |
| 50 |
} |
} |
| 51 |
break; |
break; |
|
|
|
| 52 |
case 'view': |
case 'view': |
| 53 |
if (formattedtitle_get_format('bold', $node->nid)) { |
$custom_title = formattedtitle_get($node->nid) .''; |
| 54 |
$node->title = '<i>'. $node->title .'</i>'; |
if (strlen($custom_title)) { |
| 55 |
} |
drupal_set_title($custom_title); |
|
if (formattedtitle_get_format('italic', $node->nid)) { |
|
|
$node->title = '<b>'. $node->title .'</b>'; |
|
| 56 |
} |
} |
|
drupal_set_title($node->title); |
|
| 57 |
break; |
break; |
| 58 |
} |
} |
| 59 |
} |
} |