Example #1
0
 /**
  * Method to get an object.
  *
  * @param   integer  $id  The id of the object to get.
  *
  * @return  mixed    Object on success, false on failure.
  */
 public function &getData($id = null)
 {
     if ($this->_item === null) {
         $this->_item = false;
         if (empty($id)) {
             $id = $this->getState('recipe.id');
         }
         // Get a level row instance.
         $table = $this->getTable();
         // Attempt to load the row.
         if ($table->load($id)) {
             // Check published state.
             if ($published = $this->getState('filter.published')) {
                 if ($table->state != $published) {
                     return $this->_item;
                 }
             }
             // Convert the JTable to a clean JObject.
             $properties = $table->getProperties(1);
             $this->_item = ArrayHelper::toObject($properties, 'JObject');
         }
     }
     // try to get ingredients if it is set
     if (!empty($id)) {
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select('a.*');
         $query->from('`#__akrecipes_recipe_ingredients` as a');
         $query->select('ing.name as ingredient_name, ing.description as ingredient_tooltip, ing.ingredient_url as ingredient_url, ing.image as ingredient_image ');
         $query->join('LEFT', '#__akrecipes_ingredient AS ing ON ing.id = a.ingredients_id');
         $query->where('a.recipe_id = ' . $id);
         $query->order(' a.sequence_no asc ');
         $db->setQuery($query);
         $result = $db->loadObjectList();
         if ($result) {
             $this->_item->qty = array();
             $this->_item->unit = array();
             $this->_item->ingredient = array();
             $this->_item->ingredient_description = array();
             $this->_item->ingredient_tooltip = array();
             $this->_item->ingredient_url = array();
             $this->_item->ingredient_image = array();
             foreach ($result as $key => $row) {
                 $this->_item->qty[] = $row->qty;
                 $this->_item->unit[] = $row->unit;
                 $this->_item->ingredient[] = $row->ingredient_name;
                 $this->_item->ingredient_description[] = $row->ingredient_description;
                 $this->_item->ingredient_tooltip[] = $row->ingredient_tooltip;
                 $this->_item->ingredient_url[] = $row->ingredient_url;
                 $this->_item->ingredient_image[] = $row->ingredient_image;
             }
         }
     }
     if (isset($this->_item->cuisines_id) && $this->_item->cuisines_id != '') {
         if (is_object($this->_item->cuisines_id)) {
             $this->_item->cuisines_id = JArrayHelper::fromObject($this->_item->cuisines_id);
         }
         $values = is_array($this->_item->cuisines_id) ? $this->_item->cuisines_id : explode(',', $this->_item->cuisines_id);
         $textValue = array();
         foreach ($values as $value) {
             $db = JFactory::getDbo();
             $query = $db->getQuery(true);
             $query->select('name')->from('`#__akrecipes_cuisines`')->where('id = ' . $db->quote($db->escape($value)));
             $db->setQuery($query);
             $results = $db->loadObject();
             if ($results) {
                 $textValue[] = $results->name;
             }
         }
         //$this->_item->cuisines_id = !empty($textValue) ? implode(', ', $textValue) : $this->_item->cuisines_id;
         $this->_item->cuisines = !empty($textValue) ? implode(', ', $textValue) : $this->_item->cuisines_id;
     }
     if (isset($this->_item->meal_course_id) && $this->_item->meal_course_id != '') {
         if (is_object($this->_item->meal_course_id)) {
             $this->_item->meal_course_id = JArrayHelper::fromObject($this->_item->meal_course_id);
         }
         $values = is_array($this->_item->meal_course_id) ? $this->_item->meal_course_id : explode(',', $this->_item->meal_course_id);
         $textValue = array();
         foreach ($values as $value) {
             $db = JFactory::getDbo();
             $query = $db->getQuery(true);
             $query->select('name')->from('`#__akrecipes_meal_course`')->where('id = ' . $db->quote($db->escape($value)));
             $db->setQuery($query);
             $results = $db->loadObject();
             if ($results) {
                 $textValue[] = $results->name;
             }
         }
         //$this->_item->meal_course_id = !empty($textValue) ? implode(', ', $textValue) : $this->_item->meal_course_id;
         $this->_item->meal_course = !empty($textValue) ? implode(', ', $textValue) : $this->_item->meal_course_id;
     }
     // get products used
     if (isset($this->_item->product_id) && $this->_item->product_id != '') {
         if (is_object($this->_item->product_id)) {
             $this->_item->product_id = JArrayHelper::fromObject($this->_item->product_id);
         }
         //$values = (is_array($this->_item->product_id)) ? $this->_item->product_id : explode(',',$this->_item->product_id);
         $values = is_array($this->_item->product_id) ? implode(',', $this->_item->product_id) : $this->_item->product_id;
         $textValue = array();
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select('product_name, product_description, product_link, product_image, product_affiliate')->from('`#__akrecipes_products`')->where('id IN ( ' . $values . ' )');
         $db->setQuery($query);
         $results = $db->loadObjectList();
         if ($results) {
             $this->_item->products = $results;
         }
     }
     if (isset($this->_item->diet_id) && $this->_item->diet_id != '') {
         if (is_object($this->_item->diet_id)) {
             $this->_item->diet_id = JArrayHelper::fromObject($this->_item->diet_id);
         }
         $values = is_array($this->_item->diet_id) ? $this->_item->diet_id : explode(',', $this->_item->diet_id);
         $textValue = array();
         foreach ($values as $value) {
             $db = JFactory::getDbo();
             $query = $db->getQuery(true);
             $query->select('name')->from('`#__akrecipes_diets`')->where('id = ' . $db->quote($db->escape($value)));
             $db->setQuery($query);
             $results = $db->loadObject();
             if ($results) {
                 $textValue[] = $results->name;
             }
         }
         //$this->_item->diet_id = !empty($textValue) ? implode(', ', $textValue) : $this->_item->diet_id;
         $this->_item->diet = !empty($textValue) ? implode(', ', $textValue) : $this->_item->diet_id;
     }
     if (isset($this->_item->created_by)) {
         $this->_item->created_by_name = JFactory::getUser($this->_item->created_by)->name;
     }
     if (isset($this->_item->modified_by)) {
         $this->_item->modified_by_name = JFactory::getUser($this->_item->modified_by)->name;
     }
     // add user object to the item
     $this->_item->user = AkrecipesHelper::getUserObject($this->_item->created_by);
     // add brand object
     if (isset($this->_item->brand_id) && $this->_item->brand_id != '') {
         $this->_item->brand = AkrecipesHelper::getBrandObject($this->_item->brand_id);
         //error_log("Got brand object -> " . print_r($this->_item->brand,true));
     }
     // add contest object
     if (isset($this->_item->contest_id) && $this->_item->contest_id != '') {
         $this->_item->contest = AkrecipesHelper::getContestObject($this->_item->contest_id);
     }
     // add ratings data
     $rating = AkrecipesHelper::getRatingObject($this->_item->id);
     //error_log("Got rating object --> " . print_r($rating,true));
     if ($rating) {
         $this->_item->rating = $rating;
     }
     $stats = AkrecipesHelper::getStatsObject($this->_item->id);
     if ($stats) {
         $this->_item->stats = $stats;
     }
     return $this->_item;
 }