| 1 |
<?php
|
| 2 |
// $Id: directory.module,v 1.1 2009/02/09 07:43:05 fgm Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* A mini demo directory module. For an actual directory module:
|
| 6 |
* @link http://drupal.org/project/directory @endlink
|
| 7 |
*
|
| 8 |
* @copyright (c) 2009 Ouest Systèmes Informatiques (OSInet)
|
| 9 |
* @author Frédéric G. MARAND
|
| 10 |
* @license General Public License version 2 and later
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implement the former hook_settings(), build section.
|
| 15 |
*
|
| 16 |
* @return array
|
| 17 |
*/
|
| 18 |
function _directory_admin_build()
|
| 19 |
{
|
| 20 |
$form = array();
|
| 21 |
|
| 22 |
$arVoc = array();
|
| 23 |
foreach (taxonomy_get_vocabularies() as $voc)
|
| 24 |
{
|
| 25 |
$arVoc[$voc->vid] = $voc->name;
|
| 26 |
}
|
| 27 |
$form['directory-vid'] = array
|
| 28 |
(
|
| 29 |
'#title' => t('Vocabulary'),
|
| 30 |
'#type' => 'select',
|
| 31 |
'#default_value' => variable_get('directory-vid', 0),
|
| 32 |
'#options' => $arVoc,
|
| 33 |
);
|
| 34 |
$arTypes = array();
|
| 35 |
foreach (node_get_types('types') as $typeType => $type)
|
| 36 |
{
|
| 37 |
$arTypes[$typeType] = $type->name;
|
| 38 |
}
|
| 39 |
$form['directory-types'] = array
|
| 40 |
(
|
| 41 |
'#title' => t('Node types allowed in directory'),
|
| 42 |
'#type' => 'select',
|
| 43 |
'#default_value' => variable_get('directory-types', array()),
|
| 44 |
'#options' => $arTypes,
|
| 45 |
'#multiple' => TRUE,
|
| 46 |
);
|
| 47 |
return system_settings_form($form);
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implement hook_menu().
|
| 52 |
*
|
| 53 |
* @return array
|
| 54 |
*/
|
| 55 |
function directory_menu()
|
| 56 |
{
|
| 57 |
$items['directory'] = array
|
| 58 |
(
|
| 59 |
'title' => 'Directory',
|
| 60 |
'page callback' => '_directory_page',
|
| 61 |
'access arguments' => array('access content'),
|
| 62 |
);
|
| 63 |
$items['admin/build/directory'] = array
|
| 64 |
(
|
| 65 |
'title' => 'Directory',
|
| 66 |
'page callback' => 'drupal_get_form',
|
| 67 |
'page arguments' => array('_directory_admin_build'),
|
| 68 |
'access arguments' => array('administer site configuration'),
|
| 69 |
);
|
| 70 |
return $items;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Implement hook_theme().
|
| 75 |
*
|
| 76 |
* @return array
|
| 77 |
*/
|
| 78 |
function directory_theme()
|
| 79 |
{
|
| 80 |
$ret = array
|
| 81 |
(
|
| 82 |
'directory_parents' => array
|
| 83 |
(
|
| 84 |
'arguments' => array('parents' => array(), 'term' => new stdClass()),
|
| 85 |
'template' => 'directory_parents',
|
| 86 |
),
|
| 87 |
'directory_children' => array
|
| 88 |
(
|
| 89 |
'arguments' => array('children' => array()),
|
| 90 |
'template' => 'directory_children',
|
| 91 |
),
|
| 92 |
'directory_nodes' => array
|
| 93 |
(
|
| 94 |
'arguments' => array('nodes' => array()),
|
| 95 |
'template' => 'directory_nodes',
|
| 96 |
),
|
| 97 |
);
|
| 98 |
return $ret;
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Obtain a simplified term hierarchy for a valid tid
|
| 103 |
*
|
| 104 |
* @param integer $tid
|
| 105 |
* @return array
|
| 106 |
*/
|
| 107 |
function _directory_get_terms($tid = 0)
|
| 108 |
{
|
| 109 |
$ret = array();
|
| 110 |
|
| 111 |
$term = taxonomy_get_term($tid);
|
| 112 |
$ret['current'] = $term ? $term : 0;
|
| 113 |
|
| 114 |
$arParents = array();
|
| 115 |
$loopTid = $tid;
|
| 116 |
while ($parent = reset(taxonomy_get_parents($loopTid)))
|
| 117 |
{
|
| 118 |
array_unshift($arParents, l($parent->name, 'directory/' . $parent->tid));
|
| 119 |
$loopTid = $parent->tid;
|
| 120 |
}
|
| 121 |
$ret['parents'] = $arParents;
|
| 122 |
|
| 123 |
$arChildren1 = taxonomy_get_children($tid, variable_get('directory-vid', 0));
|
| 124 |
$arChildren2 = array();
|
| 125 |
foreach ($arChildren1 as $childTid => $childTerm)
|
| 126 |
{
|
| 127 |
$arChildren2[] = t('!link: @description',
|
| 128 |
array
|
| 129 |
(
|
| 130 |
'!link' => l($childTerm->name, 'directory/' . $childTerm->tid),
|
| 131 |
'@description' => $childTerm->description,
|
| 132 |
)
|
| 133 |
);
|
| 134 |
}
|
| 135 |
$ret['children'] = $arChildren2;
|
| 136 |
return $ret;
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Obtain the list of nodes bound to a valid tid
|
| 141 |
*
|
| 142 |
* @param integer $tid
|
| 143 |
* @return array
|
| 144 |
*/
|
| 145 |
function _directory_get_nodes($tid = 0)
|
| 146 |
{
|
| 147 |
$arTypes = variable_get('directory-types', array());
|
| 148 |
$arParams = $arTypes;
|
| 149 |
array_unshift($arParams, $tid);
|
| 150 |
$sq = 'SELECT n.nid, n.title '
|
| 151 |
. 'FROM {node} n '
|
| 152 |
. ' INNER JOIN {term_node} tn ON n.nid = tn.nid AND n.vid = tn.vid '
|
| 153 |
. 'WHERE n.status = 1 AND tn.tid = %d '
|
| 154 |
. ' AND n.type IN (' . db_placeholders($arTypes, 'varchar') . ') '
|
| 155 |
. 'ORDER BY n.title ASC ';
|
| 156 |
$sq = db_rewrite_sql($sq);
|
| 157 |
$q = db_query($sq, $arParams);
|
| 158 |
$arNodes = array();
|
| 159 |
while (is_object($o = db_fetch_object($q)))
|
| 160 |
{
|
| 161 |
$arNodes[] = l($o->title . " ($o->nid)", 'node/' . $o->nid);
|
| 162 |
}
|
| 163 |
return $arNodes;
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Sample directory page
|
| 168 |
*
|
| 169 |
* @param integer $tid
|
| 170 |
* @return string
|
| 171 |
*/
|
| 172 |
function _directory_page($tid = 0)
|
| 173 |
{
|
| 174 |
$arTerms = _directory_get_terms($tid);
|
| 175 |
$term = $arTerms['current'];
|
| 176 |
$ret = theme('directory_parents', $arTerms['parents'], $term)
|
| 177 |
. theme('directory_children', $arTerms['children'])
|
| 178 |
. theme('directory_nodes', _directory_get_nodes($tid));
|
| 179 |
return $ret;
|
| 180 |
}
|