/** * Load types data from database. * * <code> * $options = array( * "order_column" => "title", // id or title * "order_direction" => "DESC", * ); * * $types = new Crowdfunding\Types(); * $types->setDb(\JFactory::getDbo()); * $types->load($options); * * foreach ($types as $type) { * echo $type["title"]; * echo $type["description"]; * } * </code> * * @param array $options */ public function load($options = array()) { $query = $this->db->getQuery(true); $query->select("a.id, a.title, a.description, a.params")->from($this->db->quoteName("#__crowdf_types", "a")); // Order by column if (isset($options["order_column"])) { $orderString = $this->db->quoteName($options["order_column"]); // Order direction if (isset($options["order_direction"])) { $orderString .= strcmp("DESC", $options["order_direction"]) ? " DESC" : " ASC"; } $query->order($orderString); } $this->db->setQuery($query); $results = $this->db->loadAssocList(); if (!empty($results)) { foreach ($results as $result) { $type = new Type(\JFactory::getDbo()); $type->bind($result); $this->items[] = $type; } } else { $this->items = array(); } }
/** * Return the types as array with objects. * * <code> * $options = array( * "ids" => array(1,2,3,4,5) * ); * * $types = new Crowdfunding\Type\Types(\JFactory::getDbo()); * $types->load($options); * * $types = $types->getTypes(); * </code> * * @return array */ public function getTypes() { $results = array(); $i = 0; foreach ($this->items as $item) { $type = new Type($this->db); $type->bind($item); $results[$i] = $type; $i++; } return $results; }
/** * Load types data from database. * * <code> * $options = array( * "order_column" => "title", // id or title * "order_direction" => "DESC", * ); * * $types = new Crowdfunding\Types(); * $types->setDb(\JFactory::getDbo()); * $types->load($options); * * foreach ($types as $type) { * echo $type["title"]; * echo $type["description"]; * } * </code> * * @param array $options */ public function load($options = array()) { $query = $this->db->getQuery(true); $query->select('a.id, a.title, a.description, a.params')->from($this->db->quoteName('#__crowdf_types', 'a')); // Order by column if (array_key_exists('order_column', $options)) { $orderString = $this->db->quoteName($options['order_column']); // Order direction if (array_key_exists('order_direction', $options)) { $orderString .= strcmp('DESC', $options['order_direction']) ? ' DESC' : ' ASC'; } $query->order($orderString); } $this->db->setQuery($query); $results = (array) $this->db->loadAssocList(); if (count($results) > 0) { foreach ($results as $result) { $type = new Type(\JFactory::getDbo()); $type->bind($result); $this->items[] = $type; } } else { $this->items = array(); } }
/** * Create a type object and return it. * * <code> * $options = array( * "ids" => array(1,2,3,4,5) * ); * * $types = new Crowdfunding\Type\Types(\JFactory::getDbo()); * $types->load($options); * * $typeId = 1; * $type = $types->getType($typeId); * </code> * * @param int $id Type ID. * * @return null|Type */ public function getType($id) { if (!$id) { throw new \UnexpectedValueException(\JText::_('LIB_CROWDFUNDING_INVALID_TYPE_ID')); } $type = null; foreach ($this->items as $item) { if ((int) $id === (int) $item['id']) { $type = new Type($this->db); $type->bind($item); break; } } return $type; }