| 15 |
public $block_enabled = ''; |
public $block_enabled = ''; |
| 16 |
public $block_mode = ''; |
public $block_mode = ''; |
| 17 |
public $layers = array(); |
public $layers = array(); |
|
public $data = array(); |
|
| 18 |
|
|
| 19 |
/** |
/** |
| 20 |
* Fill all properties. |
* Fill all properties. |
| 38 |
// Load and process layers. |
// Load and process layers. |
| 39 |
$layers = array(); |
$layers = array(); |
| 40 |
$result = db_query("SELECT lineage, entity_type, count FROM {index_layer} WHERE iid = %d", $this->iid); |
$result = db_query("SELECT lineage, entity_type, count FROM {index_layer} WHERE iid = %d", $this->iid); |
| 41 |
while ($layer = db_fetch_array($result)) { |
while ($layer_data = db_fetch_object($result)) { |
| 42 |
$layers[$layer['lineage']] = $layer; |
$layers[$layer_data->lineage] = new indexLayer(array( |
| 43 |
|
'lineage' => $layer_data->lineage, |
| 44 |
|
'entity_type' => $layer_data->entity_type, |
| 45 |
|
'count' => $layer_data->count, |
| 46 |
|
)); |
| 47 |
} |
} |
| 48 |
$this->setLayers($layers); |
$this->setLayers($layers); |
| 49 |
} |
} |
| 57 |
* 'count' item. |
* 'count' item. |
| 58 |
*/ |
*/ |
| 59 |
function setLayers(array $layers) { |
function setLayers(array $layers) { |
| 60 |
foreach ($layers as $lineage => &$layer) { |
foreach ($layers as $lineage => $layer) { |
| 61 |
$layer['children'] = &$this->childLayers($layers, $lineage); |
$layer->children = &$this->childLayers($layers, $lineage); |
| 62 |
ksort($layer['children']); |
ksort($layer->children); |
| 63 |
} |
} |
| 64 |
$this->layers = &$this->rootLayers($layers); |
$this->layers = &$this->rootLayers($layers); |
| 65 |
} |
} |
| 72 |
*/ |
*/ |
| 73 |
private function rootLayers(array $layers) { |
private function rootLayers(array $layers) { |
| 74 |
$root_layers = array(); |
$root_layers = array(); |
| 75 |
foreach ($layers as $lineage => &$layer) { |
foreach ($layers as $lineage => $layer) { |
| 76 |
if (strpos($lineage, '-') === FALSE) { |
if (strpos($lineage, '-') === FALSE) { |
| 77 |
$root_layers[$lineage] = &$layer; |
$root_layers[$lineage] = $layer; |
| 78 |
} |
} |
| 79 |
} |
} |
| 80 |
|
|
| 109 |
*/ |
*/ |
| 110 |
function insert() { |
function insert() { |
| 111 |
$status = drupal_write_record('index_index', $this); |
$status = drupal_write_record('index_index', $this); |
| 112 |
$this->insertLayers($this->layers); |
$this->insertLayers(); |
| 113 |
|
|
| 114 |
return $status; |
return $status; |
| 115 |
} |
} |
| 119 |
*/ |
*/ |
| 120 |
function update() { |
function update() { |
| 121 |
$this->deleteLayers(); |
$this->deleteLayers(); |
| 122 |
$this->insertLayers($this->layers); |
$this->insertLayers(); |
| 123 |
|
|
| 124 |
return drupal_write_record('index_index', $this, 'iid'); |
return drupal_write_record('index_index', $this, 'iid'); |
| 125 |
} |
} |
| 134 |
/** |
/** |
| 135 |
* Insert this index' layers. |
* Insert this index' layers. |
| 136 |
*/ |
*/ |
| 137 |
private function insertLayers($layers) { |
private function insertLayers($layers = NULL) { |
| 138 |
|
$ayers = $layers ? $layers : $this->layers; |
| 139 |
foreach ($layers as $layer) { |
foreach ($layers as $layer) { |
| 140 |
db_query("INSERT INTO {index_layer} (iid, lineage, entity_type, count) VALUES (%d, '%s', '%s', %d)", $this->iid, $layer['lineage'], $layer['entity_type'], $layer['count']); |
db_query("INSERT INTO {index_layer} (iid, lineage, entity_type, count) VALUES (%d, '%s', '%s', %d)", $this->iid, $layer->lineage, $layer->entity_type, $layer->count); |
| 141 |
$this->insertLayers($layer['children']); |
$this->insertLayers($layer->children); |
| 142 |
} |
} |
| 143 |
} |
} |
| 144 |
|
|
| 153 |
/** |
/** |
| 154 |
* View the index. |
* View the index. |
| 155 |
* |
* |
| 156 |
* @param $type |
* @param $type string |
| 157 |
* Either 'page' or 'block'. |
* Either 'page' or 'block' to respectively view an index page or a block. |
| 158 |
* |
* |
| 159 |
* @return |
* @return |
| 160 |
* The rendered index. |
* The rendered index. |
| 161 |
*/ |
*/ |
| 162 |
function view($type) { |
function view($type) { |
| 163 |
$this->getData(); |
$this->getData($type); |
| 164 |
index_load_include('view'); |
index_load_include('view'); |
| 165 |
$modes_info = index_modes_get(); |
$modes_info = index_modes_get(); |
| 166 |
$mode_key = $type . '_mode'; |
$mode_key = $type . '_mode'; |
| 167 |
$content = index_callback_execute($modes_info[$this->$mode_key]['#callback_info'], $this); |
// $content = index_callback_execute($mode_info[$this->$mode_key]['#callback_info'], $this); |
| 168 |
|
|
| 169 |
return theme('index', check_plain($this->description), $content); |
return theme('index', check_plain($this->description), $content); |
| 170 |
} |
} |
| 171 |
|
|
| 172 |
/** |
/** |
| 173 |
* Render a preview of the index. |
* Get the data for all layers of this index. |
| 174 |
|
* |
| 175 |
|
* @param $type string |
| 176 |
|
* Either 'page' or 'block' to respectively view an index page or a block. |
| 177 |
|
* @param $layers array |
| 178 |
|
* The layers to get the data for. They should have the same parent. |
| 179 |
*/ |
*/ |
| 180 |
function preview() { |
function getData($type, array $layers = NULL) { |
| 181 |
// return theme('index_preview_title', check_plain($this->title)) . $this->view('block'); |
$modes_info = index_modes_get(); |
| 182 |
return time(); |
$mode_key = $type . '_mode'; |
| 183 |
|
$mode_info = $modes_info[$this->$mode_key]; |
| 184 |
|
$this->_getData($type, $this->layers, $mode_info['#depth_data'], $mode_info['#depth_count']); |
| 185 |
} |
} |
| 186 |
|
|
| 187 |
/** |
/** |
| 188 |
* Get the data for all layers of this index. |
* Helper function for $this->getData(). |
| 189 |
*/ |
* |
| 190 |
function getData() { |
* @param $type string |
| 191 |
// TODO: Build in a depth check so we don't load more data than required by |
* Either 'page' or 'block' to respectively view an index page or a block. |
| 192 |
// the display mode. |
* @param $layers array |
| 193 |
$entity_types_info = index_entity_types_get(); |
* The layers to get the data for. |
| 194 |
foreach ($this->layers as $depth => $layer) { |
* @param $depth_data integer |
| 195 |
if ($depth) { |
* The maximum depth of the layers for which to get data. |
| 196 |
$key = $entity_types_info[$layer['type']]['#relations'][$this->layers[$depth - 1]['type']]; |
* @param $depth_count integer |
| 197 |
$values = array(); |
* The maximum depth of the layers for which to get the entity count only. |
| 198 |
foreach ($this->data[$depth - 1] as $entity) { |
* @param $depth_current integer |
| 199 |
$values[] = $entity->$key; |
* The current layer depth. |
| 200 |
|
* @param $parent_layer indexLayer |
| 201 |
|
* The optional parent layer of $layers. |
| 202 |
|
*/ |
| 203 |
|
private function _getData($type, array $layers, $depth_data, $depth_count, $depth_current = 1, indexLayer $parent_layer = NULL) { |
| 204 |
|
// Only proceed if we can at least get the entity count. |
| 205 |
|
if ($depth_count == 0 || $depth_current <= $depth_count) { |
| 206 |
|
$entity_types_info = index_entity_types_get(); |
| 207 |
|
foreach ($layers as $layer) { |
| 208 |
|
// If there is data for a parent layer, compute the conditions for |
| 209 |
|
// which to get this layer's data. |
| 210 |
|
if ($parent_layer && $parent_layer->data_count[$type]) { |
| 211 |
|
$key = $entity_types_info[$layer->entity_type]['#relations'][$parent_layer->entity_type]; |
| 212 |
|
$values = array(); |
| 213 |
|
foreach ($parent_layer->data[$type] as $entity) { |
| 214 |
|
if ($entity->$key) { |
| 215 |
|
$values[] = $entity->$key; |
| 216 |
|
} |
| 217 |
|
} |
| 218 |
} |
} |
| 219 |
|
// There is no parent data, so leave the conditions blank. |
| 220 |
|
else { |
| 221 |
|
$key = NULL; |
| 222 |
|
$values = array(); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
// We need to get data. |
| 226 |
|
if ($depth_data == 0 || $depth_data <= $depth_current) { |
| 227 |
|
$layer->data[$type] = index_callback_execute($entity_types_info[$layer->entity_type]['#data_callback_info'], $key, $values); |
| 228 |
|
$layer->data_count[$type] = count($layer->data); |
| 229 |
|
} |
| 230 |
|
// We need to get the entity count only. |
| 231 |
|
else { |
| 232 |
|
$layer->data_count = index_callback_execute($entity_types_info[$layer->entity_type]['#count_callback_info'], $key, $values); |
| 233 |
|
} |
| 234 |
|
// Get data for this layer's children. |
| 235 |
|
$this->_getData($type, $layer->children, $depth_data, $depth_count, $depth_current + 1, $layer); |
| 236 |
} |
} |
| 237 |
else { |
} |
| 238 |
$key = NULL; |
} |
| 239 |
$values = array(); |
} |
| 240 |
} |
|
| 241 |
$this->data[$depth] = index_callback_execute($entity_types_info[$layer['type']]['#data_callback_info'], $key, $values); |
class indexLayer { |
| 242 |
|
public $lineage = NULL; |
| 243 |
|
public $entity_type = NULL; |
| 244 |
|
public $count = 0; |
| 245 |
|
public $children = array(); |
| 246 |
|
public $data = array( |
| 247 |
|
'page' => array(), |
| 248 |
|
'block' => array(), |
| 249 |
|
); |
| 250 |
|
public $data_count = array( |
| 251 |
|
'page' => array(), |
| 252 |
|
'block' => array(), |
| 253 |
|
); |
| 254 |
|
|
| 255 |
|
/** |
| 256 |
|
* Fill all properties. |
| 257 |
|
* |
| 258 |
|
* @param $data |
| 259 |
|
* An array with properties and their values. |
| 260 |
|
*/ |
| 261 |
|
function __construct($data) { |
| 262 |
|
// Put all loaded or passed on properties in $this. |
| 263 |
|
foreach ($data as $property => $value) { |
| 264 |
|
$this->$property = $value; |
| 265 |
} |
} |
| 266 |
} |
} |
| 267 |
} |
} |