1: <?php
2:
3: class Item_menu {
4:
5: 6: 7: 8: 9:
10: protected $manager;
11:
12: 13: 14: 15: 16:
17: protected $id;
18:
19: 20: 21: 22: 23:
24: protected $parent_id;
25:
26: 27: 28: 29: 30:
31: protected $meta;
32:
33: 34: 35: 36: 37:
38: protected $attributes = array();
39:
40: 41: 42: 43: 44:
45: public $link;
46:
47:
48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function __construct($manager, $title, $url, $attributes = array(), $parent_id = 0)
59: {
60: $this->manager = $manager;
61: $this->id = $this->id();
62: $this->parent_id = $parent_id;
63: $this->title = $title;
64: $this->attributes = $attributes;
65:
66:
67: $this->link = new Link($title, $url);
68: }
69:
70: 71: 72: 73: 74: 75: 76:
77: public function add($title, $options, $externo=false)
78: {
79: if( !is_array($options) ) {
80: $options = array('url' => $options);
81: }
82:
83: $options['parent_id'] = $this->id;
84:
85: return $this->manager->add( $title, $options );
86: }
87:
88: 89: 90: 91: 92:
93: protected function id()
94: {
95: return $this->manager->length() + 1;
96: }
97:
98: 99: 100: 101: 102:
103: public function get_id()
104: {
105: return $this->id;
106: }
107:
108: 109: 110: 111: 112:
113: public function get_parent_id()
114: {
115: return $this->parent_id;
116: }
117:
118: 119: 120: 121:
122: public function hasChildren()
123: {
124: return (count($this->manager->whereParent($this->id))) ? true : false;
125: }
126:
127: 128: 129: 130: 131:
132: public function children()
133: {
134: return $this->manager->whereParent($this->id);
135: }
136:
137:
138: 139: 140: 141: 142:
143: public function attributes()
144: {
145: $args = func_get_args();
146:
147: if(is_array($args[0])) {
148: $this->attributes = array_merge($this->attributes, $args[0]);
149: return $this;
150: }
151:
152: elseif(isset($args[0]) && isset($args[1])) {
153: $this->attributes[$args[0]] = $args[1];
154: return $this;
155: }
156:
157: elseif(isset($args[0])) {
158: return isset($this->attributes[$args[0]]) ? $this->attributes[$args[0]] : null;
159: }
160:
161: return $this->attributes;
162: }
163:
164:
165: 166: 167: 168: 169:
170: public function meta()
171: {
172: $args = func_get_args();
173:
174: if(is_array($args[0])) {
175: $this->meta = array_merge($this->meta, $args[0]);
176: return $this;
177: }
178:
179: elseif(isset($args[0]) && isset($args[1])) {
180: $this->meta[$args[0]] = $args[1];
181: return $this;
182: }
183:
184: elseif(isset($args[0])) {
185: return isset($this->meta[$args[0]]) ? $this->meta[$args[0]] : null;
186: }
187:
188: return $this->meta;
189: }
190:
191: }