| 1 |
<?php |
<?php |
| 2 |
// $Id: contemplate.module,v 1.8.4.62.2.1 2009/09/12 02:25:14 jrglasgow Exp $ |
// $Id: contemplate.module,v 1.8.4.62.2.2 2009/09/12 03:46:51 jrglasgow Exp $ |
| 3 |
// by Jeff Robbins - Lullabot - www.lullabot.com |
// by Jeff Robbins - Lullabot - www.lullabot.com |
| 4 |
|
|
| 5 |
define('CONTEMPLATE_TEASER_ENABLED', 0x0001); |
define('CONTEMPLATE_TEASER_ENABLED', 0x0001); |
| 56 |
*/ |
*/ |
| 57 |
function contemplate_menu() { |
function contemplate_menu() { |
| 58 |
$items = array(); |
$items = array(); |
| 59 |
$items['admin/content/types/templates'] = array( |
$items['admin/structure/types/templates'] = array( |
| 60 |
'title' => 'Content Templates', |
'title' => 'Content Templates', |
| 61 |
'description' => 'Create templates to customize output of teaser and body content.', |
'description' => 'Create templates to customize output of teaser and body content.', |
| 62 |
'page callback' => 'contemplate_edit_type', |
//'page callback' => 'contemplate_edit_type', |
| 63 |
|
'page callback' => 'contemplate_admin', |
| 64 |
'access arguments' => array('administer templates'), |
'access arguments' => array('administer templates'), |
| 65 |
'type' => MENU_LOCAL_TASK, |
'type' => MENU_LOCAL_TASK, |
| 66 |
'weight' => 7, |
'weight' => 7, |
| 72 |
'page arguments' => array('admin/content/types/templates'), |
'page arguments' => array('admin/content/types/templates'), |
| 73 |
'access arguments' => array('administer templates'), |
'access arguments' => array('administer templates'), |
| 74 |
); |
); |
| 75 |
$node_types = node_type_get_types(); |
$items['admin/structure/types/manage/%node_type/template'] = array( |
| 76 |
foreach ($node_types as $type) { |
'title' => 'Template', |
| 77 |
$type_url_str = str_replace('_', '-', $type->type); |
'page callback' => 'contemplate_edit_type', |
| 78 |
$items['admin/content/node-type/'. $type_url_str .'/template'] = array( |
'page arguments' => array(4), |
| 79 |
'title' => 'Template', |
'access arguments' => array('administer templates'), |
| 80 |
'page callback' => 'contemplate_edit_type', |
'type' => MENU_LOCAL_TASK, |
| 81 |
'page arguments' => array($type), |
'weight' => 10, |
| 82 |
'access arguments' => array('administer templates'), |
); |
| 83 |
'type' => MENU_LOCAL_TASK, |
$items['admin/config/config/contemplate'] = array( |
|
'weight' => 10, |
|
|
); |
|
|
} |
|
|
$items['admin/settings/contemplate'] = array( |
|
| 84 |
'title' => 'Content Template Settings', |
'title' => 'Content Template Settings', |
| 85 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 86 |
'page arguments' => array('contemplate_system_settings'), |
'page arguments' => array('contemplate_system_settings'), |
| 173 |
* Implementation of hook_node_build_alter(). |
* Implementation of hook_node_build_alter(). |
| 174 |
*/ |
*/ |
| 175 |
function contemplate_node_build_alter(&$node, $build_mode) { |
function contemplate_node_build_alter(&$node, $build_mode) { |
| 176 |
if ($template = contemplate_get_template($node->type)) { |
if ($template = contemplate_get_template(node_type_get_type($node))) { |
| 177 |
switch ($build_mode) { |
switch ($build_mode) { |
| 178 |
case 'teaser': |
case 'teaser': |
| 179 |
if (CONTEMPLATE_TEASER_ENABLED & $template['flags'] && trim($template['teaser'])) { // only if there's content in teaser field |
if (CONTEMPLATE_TEASER_ENABLED & $template['flags'] && trim($template['teaser'])) { // only if there's content in teaser field |
| 217 |
isset($templates[$type->type]['teaser-enabled']) ? ($templates[$type->type]['teaser-enabled'] ? ($templates[$type->type]['teaser-file'] ? $file : $enabled) : $disabled) : '', |
isset($templates[$type->type]['teaser-enabled']) ? ($templates[$type->type]['teaser-enabled'] ? ($templates[$type->type]['teaser-file'] ? $file : $enabled) : $disabled) : '', |
| 218 |
isset($templates[$type->type]['body-enabled']) ? ($templates[$type->type]['body-enabled'] ? ($templates[$type->type]['body-file'] ? $file : $enabled) : $disabled) : '', |
isset($templates[$type->type]['body-enabled']) ? ($templates[$type->type]['body-enabled'] ? ($templates[$type->type]['body-file'] ? $file : $enabled) : $disabled) : '', |
| 219 |
isset($templates[$type->type]['rss-enabled']) ? ($templates[$type->type]['rss-enabled'] ? ($templates[$type->type]['rss-file'] ? $file : $enabled) : $disabled) : '', |
isset($templates[$type->type]['rss-enabled']) ? ($templates[$type->type]['rss-enabled'] ? ($templates[$type->type]['rss-file'] ? $file : $enabled) : $disabled) : '', |
| 220 |
l(isset($templates[$type->type]) ? t('edit template') : t('create template'), 'admin/content/node-type/'. $type_url_str .'/template'), |
l(isset($templates[$type->type]) ? t('edit template') : t('create template'), 'admin/structure/types/manage/'. $type_url_str .'/template'), |
| 221 |
(isset($templates[$type->type]) ? l(t('delete template'), 'admin/content/node-type/'. $type_url_str .'/template/delete', array('query' => $destination)) : ''), |
(isset($templates[$type->type]) ? l(t('delete template'), 'admin/structure/types/manage/'. $type_url_str .'/template/delete', array('query' => $destination)) : ''), |
| 222 |
); |
); |
| 223 |
} |
} |
| 224 |
$header = array( |
$header = array( |
| 229 |
'', |
'', |
| 230 |
'', |
'', |
| 231 |
); |
); |
| 232 |
return theme('table', $header, $rows); |
return theme('table', array('header' => $header, 'rows' => $rows)); |
| 233 |
} |
} |
| 234 |
|
|
| 235 |
/** |
/** |
| 239 |
* @param string $type |
* @param string $type |
| 240 |
*/ |
*/ |
| 241 |
function contemplate_delete_type_form(&$form_state, $type = NULL) { |
function contemplate_delete_type_form(&$form_state, $type = NULL) { |
| 242 |
$form['type'] = array('#type' => 'value', '#value' => $type); |
$form['type'] = array('#type' => 'value', '#value' => $type->type); |
| 243 |
|
|
| 244 |
return confirm_form($form, |
return confirm_form($form, |
| 245 |
t('Are you sure you want to delete the template for %type?', array('%type' => $type)), |
t('Are you sure you want to delete the template for %type?', array('%type' => $type->name)), |
| 246 |
isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/types/templates', |
isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/types/templates', |
| 247 |
t('This action cannot be undone.'), |
t('This action cannot be undone.'), |
| 248 |
t('Delete'), t('Cancel')); |
t('Delete'), t('Cancel')); |
| 258 |
* |
* |
| 259 |
* @param string $type |
* @param string $type |
| 260 |
*/ |
*/ |
| 261 |
function contemplate_edit_type_form(&$form_state, $type = NULL) { |
function contemplate_edit_type_form(&$form, &$form_state, $type = NULL) { |
| 262 |
$example = contemplate_examples($type); |
$example = contemplate_examples($type); |
| 263 |
//drupal_set_message('$example = <pre>'. print_r($example, TRUE) .'</pre>'); |
//drupal_set_message('$example = <pre>'. print_r($example, TRUE) .'</pre>'); |
| 264 |
$template = contemplate_get_template($type); |
$template = contemplate_get_template($type); |
| 428 |
|
|
| 429 |
$form['type'] = array( |
$form['type'] = array( |
| 430 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 431 |
'#value' => $type, |
'#value' => $type->type, |
| 432 |
); |
); |
| 433 |
|
|
| 434 |
|
|
| 452 |
if (arg(0) == 'admin' && arg(1) == 'content' && arg(4) == 'template') { |
if (arg(0) == 'admin' && arg(1) == 'content' && arg(4) == 'template') { |
| 453 |
$breadcrumbs = drupal_get_breadcrumb(); |
$breadcrumbs = drupal_get_breadcrumb(); |
| 454 |
$type_crumb = array_pop($breadcrumbs); |
$type_crumb = array_pop($breadcrumbs); |
| 455 |
array_push($breadcrumbs, l(t('Templates'), 'admin/content/types/templates'), $type_crumb); |
array_push($breadcrumbs, l(t('Templates'), 'admin/structure/types/templates'), $type_crumb); |
| 456 |
drupal_set_breadcrumb($breadcrumbs); |
drupal_set_breadcrumb($breadcrumbs); |
| 457 |
} |
} |
| 458 |
if (arg(5) == 'delete') { |
if (arg(5) == 'delete') { |
| 459 |
return drupal_get_form('contemplate_delete_type_form', $type->type); |
return drupal_get_form('contemplate_delete_type_form', $type); |
| 460 |
} |
} |
| 461 |
else { |
else { |
| 462 |
contemplate_refresh_files(); |
contemplate_refresh_files(); |
| 463 |
return drupal_get_form('contemplate_edit_type_form', $type->type); |
return drupal_get_form('contemplate_edit_type_form', $type); |
| 464 |
} |
} |
| 465 |
} |
} |
| 466 |
|
|
| 473 |
//only load each template once per page hit |
//only load each template once per page hit |
| 474 |
static $types = array(); |
static $types = array(); |
| 475 |
|
|
| 476 |
if (!isset($types[$type])) { |
if (!isset($types[$type->type])) { |
| 477 |
|
$types[$type->type] = array(); |
| 478 |
|
$types[$type->type]['flags'] = 0; |
| 479 |
// first check to see what's stored in the contemplate table |
// first check to see what's stored in the contemplate table |
| 480 |
$types[$type] = (array)db_fetch_object(db_query("SELECT * FROM {contemplate} WHERE type = '%s'", $type));; |
$result = db_query("SELECT * FROM {contemplate} WHERE type = :type", array(':type' => $type->type)); |
| 481 |
if (empty($types[$type]['flags'])) { |
foreach ($result as $record) { |
| 482 |
$types[$type]['flags'] = 0; |
$types[$type->type] = (array)$record; |
| 483 |
|
} |
| 484 |
|
|
| 485 |
|
if (isset($types[$type->type]['flags']) && empty($types[$type->type]['flags'])) { |
| 486 |
|
$types[$type->type]['flags'] = 0; |
| 487 |
} |
} |
| 488 |
// now check to see if there are files |
// now check to see if there are files |
| 489 |
$fields = array( |
$fields = array( |
| 493 |
); |
); |
| 494 |
foreach ($fields as $field => $enable) { |
foreach ($fields as $field => $enable) { |
| 495 |
if ($file = contemplate_get_file($type, $field)) { |
if ($file = contemplate_get_file($type, $field)) { |
| 496 |
$types[$type][$field] = $file->contents; |
$types[$type->type][$field] = $file->contents; |
| 497 |
$types[$type][$field .'-file'] = $file; |
$types[$type->type][$field .'-file'] = $file; |
| 498 |
$types[$type]['flags'] |= $enable; // if there is a file, the field is always enabled... |
$types[$type->type]['flags'] |= $enable; // if there is a file, the field is always enabled... |
| 499 |
} |
} |
| 500 |
$types[$type][$field .'-enabled'] = $types[$type]['flags'] & $enable ? TRUE : FALSE; |
$types[$type->type][$field .'-enabled'] = $types[$type->type]['flags'] & $enable ? TRUE : FALSE; |
| 501 |
} |
} |
| 502 |
} |
} |
| 503 |
return $types[$type]; |
return $types[$type->type]; |
| 504 |
} |
} |
| 505 |
|
|
| 506 |
/** |
/** |
| 517 |
$templates[$r->type]['rss'] = $r->rss; |
$templates[$r->type]['rss'] = $r->rss; |
| 518 |
$templates[$r->type]['enclosure'] = $r->enclosure; |
$templates[$r->type]['enclosure'] = $r->enclosure; |
| 519 |
*/ |
*/ |
| 520 |
$templates[$r->type] = contemplate_get_template($r->type); |
$templates[$r->type] = contemplate_get_template($r); |
| 521 |
} |
} |
| 522 |
return $templates; |
return $templates; |
| 523 |
} |
} |
| 535 |
} |
} |
| 536 |
|
|
| 537 |
function contemplate_save($edit) { |
function contemplate_save($edit) { |
| 538 |
$type = $edit['values']['type']; |
$types = node_type_get_types(); |
| 539 |
|
$flags = 0; |
| 540 |
|
$type = $types[$edit['values']['type']]; |
| 541 |
$teaserfield = !empty($edit['values']['teaserfield']) ? $edit['values']['teaserfield'] : ''; |
$teaserfield = !empty($edit['values']['teaserfield']) ? $edit['values']['teaserfield'] : ''; |
| 542 |
$bodyfield = !empty($edit['values']['bodyfield']) ? $edit['values']['bodyfield'] : ''; |
$bodyfield = !empty($edit['values']['bodyfield']) ? $edit['values']['bodyfield'] : ''; |
| 543 |
$rssfield = !empty($edit['values']['rssfield']) ? $edit['values']['rssfield'] : ''; |
$rssfield = !empty($edit['values']['rssfield']) ? $edit['values']['rssfield'] : ''; |
| 547 |
$flags |= !empty($edit['values']['rss-enable']) ? CONTEMPLATE_RSS_ENABLED : 0; |
$flags |= !empty($edit['values']['rss-enable']) ? CONTEMPLATE_RSS_ENABLED : 0; |
| 548 |
|
|
| 549 |
contemplate_delete($type); |
contemplate_delete($type); |
| 550 |
$sql = "INSERT INTO {contemplate} (type, teaser, body, rss, enclosure, flags) VALUES ('%s', '%s', '%s', '%s', '%s', %d)"; |
$id = db_insert('contemplate') |
| 551 |
return db_query($sql, $type, $teaserfield, $bodyfield, $rssfield, $enclosure, $flags); |
->fields(array( |
| 552 |
|
'type' => $type->type, |
| 553 |
|
'teaser' => $teaserfield, |
| 554 |
|
'body' => $bodyfield, |
| 555 |
|
'rss' => $rssfield, |
| 556 |
|
'enclosure' => $enclosure, |
| 557 |
|
'flags' => $flags |
| 558 |
|
)) |
| 559 |
|
->execute(); |
| 560 |
|
return $id; |
| 561 |
} |
} |
| 562 |
|
|
| 563 |
function contemplate_delete($type) { |
function contemplate_delete($type) { |
| 564 |
return db_query("DELETE FROM {contemplate} WHERE type='%s'", $type); |
return db_delete('contemplate') |
| 565 |
|
->condition('type', $type->type) |
| 566 |
|
->execute(); |
| 567 |
} |
} |
| 568 |
|
|
| 569 |
/** |
/** |
| 576 |
static $data; |
static $data; |
| 577 |
if (!isset($data)) { |
if (!isset($data)) { |
| 578 |
$conf = conf_path(); |
$conf = conf_path(); |
| 579 |
$data = unserialize(db_result(db_query("SELECT data FROM {contemplate_files} WHERE site = '%s'", $conf))); |
$result = db_query("SELECT data FROM {contemplate_files} WHERE site = '%s'", array($conf)); |
| 580 |
|
foreach ($result as $record) { |
| 581 |
|
$data = unserialize($record->data); |
| 582 |
|
} |
| 583 |
} |
} |
| 584 |
return $data; |
return $data; |
| 585 |
} |
} |
| 594 |
function contemplate_refresh_files() { |
function contemplate_refresh_files() { |
| 595 |
$data = drupal_system_listing('/\.tpl\.php$/', 'contemplates', 'name', 0); |
$data = drupal_system_listing('/\.tpl\.php$/', 'contemplates', 'name', 0); |
| 596 |
$conf = conf_path(); |
$conf = conf_path(); |
| 597 |
db_query("DELETE FROM {contemplate_files} WHERE site = '%s'", $conf); |
$sql_args = array($conf, serialize($data)); |
| 598 |
db_query("INSERT INTO {contemplate_files} (site, data) VALUES ('%s', '%s')", $conf, serialize($data)); |
db_query("DELETE FROM {contemplate_files} WHERE site = '%s'", array($conf)); |
| 599 |
|
if (!empty($data)) { |
| 600 |
|
db_query("INSERT INTO {contemplate_files} (site, data) VALUES ('%s', '%s')", $sql_args); |
| 601 |
|
} |
| 602 |
} |
} |
| 603 |
|
|
| 604 |
|
|
| 617 |
* or FALSE if no file is found |
* or FALSE if no file is found |
| 618 |
* |
* |
| 619 |
*/ |
*/ |
| 620 |
function contemplate_get_file($node_type, $field) { |
function contemplate_get_file($type, $field) { |
| 621 |
$files = contemplate_available_files(); |
$files = contemplate_available_files(); |
| 622 |
if (isset($files['node-'. $node_type .'-'. $field .'.tpl'])) { |
if (isset($files['node-'. $type->type .'-'. $field .'.tpl'])) { |
| 623 |
$file = $files['node-'. $node_type .'-'. $field .'.tpl']; |
$file = $files['node-'. $type->type .'-'. $field .'.tpl']; |
| 624 |
} |
} |
| 625 |
elseif (isset($files['node-'. $node_type .'.tpl'])) { |
elseif (isset($files['node-'. $type->type .'.tpl'])) { |
| 626 |
$file = $files['node-'. $node_type .'.tpl']; |
$file = $files['node-'. $type->type .'.tpl']; |
| 627 |
} |
} |
| 628 |
elseif (isset($files['node.tpl'])) { |
elseif (isset($files['node.tpl'])) { |
| 629 |
$file = $files['node.tpl']; |
$file = $files['node.tpl']; |
| 650 |
*/ |
*/ |
| 651 |
function contemplate_node_views($type) { |
function contemplate_node_views($type) { |
| 652 |
// get the nid of the latest node of this type |
// get the nid of the latest node of this type |
| 653 |
$nid = db_result(db_query("SELECT nid FROM {node} WHERE type = '%s' ORDER BY created DESC", $type)); |
$result = db_query("SELECT nid FROM {node} WHERE type = '%s' ORDER BY created DESC", array($type->type)); |
| 654 |
if ($nid) { |
foreach ($result as $record) { |
| 655 |
|
error_log('$record = '. print_r($record, TRUE)); |
| 656 |
|
$nid = $record->nid; |
| 657 |
|
} |
| 658 |
|
if (isset($nid) && $nid) { |
| 659 |
$bodynode = contemplate_template_node_view(node_load($nid), 'full'); |
$bodynode = contemplate_template_node_view(node_load($nid), 'full'); |
| 660 |
$teasernode = contemplate_template_node_view(node_load($nid), 'teaser'); |
$teasernode = contemplate_template_node_view(node_load($nid), 'teaser'); |
| 661 |
return array('body' => $bodynode, 'teaser' => $teasernode); |
return array('body' => $bodynode, 'teaser' => $teasernode); |
| 687 |
$routput = contemplate_array_variables((array)$views['teaser'], 'rss', FALSE, FALSE, 0, $recursion_limit_hit); |
$routput = contemplate_array_variables((array)$views['teaser'], 'rss', FALSE, FALSE, 0, $recursion_limit_hit); |
| 688 |
} |
} |
| 689 |
else { |
else { |
| 690 |
$error = t('No %type content items exist to use as an example. Please create a %type item and then come back here to see an output of its parts.', array('%type' => $type)); |
$error = t('No %type content items exist to use as an example. Please create a %type item and then come back here to see an output of its parts.', array('%type' => $type->type)); |
| 691 |
$toutput = $boutput = $routput = $error; |
$toutput = $boutput = $routput = $error; |
| 692 |
} |
} |
| 693 |
if ($recursion_limit_hit) { |
if ($recursion_limit_hit) { |