diff --git a/library/Class/CompositeBuilder.php b/library/Class/CompositeBuilder.php index 1d64d6682c0e1e133180be9c0b3e8b93bdb55fa4..c177513d22d2c81c38fb82031250299d2d681d85 100644 --- a/library/Class/CompositeBuilder.php +++ b/library/Class/CompositeBuilder.php @@ -16,7 +16,7 @@ * * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE * along with AFI-OPAC 2.0; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ require_once "CompositeBuilder.php"; @@ -95,24 +95,25 @@ class AbstractItem { protected function addAbstractItemsFromRows($rows, $id_field, $cat_field, $build_func){ $builder = $this->getBuilder(); foreach ($rows as $row) { - $abs_item = $builder->$build_func($row[$id_field], + $abs_item = $builder->$build_func($row[$id_field], $row[$cat_field]); if ($abs_item !== null) $abs_item->update_attributes($row); } + $builder->tryToAddOrphans(); return $this; } public function addItemsFromRows($rows, $id_field, $cat_field){ - return $this->addAbstractItemsFromRows($rows, - $id_field, - $cat_field, + return $this->addAbstractItemsFromRows($rows, + $id_field, + $cat_field, "newItemIn"); } public function addCategoriesFromRows($rows, $id_field, $cat_field){ - return $this->addAbstractItemsFromRows($rows, + return $this->addAbstractItemsFromRows($rows, $id_field, - $cat_field, + $cat_field, "newSubcategoryIn"); } } @@ -130,40 +131,47 @@ class ItemCategory extends AbstractItem { public function initialize() { parent::initialize(); - $this->categories = array(); - $this->items = array(); + $this->categories = []; + $this->items = []; } + public function getCategories() { return array_values($this->categories); } + public function getItems() { return array_values($this->items); } + public function addCategory($sub_category) { return $this->addChildToList($sub_category, $this->categories); } + public function addAllCategories($categories) { - foreach($categories as $cat) - $this->addCategory($cat); + array_walk($categories, function($cat) { $this->addCategory($cat); }); } + public function addItem($sub_item) { return $this->addChildToList($sub_item, $this->items); } + public function getCategoryWithId($id) { if ($this->getId()==$id) return $this; return $this->getChildWithId($id, $this->categories, "getCategoryWithId"); } + public function getItemWithId($id) { return $this->getChildWithId($id, $this->items, "getItemWithId"); } + public function acceptVisitor($visitor) { $visitor->startVisitCategory($this); foreach($this->categories as $cat) $cat->acceptVisitor($visitor); @@ -171,6 +179,7 @@ class ItemCategory extends AbstractItem { $visitor->endVisitCategory($this); } + public function toJSON(){ $json_categories = array(); $json_items = array(); @@ -178,7 +187,7 @@ class ItemCategory extends AbstractItem { foreach($this->categories as $cat) $json_categories []= $cat->toJSON(); - foreach($this->items as $item) + foreach($this->items as $item) $json_items []= $item->toJSON(); return '{'. @@ -191,7 +200,7 @@ class ItemCategory extends AbstractItem { protected function getChildWithId($id, &$list, $get_func) { if (array_key_exists($id, $list)) return $list[$id]; - + foreach($this->categories as $subcat) { $result = $subcat->$get_func($id); if ($result) return $result; @@ -201,7 +210,7 @@ class ItemCategory extends AbstractItem { } protected function addChildToList($child, &$list) { - $list [$child->getId()]= $child; + $list[$child->getId()] = $child; $child->setParent($this); return $child; } @@ -243,23 +252,21 @@ class CompositeBuilder { return $this->root->getCategoryWithId($id); } - public function newSubcategoryIn($id, $parent_id) { + public function newSubcategoryIn($id, $parent_id) { $new_category = $this->newCategory($id); if (array_key_exists($parent_id, $this->orphan_categories)) { $parent = $this->orphan_categories[$parent_id]['category']; $parent->addCategory($new_category); } else { - $this->orphan_categories [$id]= array('parent_id' => $parent_id, + $this->orphan_categories [$id]= array('parent_id' => $parent_id, 'category' => $new_category); } - while ($this->tryToAddOrphans()) {} - return $new_category; } - protected function tryToAddOrphans(){ + public function tryToAddOrphans(){ $orphan_added = false; foreach($this->orphan_categories as $id => $pid_cat) { @@ -274,6 +281,7 @@ class CompositeBuilder { return $orphan_added; } + public function newItemIn($id, $parent_id) { $parent = $this->getCategoryWithId($parent_id); if ($parent==null) return;