/** * Retrieve all posts to display on a specific profile page * * @param Object $target The target to retrieve posts for * * @return array[array[string]]string Returns a 2D array with each internal array holding the relevant post data */ public function get_posts($target) { if ($target->exists()) { $sql = "SELECT id, owner_id, post_data FROM posts WHERE target_id=? AND (target_type=?) ORDER BY created DESC LIMIT 10"; if (empty($this->_db->query($sql, [$target->id(), $target->type()])->errors())) { if (!empty($this->_db->results())) { $posts_info = array(); foreach ($this->_db->results() as $result) { $user = new User(); $user->find($result->owner_id, 'users', ['id']); $posts_info[] = ['id' => $result->id, 'content' => $result->post_data, 'user' => $user->name(), 'avatar' => $user->avatar()]; } return $posts_info; } } } return false; }
/** * Send reviews to Slack * * @param array $reviews list of reviews to send * @return boolean successful sending */ public function sendReviews($reviews) { if (!is_array($reviews) || !count($reviews)) { return false; } if (!isset($this->slackSettings['endpoint'])) { if ($this->logger) { $this->logger->error('Reviewer: you should set endpoint in Slack settings'); } return false; } $firstTime = !(bool) $this->storage->exists('tjournal:reviewer:init'); $config = ['username' => 'TJ Reviewer', 'icon' => 'https://i.imgur.com/GX1ASZy.png']; if (isset($this->slackSettings['channel'])) { $config['channel'] = $this->slackSettings['channel']; } $slack = new Slack($this->slackSettings['endpoint'], $config); foreach ($reviews as $review) { $ratingText = ''; for ($i = 1; $i <= 5; $i++) { $ratingText .= $i <= $review['rating'] ? "★" : "☆"; } try { if (!$firstTime) { $slack->attach(['fallback' => "{$ratingText} {$review['title']} — {$review['content']}", 'author_name' => $review['application']['name'], 'author_icon' => $review['application']['image'], 'author_link' => $review['application']['link'], 'color' => $review['rating'] >= 4 ? 'good' : ($review['rating'] == 3 ? 'warning' : 'danger'), 'fields' => [['title' => $review['title'], 'value' => $review['content']], ['title' => 'Rating', 'value' => $ratingText, 'short' => true], ['title' => 'Author', 'value' => "<{$review['author']['uri']}|{$review['author']['name']}>", 'short' => true], ['title' => 'Version', 'value' => $review['application']['version'], 'short' => true], ['title' => 'Country', 'value' => $review['country'], 'short' => true]]])->send(); } $this->storage->sadd("tjournal:reviewer:reviews", $review['id']); } catch (Exception $e) { if ($this->logger) { $this->logger->error('Reviewer: exception while sending reviews', ['exception' => $e]); } } } $this->storage->set('tjournal:reviewer:init', 1); return true; }
/** * Gets or creates the current order. * IMPORTANT FUNCTION! * @todo - does this need to be public???? * @return void */ public function currentOrder() { if (!$this->order) { $sessionVariableName = $this->sessionVariableName("OrderID"); $orderIDFromSession = intval(Session::get($sessionVariableName)); if ($orderIDFromSession > 0) { $this->order = DataObject::get_by_id("Order", $orderIDFromSession); } //order has already been submitted - immediately remove it because we dont want to change it. $member = Member::currentUser(); if ($this->order) { //first reason to set to null: it is already submitted if ($this->order->IsSubmitted()) { $this->order = null; } elseif (!$this->order->canView()) { $this->order = null; } elseif ($member && $member->exists()) { if ($this->order->MemberID != $member->ID) { $updateMember = false; if (!$this->order->MemberID) { $updateMember = true; } if (!$member->IsShopAdmin()) { $updateMember = true; } if ($updateMember) { $this->order->MemberID = $member->ID; $this->order->write(); } } //current order has nothing in it AND the member already has an order: use the old one first if ($this->order->StatusID) { $firstStep = DataObject::get_one("OrderStep"); //we assume the first step always exists. //TODO: what sort order? $previousOrderFromMember = DataObject::get_one("Order", "\r\n\t\t\t\t\t\t\t\t\"MemberID\" = " . $member->ID . "\r\n\t\t\t\t\t\t\t\tAND (\"StatusID\" = " . $firstStep->ID . " OR \"StatusID\" = 0)\r\n\t\t\t\t\t\t\t\tAND \"Order\".\"ID\" <> " . $this->order->ID); if ($previousOrderFromMember && $previousOrderFromMember->canView()) { if ($previousOrderFromMember->StatusID) { $this->order->delete(); $this->order = $previousOrderFromMember; } else { $previousOrderFromMember->delete(); } } } } } if (!$this->order) { if ($member) { $firstStep = DataObject::get_one("OrderStep"); $previousOrderFromMember = DataObject::get_one("Order", "\"MemberID\" = " . $member->ID . " AND (\"StatusID\" = " . $firstStep->ID . " OR \"StatusID\" = 0)"); if ($previousOrderFromMember) { if ($previousOrderFromMember->canView()) { $this->order = $previousOrderFromMember; } } } if (!$this->order) { //here we cleanup old orders, because they should be cleaned at the same rate that they are created... $cleanUpEveryTime = EcommerceConfig::get("ShoppingCart", "cleanup_every_time"); if ($cleanUpEveryTime) { $obj = new CartCleanupTask(); $obj->runSilently(); } //create new order $this->order = new Order(); if ($member) { $this->order->MemberID = $member->ID; } $this->order->write(); } Session::set($sessionVariableName, intval($this->order->ID)); } //member just logged in and is not associated with order yet //if you are not logged in but the order belongs to a member then clear the cart. /***** THIS IS NOT CORRECT, BECAUSE YOU CAN CREATE AN ORDER FOR A USER AND NOT BE LOGGED IN!!! *** elseif($this->order->MemberID && !$member) { $this->clear(); return false; } */ if ($this->order && $this->order->exists() && $this->order->StatusID) { $this->order->calculateOrderAttributes(); } } return $this->order; }
/** * Search a secondary index * * @author Eric Stevens <*****@*****.**> * @param string $indexName * - The name of the index to search * @param string $indexType * - The type of index ('int' or 'bin') * @param string|int $startOrExact * @param * string|int optional $end * @param * bool optional $dedupe - whether to eliminate duplicate entries if any * @return array of Links */ function indexSearch($indexName, $indexType, $startOrExact, $end = NULL, $dedupe = false) { $url = Utils::buildIndexPath($this->client, $this, "{$indexName}_{$indexType}", $startOrExact, $end); $response = Utils::httpRequest('GET', $url); $obj = new Object($this->client, $this, NULL); $obj->populate($response, array(200)); if (!$obj->exists()) { throw new Exception("Error searching index."); } $data = $obj->getData(); $keys = array_map("urldecode", $data["keys"]); $seenKeys = array(); foreach ($keys as $id => &$key) { if ($dedupe) { if (isset($seenKeys[$key])) { unset($keys[$id]); continue; } $seenKeys[$key] = true; } $key = new Link($this->name, $key); $key->client = $this->client; } return $keys; }
/** * Increment or decrement the comment count cache on the associated model * * @param Object $model Model to change count of * @param mixed $id The id to change count of * @param string $direction 'up' or 'down' * @access public * @return null */ public function changeCommentCount(&$model, $id = null, $direction = 'up') { if ($model->hasField('comments')) { if ($direction == 'up') { $direction = '+ 1'; } elseif ($direction == 'down') { $direction = '- 1'; } else { $direction = null; } $model->id = $id; if (!is_null($direction) && $model->exists(true)) { return $model->updateAll(array($model->alias . '.comments' => 'comments ' . $direction), array($model->alias . '.id' => $id)); } } return false; }
public function exists() { return parent::exists() && class_exists($this->class) && is_object($this->getRecord()); }
/** * @param int|string|array|Object $record * @return string * @throws DbModelException */ public function getRecordCacheTag($record) { /** @var CmfDbModel|CacheableDbModel $this */ if ($record instanceof DbObject) { $id = $record->exists() ? $record->_getPkValue() : null; } else { if (!is_array($record)) { $id = $record; } else { if (!empty($record[$this->getPkColumnName()])) { $id = $record[$this->getPkColumnName()]; } else { if (!empty($record[$this->getAlias()]) && !empty($record[$this->getAlias()][$this->getPkColumnName()])) { $id = $record[$this->getAlias()][$this->getPkColumnName()]; } } } } if (empty($id)) { throw new DbModelException($this, 'Data passed to getRecordCacheTag() has no value for primary key'); } return $this->getModelCachePrefix() . 'id_' . $id; }
/** * Gets or creates the current order. * Based on the session ONLY! * IMPORTANT FUNCTION! * @todo - does this need to be public???? * @return void */ public function currentOrder($recurseCount = 0) { if (!$this->order) { $sessionVariableName = $this->sessionVariableName("OrderID"); $orderIDFromSession = intval(Session::get($sessionVariableName)); if ($orderIDFromSession > 0) { $this->order = Order::get()->byID($orderIDFromSession); } $member = Member::currentUser(); if ($this->order) { //first reason to set to null: it is already submitted if ($this->order->IsSubmitted()) { $this->order = null; } elseif (!$this->order->canView()) { $this->order = null; } elseif ($member && $member->exists()) { if ($this->order->MemberID != $member->ID) { $updateMember = false; if (!$this->order->MemberID) { $updateMember = true; } if (!$member->IsShopAdmin()) { $updateMember = true; } if ($updateMember) { $this->order->MemberID = $member->ID; $this->order->write(); } } //IF current order has nothing in it AND the member already has an order: use the old one first //first, lets check if the current order is worthwhile keeping if ($this->order->StatusID || $this->order->TotalItems()) { //do NOTHING! } else { $firstStep = OrderStep::get()->First(); //we assume the first step always exists. //TODO: what sort order? $count = 0; while ($firstStep && ($previousOrderFromMember = Order::get()->where("\r\n\t\t\t\t\t\t\t\t\t\"MemberID\" = " . $member->ID . "\r\n\t\t\t\t\t\t\t\t\tAND (\"StatusID\" = " . $firstStep->ID . " OR \"StatusID\" = 0)\r\n\t\t\t\t\t\t\t\t\tAND \"Order\".\"ID\" <> " . $this->order->ID)->First())) { //arbritary 12 attempts ... if ($count > 12) { break; } $count++; if ($previousOrderFromMember && $previousOrderFromMember->canView()) { if ($previousOrderFromMember->StatusID || $previousOrderFromMember->TotalItems()) { $this->order->delete(); $this->order = $previousOrderFromMember; break; } else { $previousOrderFromMember->delete(); } } } } } } if (!$this->order) { if ($member) { $firstStep = OrderStep::get()->First(); if ($firstStep) { $previousOrderFromMember = Order::get()->filter(array("MemberID" => $member->ID, "StatusID" => array($firstStep->ID, 0)))->First(); if ($previousOrderFromMember) { if ($previousOrderFromMember->canView()) { $this->order = $previousOrderFromMember; } } } } if ($this->order && !$this->order->exists()) { $this->order = null; } if (!$this->order) { //here we cleanup old orders, because they should be //cleaned at the same rate that they are created... if (EcommerceConfig::get("ShoppingCart", "cleanup_every_time")) { $cartCleanupTask = EcommerceTaskCartCleanup::create(); $cartCleanupTask->runSilently(); } //create new order $this->order = Order::create(); if ($member) { $this->order->MemberID = $member->ID; } $this->order->write(); } Session::set($sessionVariableName, intval($this->order->ID)); } elseif ($this->order->MemberID && !$member) { $this->clear(); $this->order = null; } if ($this->order && $this->order->exists()) { $this->order->calculateOrderAttributes($force = false); } if ($this->order && !$this->order->SessionID) { $this->order->SessionID = session_id(); $this->order->write(); } } //just in case ... if (!$this->order && $recurseCount < 3) { $recurseCount++; return $this->currentOrder(); } return $this->order; }
/** * @param array|object $input * @param array|object $output */ public function map($input, &$output) { if ($this->source === null) { $value = $this->encode($this->default); } elseif (Object::exists($this->source, $input, $value)) { $value = $this->encode($value); } else { $value = $this->default; } Object::assign($this->target, $output, $value, $this->mode); }
/** * Check if the repositoriesServiceProvider file already exist in the app/providers folder. * @param Object $fileSystem * @return Boolean */ private function repositoriesServiceProviderExist($fileSystem) { return $fileSystem->exists(app_path() . "/Providers/RepositoriesServiceProvider.php"); }