Created
January 6, 2009 21:56
-
-
Save delonnewman/44028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implementation of hook_menu(). | |
*/ | |
function pdresources_menu() { | |
$items['resources'] = array( | |
'title' => 'Resources', | |
'page callback' => 'display_resources', | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK | |
); | |
/*$items['resources/employers'] = array( | |
'title' => 'Employer Resources', | |
'page callback' => 'display_employer_resources', | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK | |
); | |
$items['resources/providers'] = array( | |
'title' => 'Provider Resources', | |
'page callback' => 'display_provider_resources', | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK | |
);*/ | |
return $items; | |
} | |
function display_resources() { | |
$html = '<ul class="links">'; | |
$html .= '<li>' . l('Employer Resources', 'resources/employer') .'</li>'; | |
$html .= '<li>' . l('Provider Resources', 'resources/provider') .'</li>'; | |
$html .= '</ul>'; | |
// FIXME: These should be set up through hook_menu(). | |
if (arg(1) == 'provider') { | |
$html .= '<h2>Provider Resources</h2>'; | |
$html .= display_resources_by_type('provider_resource'); | |
} elseif (arg(1) == 'employer') { | |
$html .= '<h2>Employer Resources</h2>'; | |
$html .= display_resources_by_type('employer_resource'); | |
} | |
return $html; | |
} | |
function display_resources_by_type($type) { | |
$category_ids = _get_category_ids_by_node_type($type); | |
foreach ($category_ids as $category_id) { | |
$category_name = _get_category_name($category_id); | |
$html .= '<div id="resource-block">'; | |
$html .= '<h3>' . _get_category_title($category_name) . '</h3>'; | |
if ($category_name == 'forms') { | |
$cols = 2; | |
$items = 7; | |
$node_ids = _get_form_node_ids(); | |
} else { | |
$cols = 1; | |
$items = 5; | |
$node_ids = _get_node_ids_by_category($category_id); | |
} | |
$html .= format_node_list($node_ids, $cols, $items, $category_name); | |
$html .= '</div>'; | |
} | |
return $html; | |
} | |
function format_node_list($nodes, $cols=2, $items=7, $category_name=NULL) { | |
$num_cols = 0; | |
$num_items = 0; | |
$html = "<ul>\n"; | |
foreach ($nodes as $node) { | |
$html .= format_node($node, $category_name); | |
$num_items++; | |
if ($num_items == $items) { | |
$num_items = 0; | |
$num_cols++; | |
if ($num_cols == $cols) { | |
$num_cols = 0; | |
break; | |
} | |
$html .= "</ul>\n<ul>\n"; | |
} | |
} | |
$html .= "</ul>\n"; | |
return $html; | |
} | |
function format_node($node_id, $category_name=NULL) { | |
if ($category_name == 'forms') { | |
$path = _get_node_attachment_path($node_id); | |
$html .= '<li><a href="' . $path . '">' . _get_node_title($node_id) . '</a></li>'; | |
} else { | |
$html .= '<li>' . l(_get_node_title($node_id), 'node/' . $node_id) . '</li>'; | |
} | |
return $html; | |
} | |
function _get_category_title($category_name) { | |
$title = array( | |
'forms' => 'Forms, Contracts & Logistics', | |
'employing' => 'Employing Domestic Help', | |
'perfectly domestic' => 'Getting Perfectly Domestic', | |
'balancing' => 'Balancing it all... Enjoy!', | |
'childcare' => 'Childcare Providers', | |
'assistants' => 'Personal Assistants', | |
'housekeepers' => 'Housekeepers' | |
); | |
return $title[$category_name]; | |
} | |
function _get_nodes_by_type($type) { | |
return db_query("SELECT nid FROM node WHERE type = '%s'", $type); | |
} | |
function _get_node_title($node_id) { | |
return db_result(db_query("SELECT title FROM node WHERE nid = %d", $node_id)); | |
} | |
function _get_category_name($category_id) { | |
return db_result(db_query("SELECT name FROM term_data WHERE tid = %d", $category_id)); | |
} | |
function _get_node_categories($node_id) { | |
return db_query("SELECT term_data.name as category FROM term_node, term_data WHERE term_node.tid = term_data.tid AND term_node.nid = %d", $node_id); | |
} | |
function _get_node_ids_by_category($category_id) { | |
return _get_array_from_result(db_query("SELECT node.nid FROM term_node, node WHERE term_node.nid = node.nid AND term_node.tid = %d", $category_id), 'nid'); | |
} | |
function _get_form_node_ids() { | |
return _get_array_from_result(db_query("SELECT node.nid FROM node, term_node, term_data WHERE node.nid = term_node.nid AND term_node.tid = term_data.tid AND term_data.name = 'forms'"), 'nid'); | |
} | |
function _get_node_attachment_path($node_id) { | |
return 'sites/default/files/' . db_result(db_query("SELECT description AS path FROM upload WHERE nid = %d", $node_id)); | |
} | |
function _node_is_in_category($node_id, $category_id) { | |
$tid = (int)db_result(db_query("SELECT tid FROM term_node WHERE nid = %d", (int)$node_id)); | |
if ($category_id == $tid) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
function _get_category_ids_by_node_type($node_type) { | |
return _get_array_from_result(db_query("SELECT term_node.tid FROM node, term_node WHERE node.nid = term_node.nid AND node.type = '%s' ORDER BY tid", $node_type), 'tid'); | |
} | |
function _get_array_from_result($result, $index_field=NULL) { | |
$results = array(); | |
if ($index_field) { | |
while ($row = db_fetch_array($result)) { | |
array_push($results, $row[$index_field]); | |
} | |
} else { | |
while ($row = db_fetch_array($result)) { | |
array_push($results, $row); | |
} | |
} | |
return $results; | |
} | |
function _get_category_id($category_name) { | |
return (int)db_result(db_query("SELECT tid FROM term_data WHERE name = '%s'", $category_name)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment