diff --git a/include/library/Crunchbutton/Restaurant.php b/include/library/Crunchbutton/Restaurant.php index 4724f9df4..19aa78762 100644 --- a/include/library/Crunchbutton/Restaurant.php +++ b/include/library/Crunchbutton/Restaurant.php @@ -78,123 +78,6 @@ class Crunchbutton_Restaurant extends Cana_Table { return $this->short_name ? $this->short_name : $this->name; } - public function saveDishes($newDishes) { - foreach ($this->categories() as $cat) { - if (!$category) { - $category = $cat; - } - } - - if (!$category) { - $category = new Category; - $category->id_restaurant = $this->id_restaurant; - $category->name = 'Most Popular'; - $category->sort = 1; - $category->loc = 1; - $category->save(); - } - - $dishes = $this->dishes(); - - foreach ($newDishes as $dish) { - $dishO = new Dish($dish['id_dish']); - $dishO->id_restaurant = $this->id_restaurant; - $dishO->active = isset($dish['active']) ? $dish['active'] : 1; - $dishO->name = $dish['name']; - $dishO->description = $dish['description']; - $dishO->price = $dish['price']; - if (isset($dish['id_category']) && $dish['id_category']) { - $dishO->id_category = $dish['id_category']; - } elseif (!$dishO->id_category) { // this else doesn't make sense to me, but it is what it was before my changes - $dishO->id_category = $category->id_category; - } - - $dishO->save(); - - $options = $dishO->options(); - $newOptions = []; - - if ($dish['optionGroups']) { - foreach ($dish['optionGroups'] as $optionGroup) { - if ($optionGroup['id_option'] == 'BASIC') { - $parent = null; - - } else { - - $group = new Option($optionGroup['id_option']); - $group->name = $optionGroup['name']; - $group->price_linked = $optionGroup['price']; - $group->type = $optionGroup['type']; - $group->id_restaurant = $this->id_restaurant; - $group->save(); - $parent = $group->id_option; - $newOptions[$group->id_option] = $group->id_option; - - if (!$doid = $this->_hasOption($group, $options)) { - $do = new Dish_Option; - $do->id_dish = $dishO->id_dish; - $do->id_option = $group->id_option; - $do->save(); - } else { - $do = new Dish_Option($doid); - $do->default = $opt->default; - } - } - - if ($optionGroup['options']) { - foreach ($optionGroup['options'] as $opt) { - $option = new Option($opt['id_option']); - $option->id_restaurant = $this->id_restaurant; - $option->id_option_parent = $parent; - $option->price = $opt['price']; - $option->name = $opt['name']; - $option->active = 1; - $option->type = 'check'; - $option->save(); - $newOptions[$option->id_option] = $option->id_option; - $opt['default'] = $opt['default'] == 'true' ? 1 : 0; - - if (!$doid = $this->_hasOption($option, $options)) { - $do = new Dish_Option; - $do->id_dish = $dishO->id_dish; - $do->id_option = $option->id_option; - $do->default = $opt['default']; - - $do->save(); - } else { - $do = new Dish_Option($doid); - if ($opt['default'] != $do->default) { - $do->default = $opt['default']; - $do->save(); - } - } - } - } - } - } - - foreach ($options as $option) { - if (!in_array($option->id_option, $newOptions)) { - $do = new Dish_Option($option->id_dish_option); - $do->delete(); - } - } - } - - $nd = []; - foreach ($newDishes as $dish) { - $nd[$dish['id_dish']] = $dish['id_dish']; - } - - foreach ($dishes as $dish) { - if (!in_array($dish->id_dish, $nd)) { - $d = new Dish($dish->id_dish); - $d->delete(); - } - } - - } - public function _hasOption($option, $options) { foreach ($options as $o) { if ($o->id_option == $option->id_option) { @@ -298,19 +181,146 @@ class Crunchbutton_Restaurant extends Cana_Table { */ public function saveCategories($rawData) { - foreach ($rawData as $data) { - // if (!$data['name']) continue; - $element = new Crunchbutton_Category($data['id_category']); - $element->id_restaurant = $this->id_restaurant; - $element->name = $data['name']; - $element->save(); + if ($rawData) { + foreach ($rawData as $data) { + // if (!$data['name']) continue; + $element = new Crunchbutton_Category($data['id_category']); + $element->id_restaurant = $this->id_restaurant; + $element->name = $data['name']; + $element->save(); + } } - $this->_categories = null; $elements = $this->categories(); return $elements; } + + /** + * Stores the dishes and it's options + * + * @return void + */ + public function saveDishes($newDishes) { + foreach ($this->categories() as $cat) { + if (!$category) { + $category = $cat; + } + } + + if (!$category) { + $category = new Category; + $category->id_restaurant = $this->id_restaurant; + $category->name = 'Most Popular'; + $category->sort = 1; + $category->loc = 1; + $category->save(); + } + + $dishes = $this->dishes(); + if ($newDishes) { + foreach ($newDishes as $dish) { + $dishO = new Dish($dish['id_dish']); + $dishO->id_restaurant = $this->id_restaurant; + $dishO->active = isset($dish['active']) ? $dish['active'] : 1; + $dishO->name = $dish['name']; + $dishO->description = $dish['description']; + $dishO->price = $dish['price']; + if (isset($dish['id_category']) && $dish['id_category']) { + $dishO->id_category = $dish['id_category']; + } elseif (!$dishO->id_category) { // this else doesn't make sense to me, but it is what it was before my changes + $dishO->id_category = $category->id_category; + } + + $dishO->save(); + + $options = $dishO->options(); + $newOptions = []; + + if ($dish['optionGroups']) { + foreach ($dish['optionGroups'] as $optionGroup) { + if ($optionGroup['id_option'] == 'BASIC') { + $parent = null; + + } else { + + $group = new Option($optionGroup['id_option']); + $group->name = $optionGroup['name']; + $group->price_linked = $optionGroup['price']; + $group->type = $optionGroup['type']; + $group->id_restaurant = $this->id_restaurant; + $group->save(); + $parent = $group->id_option; + $newOptions[$group->id_option] = $group->id_option; + + if (!$doid = $this->_hasOption($group, $options)) { + $do = new Dish_Option; + $do->id_dish = $dishO->id_dish; + $do->id_option = $group->id_option; + $do->save(); + } else { + $do = new Dish_Option($doid); + $do->default = $opt->default; + } + } + + if ($optionGroup['options']) { + foreach ($optionGroup['options'] as $opt) { + $option = new Option($opt['id_option']); + $option->id_restaurant = $this->id_restaurant; + $option->id_option_parent = $parent; + $option->price = $opt['price']; + $option->name = $opt['name']; + $option->active = 1; + $option->type = 'check'; + $option->save(); + $newOptions[$option->id_option] = $option->id_option; + $opt['default'] = $opt['default'] == 'true' ? 1 : 0; + + if (!$doid = $this->_hasOption($option, $options)) { + $do = new Dish_Option; + $do->id_dish = $dishO->id_dish; + $do->id_option = $option->id_option; + $do->default = $opt['default']; + + $do->save(); + } else { + $do = new Dish_Option($doid); + if ($opt['default'] != $do->default) { + $do->default = $opt['default']; + $do->save(); + } + } + } + } + } + } + + foreach ($options as $option) { + if (!in_array($option->id_option, $newOptions)) { + $do = new Dish_Option($option->id_dish_option); + $do->delete(); + } + } + } + } + + $nd = []; + if ($newDishes) { + foreach ($newDishes as $dish) { + $nd[$dish['id_dish']] = $dish['id_dish']; + } + } + + foreach ($dishes as $dish) { + if (!in_array($dish->id_dish, $nd)) { + $d = new Dish($dish->id_dish); + $d->delete(); + } + } + } + + /** * Save the notifications as they are send by the API * @@ -335,18 +345,23 @@ class Crunchbutton_Restaurant extends Cana_Table { return $elements; } - - + /** + * Saves the hours the restaurant uses for delivery + * + * @return void + */ public function saveHours($hours) { c::db()->query('delete from hour where id_restaurant="'.$this->id_restaurant.'"'); - foreach ($hours as $day => $times) { - foreach ($times as $time) { - $hour = new Hour; - $hour->id_restaurant = $this->id_restaurant; - $hour->day = $day; - $hour->time_open = $time[0]; - $hour->time_close = $time[1]; - $hour->save(); + if ($hours) { + foreach ($hours as $day => $times) { + foreach ($times as $time) { + $hour = new Hour; + $hour->id_restaurant = $this->id_restaurant; + $hour->day = $day; + $hour->time_open = $time[0]; + $hour->time_close = $time[1]; + $hour->save(); + } } } unset($this->_hours);