3 class mw_data_links extends mw_data{
7 public function init_links(){
8 $this->links = array();
12 public function load_link(&$links, $v_path, $url, $intitule = "", $position = 0){
13 if($path_item = array_shift($v_path)){
14 if(!isset($links[$path_item])) $links[$path_item] = array(
16 "url" => $v_path ? null : $url,
17 "intitule" => $v_path ? null : $intitule,
21 if($v_path) $this->load_link($links[$path_item]["subs"], $v_path, $url, $intitule, $position);
23 $links[$path_item]["nom"] = $path_item;
24 $links[$path_item]["url"] = $url;
25 $links[$path_item]["intitule"] = $intitule;
26 $links[$path_item]["position"] = $position;
31 public function valid_link_path($path){
32 $v_path = explode("/", $path);
34 foreach($v_path as $i => $path_item){
36 if(!preg_match("/^[a-zA-Z]+[a-zA-Z0-9\-_\.]*$/", $path_item)){
41 else unset($v_path[$i]);
43 return $v_path && $SYNTAX_OK ? $v_path : false;
46 public function get_link($path = null){
47 if(!isset($this->links)) $this->init_links();
48 if($this->links !== false){
49 if(!isset($path)) return $this->links;
50 if($v_path = $this->valid_link_path($path)){
51 return $this->_get_link($this->links, $v_path);
57 public function _get_link($links, $v_path){
58 if($path_item = array_shift($v_path)){
59 if(isset($links[$path_item])){
60 if($v_path) return $this->_get_link($links[$path_item]["subs"], $v_path);
61 else return $links[$path_item];
67 public function set_link($path, $url, $intitule = "", $position = 0){
68 if(!isset($this->links)) $this->init_links();
69 if($v_path = $this->valid_link_path($path)){
70 $this->load_link($this->links, $v_path, $url, $intitule, $position);
71 $this->links = $this->ordonne_links($this->links);
75 public function ordonne_links($links){
76 if(!is_array($links)) return false;
77 $values = array_values($links);
78 $maximum = count($values);
80 $maximumTemporaire = 0;
81 for($i = 0; $i < $maximum - 1; $i++){
82 if($values[$i]["position"] > $values[$i + 1]["position"]){
84 $values[$i] = $values[$i + 1];
85 $values[$i + 1] = $tmp;
86 $maximumTemporaire = $i + 1;
89 $maximum = $maximumTemporaire;
92 foreach($values as $value) { if($value["nom"]) $res[$value["nom"]] = $value; }
93 foreach($res as $nom => $sub) { if($sub["subs"]) $res[$nom]["subs"] = $this->ordonne_links($res[$nom]["subs"]); }