| 24 |
* Either an integer or an array. |
* Either an integer or an array. |
| 25 |
*/ |
*/ |
| 26 |
function __construct($data = NULL) { |
function __construct($data = NULL) { |
| 27 |
|
// Load an existing index. |
| 28 |
if (is_int($data)) { |
if (is_int($data)) { |
| 29 |
$data = db_fetch_array(db_query("SELECT * FROM {index_index} WHERE iid = %d", $data)); |
$data = db_fetch_array(db_query("SELECT * FROM {index_index} WHERE iid = %d", $data)); |
| 30 |
} |
} |
| 31 |
|
|
| 32 |
|
// Put all loaded or passed on properties in $this. |
| 33 |
if (is_array($data)) { |
if (is_array($data)) { |
| 34 |
foreach ($data as $property => $value) { |
foreach ($data as $property => $value) { |
| 35 |
$this->$property = $value; |
$this->$property = $value; |
| 36 |
} |
} |
| 37 |
} |
} |
| 38 |
$result = db_query("SELECT * FROM {index_layer} WHERE iid = %d", $this->iid); |
|
| 39 |
while ($layer = db_fetch_object($result)) { |
// Load and process layers. |
| 40 |
$this->layers[$layer->depth] = array( |
$layers = array(); |
| 41 |
'type' => $layer->type, |
$result = db_query("SELECT lineage, entity_type, count FROM {index_layer} WHERE iid = %d", $this->iid); |
| 42 |
'count' => $layer->count, |
while ($layer = db_fetch_array($result)) { |
| 43 |
); |
$layers[$layer['lineage']] = $layer; |
| 44 |
|
} |
| 45 |
|
$this->setLayers($layers); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
/** |
| 49 |
|
* Set layers for this index. |
| 50 |
|
* |
| 51 |
|
* @param $layers array |
| 52 |
|
* An array where the keys are the layer's lineages and the values are |
| 53 |
|
* arrays representing the layers, each having an 'entity_type' and a |
| 54 |
|
* 'count' item. |
| 55 |
|
*/ |
| 56 |
|
function setLayers(array $layers) { |
| 57 |
|
foreach ($layers as $lineage => &$layer) { |
| 58 |
|
$layer['children'] = &$this->childLayers($layers, $lineage); |
| 59 |
|
ksort($layer['children']); |
| 60 |
} |
} |
| 61 |
|
$this->layers = &$this->rootLayers($layers); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* Get all root (top level) layers. |
| 66 |
|
* |
| 67 |
|
* @param $layers array |
| 68 |
|
* Identical to the $layers argument of $this->setLayers(). |
| 69 |
|
*/ |
| 70 |
|
private function rootLayers(array $layers) { |
| 71 |
|
$root_layers = array(); |
| 72 |
|
foreach ($layers as $lineage => &$layer) { |
| 73 |
|
if (strpos($lineage, '-') === FALSE) { |
| 74 |
|
$root_layers[$lineage] = &$layer; |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
return $root_layers; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
/** |
| 82 |
|
* Get a layer's children. |
| 83 |
|
* |
| 84 |
|
* @param $layers array |
| 85 |
|
* Identical to the $layers argument of $this->setLayers(). |
| 86 |
|
* @param $parent_lineage string |
| 87 |
|
* The lineage of the layer to get the children for. |
| 88 |
|
*/ |
| 89 |
|
private function childLayers(array &$layers, $parent_lineage) { |
| 90 |
|
$children = array(); |
| 91 |
|
foreach ($layers as $lineage => &$layer) { |
| 92 |
|
// Find all direct and indirect children. |
| 93 |
|
if ($lineage !== $parent_lineage && strpos((string) $lineage, (string) $parent_lineage) === 0) { |
| 94 |
|
// Check if the layer is a direct child. |
| 95 |
|
if (strpos(substr($lineage, strlen($parent_lineage) + 1), '-') === FALSE) { |
| 96 |
|
$children[$lineage] = &$layer; |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
return $children; |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
/** |
/** |
| 106 |
*/ |
*/ |
| 107 |
function insert() { |
function insert() { |
| 108 |
$status = drupal_write_record('index_index', $this); |
$status = drupal_write_record('index_index', $this); |
| 109 |
$this->insertLayers(); |
$this->insertLayers($this->layers); |
| 110 |
|
|
| 111 |
return $status; |
return $status; |
| 112 |
} |
} |
| 116 |
*/ |
*/ |
| 117 |
function update() { |
function update() { |
| 118 |
$this->deleteLayers(); |
$this->deleteLayers(); |
| 119 |
$this->insertLayers(); |
$this->insertLayers($this->layers); |
| 120 |
|
|
| 121 |
return drupal_write_record('index_index', $this, 'iid'); |
return drupal_write_record('index_index', $this, 'iid'); |
| 122 |
} |
} |
| 131 |
/** |
/** |
| 132 |
* Insert this index' layers. |
* Insert this index' layers. |
| 133 |
*/ |
*/ |
| 134 |
private function insertLayers() { |
private function insertLayers($layers) { |
| 135 |
foreach ($this->layers as $depth => $layer) { |
foreach ($layers as $layer) { |
| 136 |
db_query("INSERT INTO {index_layer} (iid, depth, type, count) VALUES (%d, %d, '%s', %d)", $this->iid, $depth, $layer['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']); |
| 137 |
|
$this->insertLayers($layer['children']); |
| 138 |
} |
} |
| 139 |
} |
} |
| 140 |
|
|