/** * Load categories. * * <code> * $parentId = 2; * * $options = array( * "offset" => 0, * "limit" => 10, * "order_by" => "a.name", * "order_dir" => "DESC", * ); * * $categories = new Crowdfunding\Categories(); * $categories->setDb(\JFactory::getDbo()); * * $categories->load($parentId); * </code> * * @param null|int $parentId Parent ID or "root". * @param array $options * * @throws \RuntimeException */ public function load($parentId = null, array $options = array()) { $offset = array_key_exists('offset', $options) ? $options['offset'] : 0; $limit = array_key_exists('limit', $options) ? $options['limit'] : 0; $orderBy = array_key_exists('order_by', $options) ? $options['order_by'] : 'a.title'; $orderDir = array_key_exists('order_dir', $options) ? $options['order_dir'] : 'ASC'; $published = array_key_exists('state', $options) ? $options['state'] : null; $orderDir = strtoupper($orderDir); if (!in_array($orderDir, array('ASC', 'DESC'), true)) { $orderDir = 'ASC'; } $query = $this->db->getQuery(true); $query->select('a.id, a.title, a.alias, a.description, a.params, ' . $query->concatenate(array('a.id', 'a.alias'), ':') . ' AS slug')->from($this->db->quoteName('#__categories', 'a'))->where('a.extension = ' . $this->db->quote($this->_extension)); if ($parentId !== null) { $query->where('a.parent_id = ' . (int) $parentId); } // Filter by state. if ($published === null) { $query->where('a.published IN (0,1)'); } else { $query->where('a.published = ' . (int) $published); } $query->order($this->db->escape($this->db->quoteName($orderBy) . ' ' . $orderDir)); $this->db->setQuery($query, (int) $offset, (int) $limit); $this->data = (array) $this->db->loadAssocList('id'); }
/** * Load images from database. * * <code> * $projectId = 1; * * $options = array( * "order_direction" => "DESC" * ); * * $images = new CrowdFundingImages(JFactory::getDbo()); * $images->load($projectId, $options); * * foreach($images as $image) { * echo '<img src="'.$image["thumb"].'" />'; * echo '<img src="'.$image["image"].'" />'; * } * </code> * * @param $id * @param array $options * * @return array|mixed */ public function load($id, $options = array()) { $query = $this->db->getQuery(true); $query->select("a.id, a.image, a.thumb, a.project_id")->from($this->db->quoteName("#__crowdf_images", "a"))->where("a.project_id = " . (int) $id); if (isset($options["order_direction"])) { $orderDir = strcmp("DESC", $options["order_direction"]) ? "DESC" : "ASC"; $query->order("a.id " . $this->db->escape($orderDir)); } $this->db->setQuery($query); $results = $this->db->loadObjectList(); if (!$results) { $results = array(); } $this->items = $results; }
/** * Load locations data by string from database. * * <code> * $string = "Plovdiv"; * * $locations = new CrowdFundingLocations(JFactory::getDbo()); * $locations->loadByString($string); * * foreach($locations as $location) { * echo $location["id"]; * echo $location["name"]; * } * </code> * * @param string $string * @param int $mode Filter mode. * * Example: * * # Filter modes * 0 = "string"; * 1 = "string%"; * 2 = "%string"; * 3 = "%string%"; */ public function loadByString($string, $mode = 1) { $query = $this->db->getQuery(true); switch ($mode) { case 1: // Beginning $searchFilter = $this->db->escape($string, true) . '%'; break; case 2: // End $searchFilter = '%' . $this->db->escape($string, true); break; case 3: // Both $searchFilter = '%' . $this->db->escape($string, true) . '%'; break; default: // NONE $searchFilter = $this->db->escape($string, true); break; } $search = $this->db->quote($searchFilter); $caseWhen = ' CASE WHEN '; $caseWhen .= $query->charLength('a.state_code', '!=', '0'); $caseWhen .= ' THEN '; $caseWhen .= $query->concatenate(array('a.name', 'a.state_code', 'a.country_code'), ', '); $caseWhen .= ' ELSE '; $caseWhen .= $query->concatenate(array('a.name', 'a.country_code'), ', '); $caseWhen .= ' END as name'; $query->select("a.id, " . $caseWhen)->from($this->db->quoteName("#__crowdf_locations", "a"))->where($this->db->quoteName("a.name") . " LIKE " . $search); $this->db->setQuery($query, 0, 8); $results = $this->db->loadAssocList(); if (!$results) { $results = array(); } $this->items = $results; }
/** * Get a database escaped string. For LIKE statemends: $db->Quote( $db->getEscaped( $text, true ) . '%', false ) * * @param string $text * @param boolean $escapeForLike : escape also % and _ wildcards for LIKE statements with % or _ in search strings (since CB 1.2.3) * @return string */ public function getEscaped($text, $escapeForLike = false) { return $this->_db->escape($text, $escapeForLike); }
/** * {@inheritdoc} */ public function escape($text) { return $this->_db->escape($text); }
/** * Get a database escaped string. For LIKE statemends: $db->Quote( $db->getEscaped( $text, true ) . '%', false ) * * @param string $text * @param boolean $escapeForLike : escape also % and _ wildcards for LIKE statements with % or _ in search strings (since CB 1.2.3) * @return string */ function getEscaped($text, $escapeForLike = false) { if (checkJversion() >= 2) { $result = $this->_db->escape($text); } else { $result = $this->_db->getEscaped($text); } if ($escapeForLike) { $result = str_replace(array('%', '_'), array("\\%", "\\_"), $result); } return $result; }
/** * {@inheritdoc} * * @codeCoverageIgnore */ public function escape($text, $extra = false) { return $this->_db->escape($text, $extra); }