<?php # Copyright(C) 2004-2007 INL # Written by Eric Leblond <regit@inl.fr> # Vincent Deffontaines <gryzor@inl.fr> # Jean Gillaux <jean@inl.fr> # Damien Boucard <damien.boucard AT inl.fr> # # $Id: obj_set.class.php 17927 2009-02-16 13:16:09Z haypo $ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see <http://www.gnu.org/licenses/>. require_once('set.class.php'); // set class /** * Plural -> singular name for XML tag name. Examples: * 'periodicities' => 'periodicity' * 'localsin' => 'localin' * 'resources' => 'resource' */ function tagname_singular($tag) { $tag = preg_replace('/ies$/','y', $tag); $tag = preg_replace('/s$/','', $tag); $tag = preg_replace('/^locals/','local', $tag); return $tag; } class obj_set extends set { function obj_set($name, $hash=array(), $check_dup_names=true) { $this->name = $name; $this->elts = Array(); $this->check_dup_names = $check_dup_names; foreach ($hash as $id=>$elt) { $this->add_elt($elt, $id); } } function str() { return $this->name; } function get_max_order($from=-2,$to=-2){ if (($from == -2) or ($to == -2)) { return -1; } $max=0; if (isset($this->elts)) { foreach (($this->elts) as $elts){ $local = $elts->get_max_elt_order($from,$to); if ($local>$max) $max = $local; } } return($max); } function get_max_order_id() { $max=0; if (isset($this->elts)) { foreach (($this->elts) as $elts){ $local = $elts->get_max_elt_order_id(); if ($local>$max) $max = $local; } } return($max); } function xmldump($xml,$curelt) { $tagname = tagname_singular($curelt->tagname); if (isset($this->elts)) foreach ($this->elts as $key=>$elt){ $node = $xml->create_element($tagname); if(!isset($elt)){ continue; } $elt->xmldump($xml,$node); $curelt->append_child($node); } } function express_elt($elt,$scope="one") { $this->elts[$elt]->express($scope,$base=$this); } function getByName($name) { $use_datas = isset($elt->datas); foreach ($this->elts as $iter) { if ($use_datas) $iter_name = $iter->datas['name']; else $iter_name = $iter->name; if ($iter_name == $name) { return $iter; } } return null; } function _check_new_element($elt, $id) { parent::_check_new_element($elt, $id); // Name validation is disabled? if (!$this->check_dup_names) return; $use_datas = isset($elt->datas); if ($use_datas) $name = $elt->datas['name']; else $name = $elt->name; if (!$name) { throw new Exception(_('An empty name is forbidden!')); } $elt = $this->getByName($name); if ($elt) { $msg = sprintf( _('The name "%s" is used more than once for %s, please change the name of %s'), $name, $this->str(), $elt->str()); throw new Exception($msg); } } function &get_elt_by_id($id){ if (array_key_exists($id, $this->elts)) { return $this->elts[$id]; } else { $res=null; return $res; } } function get_max_elt_id() { $max=0; if (!isset($this->elts)) return(0); foreach (($this->elts) as $elts){ $local = $elts->get_max_elt_id(); if ($local>$max) $max = $local; } return $max; } function new_elt_id(){ return $this->get_max_elt_id() + 1; } function get_max_id(){ $max=0; if (isset($this->elts)) foreach (($this->elts) as $elts){ $local=$elts->get_id(); if ($local>$max) $max=$local; } return $max; } function new_id(){ return($this->get_max_id()+1); } function new_lower_id(){ $max=$this->get_max_id(); $seen=array(); foreach (($this->elts) as $elts) { $seen[$elts->get_id()]=1; } for ($i=1; $i<=$max+1; $i++) { if (!array_key_exists($i, $seen)) return $i; } return ($max+1); } function list_tab() { $result = Array(); foreach ($this->elts as $elt) { $key = $elt->ID; $value = $elt->name; if (!$value) $value = $key; $result[$key] = $value; } return $result; } function ordered_list_tab() { $result = $this->list_tab(); ksort($result); return $result; } function nb_obj(){ $result=0; foreach ($this->elts as $elt){ $result++; } return($result); } function reorder(){ $i=1; $clone = new obj_set($this->name, array(), $this->check_dup_names); foreach($this->elts as $id_t =>$elt){ $elt->ID = $i; $clone->add_elt($elt); $i++; } return $clone; } } ?>