1: <?php
2: 3: 4: 5: 6: 7:
8:
9: class ClipitExampleType extends UBItem{
10: 11: 12:
13: const SUBTYPE = "ClipitExampleType";
14:
15: 16: 17: 18: 19: 20:
21: public $reference = array();
22: public $item_name = array();
23: public $item_description = array();
24: public $category = array();
25: public $category_description = array();
26:
27:
28: 29: 30: 31: 32:
33: protected function copy_from_elgg($elgg_entity) {
34: parent::copy_from_elgg($elgg_entity);
35: $this->reference = (int)$elgg_entity->get("reference");
36: $this->item_name = (array)$elgg_entity->get("item_name");
37: $this->item_description = (array)$elgg_entity->get("item_description");
38: $this->category = (array)$elgg_entity->get("category");
39: $this->category_description = (array)$elgg_entity->get("category_description");
40: }
41:
42: 43: 44: 45: 46:
47: protected function copy_to_elgg($elgg_entity) {
48: parent::copy_to_elgg($elgg_entity);
49: $elgg_entity->set("reference", (int)$this->reference);
50: $elgg_entity->set("item_name", (array)$this->item_name);
51: $elgg_entity->set("item_description", (array)$this->item_description);
52: $elgg_entity->set("category", (array)$this->category);
53: $elgg_entity->set("category_description", (array)$this->category_description);
54: }
55:
56: static function create($prop_value_array){
57: if(isset($prop_value_array["reference"])){
58: $item = static::get_by_reference((int)$prop_value_array["reference"]);
59: if(!empty($item)) {
60: return static::set_properties($item->id, $prop_value_array);
61: }
62: }
63: return static::set_properties(null, $prop_value_array);
64: }
65:
66: static function set_properties($id, $prop_value_array){
67: if(!$item = new static($id)) {
68: return false;
69: }
70:
71: if(!isset($prop_value_array["language"])){
72: $lang_index = 0;
73: }else{
74: $lang_index = static::get_language_index($prop_value_array["language"]);
75: unset($prop_value_array["language"]);
76: }
77: $property_list = (array)static::list_properties();
78: foreach($prop_value_array as $prop => $value) {
79: if(!array_key_exists($prop, $property_list) && $prop != "language") {
80: throw new InvalidParameterException("ERROR: One or more property names do not exist.");
81: }
82: if($prop == "id") {
83: continue;
84: }
85:
86: if(array_search(
87: $prop,
88: array("item_name", "item_description", "category", "category_description"))
89: !== false){
90: $prop_array = (array)$item->$prop;
91: $prop_array[$lang_index] = $value;
92: $item->$prop = (array)$prop_array;
93: } else {
94: $item->$prop = $value;
95: }
96: }
97: if (array_key_exists("time_created",$prop_value_array)) {
98: return $item->save(true);
99: } else {
100: return $item->save(false);
101: }
102: }
103:
104: static function get_language_index($language){
105: switch ((string)$language){
106: case "en":
107: $lang_index = 0;
108: break;
109: case "es":
110: $lang_index = 1;
111: break;
112: case "de":
113: $lang_index = 2;
114: break;
115: case "pt":
116: $lang_index = 3;
117: break;
118: case "sv":
119: $lang_index = 4;
120: break;
121: default:
122: $lang_index = 0;
123: }
124: return $lang_index;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: static function get_from_category($category = null, $language = "en") {
136: $performance_items = static::get_all();
137: $category_array = array();
138: $lang_index = static::get_language_index($language);
139: if (empty($category)) {
140: foreach ($performance_items as $performance_item) {
141: $category_array[$performance_item->category[$lang_index]][] = $performance_item;
142: }
143: } else {
144: foreach ($performance_items as $performance_item) {
145: if ($performance_item->category[$lang_index] == $category) {
146: $category_array[] = $performance_item;
147: }
148: }
149: }
150: return $category_array;
151: }
152:
153: 154: 155: 156:
157: static function get_by_reference($reference){
158: $performance_items = static::get_all();
159: foreach($performance_items as $item){
160: if((int)$item->reference == (int)$reference) {
161: return $item;
162: }
163: }
164: return null;
165: }
166: }