/** * Remove an extra image from database and file system. * * <code> * $imageId = 1; * $imagesFolder = "/.../folder"; * * $image = new CrowdFundingImageRemoverExtra(JFactory::getDbo(), $image, $imagesFolder); * $image->remove(); * </code> */ public function remove() { // Get the image $query = $this->db->getQuery(true); $query->select("a.image, a.thumb")->from($this->db->quoteName("#__crowdf_images", "a"))->where("a.id = " . (int) $this->imageId); $this->db->setQuery($query); $row = $this->db->loadObject(); if (!empty($row)) { // Remove the image from the filesystem $file = JPath::clean($this->imagesFolder . DIRECTORY_SEPARATOR . $row->image); if (JFile::exists($file)) { JFile::delete($file); } // Remove the thumbnail from the filesystem $file = JPath::clean($this->imagesFolder . DIRECTORY_SEPARATOR . $row->thumb); if (JFile::exists($file)) { JFile::delete($file); } // Delete the record $query = $this->db->getQuery(true); $query->delete($this->db->quoteName("#__crowdf_images"))->where($this->db->quoteName("id") . " = " . (int) $this->imageId); $this->db->setQuery($query); $this->db->execute(); } }
/** * Get the Content Type * * @param integer $pk The primary key of the alias type * * @return object The UCM Type data * * @since 3.1 */ public function getType($pk = null) { if (!$pk) { $pk = $this->getTypeId(); } $query = $this->db->getQuery(true); $query->select('ct.*'); $query->from($this->db->quoteName('#__content_types', 'ct')); $query->where($this->db->quoteName('ct.type_id') . ' = ' . (int) $pk); $this->db->setQuery($query); $type = $this->db->loadObject(); return $type; }
/** * Calculate three types of project amount - goal, funded amount and remaining amount. * * <code> * $projectId = 1; * * $statistics = new CrowdfundingStatisticsProject(\JFactory::getDbo(), $projectId); * $data = $statistics->getFundedAmount(); * </code> * * @return array * * # Example result: * array( * "goal" = array("label" => "Goal", "amount" => 1000), * "funded" = array("label" => "Funded", "amount" => 100), * "remaining" = array("label" => "Remaining", "amount" => 900) * ) */ public function getFundedAmount() { $data = array(); $query = $this->db->getQuery(true); $query->select("a.funded, a.goal")->from($this->db->quoteName("#__crowdf_projects", "a"))->where("a.id = " . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); /** @var $result object */ if (empty($result->funded) or empty($result->goal)) { return $data; } // Get currency $params = \JComponentHelper::getParams("com_crowdfunding"); /** @var $params Registry */ $currencyId = $params->get("project_currency"); $currency = Currency::getInstance(\JFactory::getDbo(), $currencyId, $params); $amount = new Amount(); $amount->setCurrency($currency); $data["goal"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_GOAL_S", $amount->setValue($result->goal)->formatCurrency()), "amount" => (double) $result->goal); $data["funded"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_FUNDED_S", $amount->setValue($result->funded)->formatCurrency()), "amount" => (double) $result->funded); $remaining = (double) ($result->goal - $result->funded); if ($remaining < 0) { $remaining = 0; } $data["remaining"] = array("label" => \JText::sprintf("COM_CROWDFUNDINGFINANCE_REMAINING_S", $amount->setValue($remaining)->formatCurrency()), "amount" => $remaining); return $data; }
/** * Method to move a row in the ordering sequence of a group of rows defined by an SQL WHERE clause. * Negative numbers move the row up in the sequence and positive numbers move it down. * * @param integer $delta The direction and magnitude to move the row in the ordering sequence. * @param string $where WHERE clause to use for limiting the selection of rows to compact the * ordering values. * * @return mixed Boolean True on success. * * @link http://docs.joomla.org/JTable/move * @since 11.1 * @throws UnexpectedValueException */ public function move($delta, $where = '') { // If there is no ordering field set an error and return false. if (!property_exists($this, 'ordering')) { throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this))); } // If the change is none, do nothing. if (empty($delta)) { return true; } $k = $this->_tbl_key; $row = null; $query = $this->_db->getQuery(true); // Select the primary key and ordering values from the table. $query->select($this->_tbl_key . ', ordering'); $query->from($this->_tbl); // If the movement delta is negative move the row up. if ($delta < 0) { $query->where('ordering < ' . (int) $this->ordering); $query->order('ordering DESC'); } elseif ($delta > 0) { $query->where('ordering > ' . (int) $this->ordering); $query->order('ordering ASC'); } // Add the custom WHERE clause if set. if ($where) { $query->where($where); } // Select the first row with the criteria. $this->_db->setQuery($query, 0, 1); $row = $this->_db->loadObject(); // If a row is found, move the item. if (!empty($row)) { // Update the ordering field for this instance to the row's ordering value. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set('ordering = ' . (int) $row->ordering); $query->where($this->_tbl_key . ' = ' . $this->_db->quote($this->{$k})); $this->_db->setQuery($query); $this->_db->execute(); // Update the ordering field for the row to this instance's ordering value. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set('ordering = ' . (int) $this->ordering); $query->where($this->_tbl_key . ' = ' . $this->_db->quote($row->{$k})); $this->_db->setQuery($query); $this->_db->execute(); // Update the instance value. $this->ordering = $row->ordering; } else { // Update the ordering field for this instance. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set('ordering = ' . (int) $this->ordering); $query->where($this->_tbl_key . ' = ' . $this->_db->quote($this->{$k})); $this->_db->setQuery($query); $this->_db->execute(); } return true; }
/** * Function to retrieve the full article object * * @param object $article The content object * * @return object The full content object * * @since 1.0 */ private function loadArticle($article) { // Query the database for the article text $query = $this->db->getQuery(true)->select('*')->from($this->db->quoteName('#__content'))->where($this->db->quoteName('introtext') . ' = ' . $this->db->quote($article->text)); $this->db->setQuery($query); return $this->db->loadObject(); }
/** * Count and return transactions number. * * <code> * $usersId = 1; * * $statistics = new CrowdFundingStatisticsUsers(JFactory::getDbo(), $usersId); * $transactionsNumber = $statistics->getAmounts(); * </code> * * @return array */ public function getAmounts() { // If there are no IDs, return empty array. if (!$this->id) { return array(); } $statistics = array("invested" => array(), "received" => array()); // Count invested amount and transactions. $query = $this->db->getQuery(true); $query->select("COUNT(*) AS number, SUM(a.txn_amount) AS amount")->from($this->db->quoteName("#__crowdf_transactions", "a"))->where("a.investor_id = " . (int) $this->id); $this->db->setQuery($query); $results = $this->db->loadObject(); if (!$results) { $results = array(); } $statistics["invested"] = $results; // Count received amount and transactions. $query = $this->db->getQuery(true); $query->select("COUNT(*) AS number, SUM(a.txn_amount) AS amount")->from($this->db->quoteName("#__crowdf_transactions", "a"))->where("a.receiver_id = " . (int) $this->id); $this->db->setQuery($query); $results = $this->db->loadObject(); if (!$results) { $results = array(); } $statistics["received"] = $results; return $statistics; }
/** * Calculate three types of project amount - goal, funded amount and remaining amount. * * <code> * $projectId = 1; * * $statistics = new Crowdfunding\Statistics\Project(\JFactory::getDbo(), $projectId); * $data = $statistics->getFundedAmount(); * </code> * * @return array * * # Example result: * array( * "goal" = array("label" => "Goal", "amount" => 1000), * "funded" = array("label" => "Funded", "amount" => 100), * "remaining" = array("label" => "Remaining", "amount" => 900) * ) */ public function getFundedAmount() { $data = array(); $query = $this->db->getQuery(true); $query->select('a.funded, a.goal')->from($this->db->quoteName('#__crowdf_projects', 'a'))->where('a.id = ' . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); /** @var $result \stdClass */ if ($result->funded === null or $result->goal === null) { return $data; } // Get currency $params = \JComponentHelper::getParams('com_crowdfunding'); /** @var $params Registry */ $currencyId = $params->get('project_currency'); $currency = Currency::getInstance(\JFactory::getDbo(), $currencyId, $params); $amount = new Amount(); $amount->setCurrency($currency); $data['goal'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_GOAL_S', $amount->setValue($result->goal)->formatCurrency()), 'amount' => (double) $result->goal); $data['funded'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_FUNDED_S', $amount->setValue($result->funded)->formatCurrency()), 'amount' => (double) $result->funded); $remaining = (double) ($result->goal - $result->funded); if ($remaining < 0) { $remaining = 0; } $data['remaining'] = array('label' => \JText::sprintf('COM_CROWDFUNDINGFINANCE_REMAINING_S', $amount->setValue($remaining)->formatCurrency()), 'amount' => $remaining); return $data; }
/** * Method to move a row in the ordering sequence of a group of rows defined by an SQL WHERE clause. * * Negative numbers move the row up in the sequence and positive numbers move it down. * * @param integer $delta The direction and magnitude to move the row in the ordering sequence. * @param string $where WHERE clause to use for limiting the selection of rows to compact the ordering values. * * @return boolean True on success. * * @since 11.1 * @throws UnexpectedValueException */ public function move($delta, $where = '') { // If there is no ordering field set an error and return false. if (!property_exists($this, 'ordering')) { throw new UnexpectedValueException(sprintf('%s does not support ordering.', get_class($this))); } // If the change is none, do nothing. if (empty($delta)) { return true; } $k = $this->_tbl_key; $row = null; $query = $this->_db->getQuery(true); // Select the primary key and ordering values from the table. $query->select(implode(',', $this->_tbl_keys) . ', ordering')->from($this->_tbl); // If the movement delta is negative move the row up. if ($delta < 0) { $query->where('ordering < ' . (int) $this->ordering)->order('ordering DESC'); } elseif ($delta > 0) { $query->where('ordering > ' . (int) $this->ordering)->order('ordering ASC'); } // Add the custom WHERE clause if set. if ($where) { $query->where($where); } // Pre-processing by observers $event = AbstractEvent::create('onTableBeforeMove', ['subject' => $this, 'query' => $query, 'delta' => $delta, 'where' => $where]); $this->getDispatcher()->dispatch('onTableBeforeMove', $event); // Select the first row with the criteria. $this->_db->setQuery($query, 0, 1); $row = $this->_db->loadObject(); // If a row is found, move the item. if (!empty($row)) { // Update the ordering field for this instance to the row's ordering value. $query->clear()->update($this->_tbl)->set('ordering = ' . (int) $row->ordering); $this->appendPrimaryKeys($query); $this->_db->setQuery($query); $this->_db->execute(); // Update the ordering field for the row to this instance's ordering value. $query->clear()->update($this->_tbl)->set('ordering = ' . (int) $this->ordering); $this->appendPrimaryKeys($query, $row); $this->_db->setQuery($query); $this->_db->execute(); // Update the instance value. $this->ordering = $row->ordering; } else { // Update the ordering field for this instance. $query->clear()->update($this->_tbl)->set('ordering = ' . (int) $this->ordering); $this->appendPrimaryKeys($query); $this->_db->setQuery($query); $this->_db->execute(); } // Post-processing by observers $event = AbstractEvent::create('onTableAfterMove', ['subject' => $this, 'row' => $row, 'delta' => $delta, 'where' => $where]); $this->getDispatcher()->dispatch('onTableAfterMove', $event); return true; }
/** * Get the Content Type from the alias * * @param string $typeAlias The alias for the type * * @return object The UCM Type data * * @since 3.2 */ public function getTypeByAlias($typeAlias = null) { $query = $this->db->getQuery(true); $query->select('ct.*'); $query->from($this->db->quoteName('#__content_types', 'ct')); $query->where($this->db->quoteName('ct.type_alias') . ' = ' . $this->db->quote($typeAlias)); $this->db->setQuery($query); $type = $this->db->loadObject(); return $type; }
/** * Count and return transactions number. * * <code> * $usersId = 1; * * $statistics = new Crowdfunding\Statistics\Users(\JFactory::getDbo(), $usersId); * $transactionsNumber = $statistics->getAmounts(); * </code> * * @return array */ public function getAmounts() { $statistics = array('invested' => array(), 'received' => array()); // Count invested amount and transactions. $query = $this->db->getQuery(true); $query->select('COUNT(*) AS number, SUM(a.txn_amount) AS amount')->from($this->db->quoteName('#__crowdf_transactions', 'a'))->where('a.investor_id = ' . (int) $this->id); $this->db->setQuery($query); $statistics['invested'] = (array) $this->db->loadObject(); // Count received amount and transactions. $query = $this->db->getQuery(true); $query->select('COUNT(*) AS number, SUM(a.txn_amount) AS amount')->from($this->db->quoteName('#__crowdf_transactions', 'a'))->where('a.receiver_id = ' . (int) $this->id); $this->db->setQuery($query); $statistics['received'] = (array) $this->db->loadObject(); return $statistics; }
/** * Runs the check query and checks that 1 row is returned * If yes, return true, otherwise return false * * @return boolean true on success, false otherwise * * @since 2.5 */ public function check() { $this->checkStatus = -1; if ($this->checkQuery) { $this->db->setQuery($this->checkQuery); $rows = $this->db->loadObject(); if ($rows !== false) { if (count($rows) === $this->checkQueryExpected) { $this->checkStatus = 1; } else { $this->checkStatus = -2; } } else { $this->checkStatus = -2; } } return $this->checkStatus; }
/** * Prepare current and next badges. * * @param array $keys */ protected function prepareBadges($keys) { $this->currentUnit = Badge::getInstance($this->db, $keys); $userPoints = $this->points->getPoints(); // Get all units $query = $this->db->getQuery(true); $query->select("a.id, a.title, a.points, a.image, a.note, a.published, a.points_id, a.group_id")->from($this->db->quoteName("#__gfy_badges", "a"))->where("a.points_id = " . (int) $this->points->getPointsId())->where("a.published = 1")->where("a.points > " . (int) $userPoints); $this->db->setQuery($query, 0, 1); $result = $this->db->loadObject(); if (!empty($result)) { $this->nextUnit = new \Gamification\Badge\Badge($this->db); $this->nextUnit->bind($result); $this->percentage = $this->calculatePercentage($userPoints, $this->nextUnit->getPoints()); $this->percentNext = 100 - $this->percentage; } else { $this->percentage = 100; $this->percentNext = 100; } }
/** * This global function loads the first row of a query into an object * * If an object is passed to this function, the returned row is bound to the existing elements of <var>object</var>. * If <var>object</var> has a value of null, then all of the returned query fields returned in the object. * @param object|\stdClass $object * @return boolean Success * * @throws \RuntimeException */ public function loadObject(&$object) { if ($object === null) { $object = $this->_db->loadObject(); return is_object($object); } $array = $this->_db->loadAssoc(); if (!is_array($array)) { return false; } foreach (get_object_vars($object) as $k => $v) { if (substr($k, 0, 1) != '_') { if (array_key_exists($k, $array)) { $object->{$k} = $array[$k]; } } } return true; }
/** * Calculate three types of project amount - goal, funded amount and remaining amount. * * <code> * $projectId = 1; * * $statistics = new CrowdFundingStatisticsProject(JFactory::getDbo(), $projectId); * $data = $statistics->getFundedAmount(); * </code> * * @return array * * # Example result: * array( * "goal" = array("label" => "Goal", "amount" => 1000), * "funded" = array("label" => "Funded", "amount" => 100), * "remaining" = array("label" => "Remaining", "amount" => 900) * ) */ public function getFundedAmount() { $data = array(); $query = $this->db->getQuery(true); $query->select("a.funded, a.goal")->from($this->db->quoteName("#__crowdf_projects", "a"))->where("a.id = " . (int) $this->id); $this->db->setQuery($query); $result = $this->db->loadObject(); /** @var $result object */ if (empty($result->funded) or empty($result->goal)) { return $data; } $data["goal"] = array("label" => JText::_("LIB_CROWDFUNDING_GOAL"), "amount" => (double) $result->goal); $data["funded"] = array("label" => JText::_("LIB_CROWDFUNDING_FUNDED"), "amount" => (double) $result->funded); $remaining = (double) ($result->goal - $result->funded); if ($remaining < 0) { $remaining = 0; } $data["remaining"] = array("label" => JText::_("LIB_CROWDFUNDING_REMAINING"), "amount" => $remaining); return $data; }
/** * Runs the check query and checks that 1 row is returned * If yes, return true, otherwise return false * * @return boolean true on success, false otherwise * * @since 2.5 */ public function check() { $this->checkStatus = -1; if ($this->checkQuery) { $this->db->setQuery($this->checkQuery); try { $rows = $this->db->loadObject(); } catch (RuntimeException $e) { $rows = false; // Still render the error message from the Exception object JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); } if ($rows !== false) { if (count($rows) === $this->checkQueryExpected) { $this->checkStatus = 1; } else { $this->checkStatus = -2; } } else { $this->checkStatus = -2; } } return $this->checkStatus; }
/** * Get the the collation for a given table * * @param Registry $params * @param JDatabaseDriver $db * @param string $tableName * * @return string */ protected function getOriginalCollation($params, $db, $tableName) { if (!empty($tableName)) { $db->setQuery('SHOW TABLE STATUS LIKE ' . $db->q($tableName)); $info = $db->loadObject(); $origCollation = is_object($info) ? $info->Collation : $params->get('collation', 'none'); } else { $origCollation = $params->get('collation', 'none'); } return $origCollation; }
/** * Method to move a row in the ordering sequence of a group of rows defined by an SQL WHERE clause. * Negative numbers move the row up in the sequence and positive numbers move it down. * * @param integer $delta The direction and magnitude to move the row in the ordering sequence. * @param string $where WHERE clause to use for limiting the selection of rows to compact the * ordering values. * * @return mixed Boolean True on success. * * @throws UnexpectedValueException */ public function move($delta, $where = '') { if (!$this->onBeforeMove($delta, $where)) { return false; } // If there is no ordering field set an error and return false. $ordering_field = $this->getColumnAlias('ordering'); if (!in_array($ordering_field, $this->getKnownFields())) { throw new UnexpectedValueException(sprintf('%s does not support ordering.', $this->_tbl)); } // If the change is none, do nothing. if (empty($delta)) { $result = $this->onAfterMove(); return $result; } $k = $this->_tbl_key; $row = null; $query = $this->_db->getQuery(true); // If the table is not loaded, return false if (empty($this->{$k})) { return false; } // Select the primary key and ordering values from the table. $query->select(array($this->_db->qn($this->_tbl_key), $this->_db->qn($ordering_field))); $query->from($this->_tbl); // If the movement delta is negative move the row up. if ($delta < 0) { $query->where($this->_db->qn($ordering_field) . ' < ' . $this->_db->q((int) $this->{$ordering_field})); $query->order($this->_db->qn($ordering_field) . ' DESC'); } elseif ($delta > 0) { $query->where($this->_db->qn($ordering_field) . ' > ' . $this->_db->q((int) $this->{$ordering_field})); $query->order($this->_db->qn($ordering_field) . ' ASC'); } // Add the custom WHERE clause if set. if ($where) { $query->where($where); } // Select the first row with the criteria. $this->_db->setQuery($query, 0, 1); $row = $this->_db->loadObject(); // If a row is found, move the item. if (!empty($row)) { // Update the ordering field for this instance to the row's ordering value. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set($this->_db->qn($ordering_field) . ' = ' . $this->_db->q((int) $row->{$ordering_field})); $query->where($this->_tbl_key . ' = ' . $this->_db->q($this->{$k})); $this->_db->setQuery($query); $this->_db->execute(); // Update the ordering field for the row to this instance's ordering value. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set($this->_db->qn($ordering_field) . ' = ' . $this->_db->q((int) $this->{$ordering_field})); $query->where($this->_tbl_key . ' = ' . $this->_db->q($row->{$k})); $this->_db->setQuery($query); $this->_db->execute(); // Update the instance value. $this->{$ordering_field} = $row->{$ordering_field}; } else { // Update the ordering field for this instance. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set($this->_db->qn($ordering_field) . ' = ' . $this->_db->q((int) $this->{$ordering_field})); $query->where($this->_tbl_key . ' = ' . $this->_db->q($this->{$k})); $this->_db->setQuery($query); $this->_db->execute(); } $result = $this->onAfterMove(); return $result; }