/** * Set the state of an entry * * @return void */ public function stateTask() { // Check for request forgeries Request::checkToken(['get', 'post']); $state = $this->getTask() == 'grant' ? 1 : 0; // Incoming $cid = Request::getInt('cid', 0); $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; // Check for an ID if (count($ids) < 1) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $state == 1 ? Lang::txt('COM_WISHLIST_SELECT_PUBLISH') : Lang::txt('COM_WISHLIST_SELECT_UNPUBLISH'), 'error'); return; } // Update record(s) foreach ($ids as $id) { // Updating a category $row = new Wish($this->database); $row->load($id); $row->status = $state; $row->store(); } // Set message switch ($state) { case '-1': $message = Lang::txt('COM_WISHLIST_TRASHED', count($ids)); break; case '1': $message = Lang::txt('COM_WISHLIST_ITEMS_GRANTED', count($ids)); break; case '0': $message = Lang::txt('COM_WISHLIST_ITEMS_PENDING', count($ids)); break; } // Set the redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . ($cid ? '&id=' . $cid : ''), false), $message); }
/** * Show a form for editing a wish * * @return void */ public function editwishTask() { $refid = Request::getInt('rid', 0); $cat = Request::getVar('category', ''); $status = Request::getVar('status', ''); $vid = Request::getInt('vid', 0); // Check if wish exists on this list if ($id = Request::getInt('id', 0)) { $wishlist = Wishlist::getInstance(Request::getInt('id', 0)); } else { $wishlist = Wishlist::getInstance($refid, $cat); } if (!$wishlist->exists()) { throw new Exception(Lang::txt('COM_WISHLIST_ERROR_WISHLIST_NOT_FOUND'), 404); } // load wish $wish = new Wish(Request::getInt('wishid', 0)); if (!$wish->exists()) { throw new Exception(Lang::txt('COM_WISHLIST_ERROR_WISH_NOT_FOUND'), 404); } $changed = false; // Login required if (User::isGuest()) { // Set page title $this->_list_title = ($wishlist->isPublic() or !$wishlist->isPublic() && $wishlist->get('admin') == 2) ? $wishlist->get('title') : ''; $this->_buildTitle(); // Set the pathway $this->_taskpath = $wish->link(); $this->_taskname = Lang::txt(strtoupper($this->_option) . '_' . strtoupper($this->_task)); $this->_buildPathway($wishlist); $this->loginTask(); return; } if (!$wishlist->access('manage') && $wish->get('proposed_by') != User::get('id')) { throw new Exception(Lang::txt('COM_WISHLIST_ALERTNOTAUTH'), 403); } if ($this->_task == 'editprivacy') { $private = Request::getInt('private', 0, 'get'); if ($wish->get('private') != $private) { $wish->set('private', $private); $changed = true; } } if ($this->_task == 'editwish' && ($status = Request::getVar('status', ''))) { $former_status = $wish->get('status'); $former_accepted = $wish->get('accepted'); switch ($status) { case 'pending': $wish->set('status', 0); $wish->set('accepted', 0); break; case 'accepted': $wish->set('status', 0); $wish->set('accepted', 1); $wish->set('assigned', User::get('id')); // assign to person who accepted the wish break; case 'rejected': $wish->set('accepted', 0); $wish->set('status', 3); // return bonuses if ($this->banking) { $WE = new Economy($this->database); $WE->cleanupBonus($wish->get('id')); } break; case 'granted': $wish->set('status', 1); $wish->set('granted', Date::toSql()); $wish->set('granted_by', User::get('id')); $wish->set('granted_vid', $vid ? $vid : 0); $objWish = new Tables\Wish($this->database); $w = $objWish->get_wish($wish->get('id'), User::get('id')); $wish->set('points', $w->bonus); if ($this->banking) { // Distribute bonus and earned points $WE = new Economy($this->database); $WE->distribute_points($wish->get('id')); } break; } if ($former_status != $wish->get('status') or $former_accepted != $wish->get('accepted')) { $changed = true; } if ($changed) { // Build e-mail components // to wish author $subject1 = Lang::txt(strtoupper($this->_option)) . ', ' . Lang::txt('COM_WISHLIST_YOUR_WISH') . ' #' . $wish->get('id') . ' is ' . $status; // to wish assignee $subject2 = Lang::txt(strtoupper($this->_option)) . ', ' . Lang::txt('COM_WISHLIST_WISH') . ' #' . $wish->get('id') . ' ' . Lang::txt('COM_WISHLIST_HAS_BEEN') . ' ' . Lang::txt('COM_WISHLIST_MSG_ASSIGNED_TO_YOU'); $from = array('name' => Config::get('sitename') . ' ' . Lang::txt(strtoupper($this->_option)), 'email' => Config::get('mailfrom')); $message = array(); // Plain text email $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'wish_plain')); $eview->option = $this->_option; $eview->controller = $this->_controller; $eview->wish = $wish; $eview->wishlist = $wishlist; $eview->action = 'updated'; $eview->status = $status; $message['plaintext'] = $eview->loadTemplate(false); $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']); // HTML email $eview->setLayout('wish_html'); $message['multipart'] = $eview->loadTemplate(); $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']); } } else { if ($this->_task == 'editwish') { $this->addwishTask($wish->get('id')); return; } } if ($changed) { // save changes if (!$wish->store()) { throw new Exception($wish->getError(), 500); } else { if ($this->_task == 'editwish') { if (!Event::trigger('xmessage.onSendMessage', array('wishlist_status_changed', $subject1, $message, $from, array($wish->get('proposed_by')), $this->_option))) { $this->setError(Lang::txt('COM_WISHLIST_ERROR_FAILED_MSG_AUTHOR')); } if ($wish->get('assigned') && $wish->get('proposed_by') != $wish->get('assigned') && $status == 'accepted') { if (!Event::trigger('xmessage.onSendMessage', array('wishlist_wish_assigned', $subject2, $as_mes, $from, array($wish->get('assigned')), $this->_option))) { $this->setError(Lang::txt('COM_WISHLIST_ERROR_FAILED_MSG_ASSIGNEE')); } } } } } App::redirect(Route::url($wish->link())); }
/** * Get a count or list of wishes * * @param string $rtrn What data to return [count, list, first] * @param array $filters Filters to apply to data fetch * @param boolean $clear Clear cached data? * @return mixed */ public function wishes($rtrn = '', $filters = array(), $clear = false) { if (!isset($filters['wishlist'])) { $filters['wishlist'] = (int) $this->get('id'); } $tbl = new Tables\Wish($this->_db); switch (strtolower($rtrn)) { case 'count': if (!is_numeric($this->_cache['wishes.count']) || $clear) { $this->_cache['wishes.count'] = (int) $tbl->get_count($this->get('id'), $filters, $this->get('admin'), User::getInstance()); } return $this->_cache['wishes.count']; break; case 'list': case 'results': default: if (!$this->_cache['wishes.list'] instanceof ItemList || $clear) { if ($results = $tbl->get_wishes($this->get('id'), $filters, $this->get('admin'), User::getInstance())) { foreach ($results as $key => $result) { $results[$key] = new Wish($result); } } else { $results = array(); } $this->_cache['wishes.list'] = new ItemList($results); } return $this->_cache['wishes.list']; break; } }
/** * Distribute points * * @param integer $wishid Wish ID * @param string $type Transaction type * @param number $points Points to distribute * @return void */ public function distribute_points($wishid, $type = 'grant', $points = 0) { if (!$wishid) { return null; } require_once dirname(__DIR__) . DS . 'models' . DS . 'wishlist.php'; $objWish = new Tables\Wish($this->_db); $wish = $objWish->get_wish($wishid); $points = !$points ? $wish->bonus : $points; // Points for list owners if ($points > 0 && $type != 'royalty') { // Get the component parameters $wconfig = Component::params('com_wishlist'); $admingroup = $wconfig->get('group', 'hubadmin'); // get list owners $objOwner = new Tables\Owner($this->_db); $owners = $objOwner->get_owners($wish->wishlist, $admingroup, '', 0, $wishid); $owners = $owners['individuals']; $mainshare = $wish->assigned ? $points * 0.8 : 0; //80% $commonshare = $mainshare ? ($points - $mainshare) / count($owners) : $points / count($owners); // give the remaining 20% if ($owners && $commonshare) { foreach ($owners as $owner) { $o = User::getInstance($owner); if (!is_object($o) || !$o->get('id')) { continue; } $BTLO = new Teller($this->_db, $owner); if ($wish->assigned && $wish->assigned == $owner) { //$BTLO->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish').' #'.$wishid.' '.Lang::txt('on list').' #'.$wish->wishlist, 'wish', $wishid); $mainshare += $commonshare; } else { $BTLO->deposit($commonshare, Lang::txt('Bonus for fulfilling wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } else { $mainshare += $commonshare; } // give main share if ($wish->assigned && $mainshare) { $o = User::getInstance($wish->assigned); if (is_object($o) && $o->get('id')) { $BTLM = new Teller($this->_db, $wish->assigned); $BTLM->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } // Adjust credits $payees = $this->getPayees($wishid); if ($payees) { foreach ($payees as $p) { $o = User::getInstance($p->uid); if (!is_object($o) || !$o->get('id')) { continue; } $BTL = new Teller($this->_db, $p->uid); $hold = $this->getTotalPayment($wishid, $p->uid); if ($hold) { $credit = $BTL->credit_summary(); $adjusted = $credit - $hold; $BTL->credit_adjustment($adjusted); // withdraw bonus amount $BTL->withdraw($hold, Lang::txt('Bonus payment for granted wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } // Remove holds if exist if ($wish->bonus) { $BT = new Transaction($this->_db); $BT->deleteRecords('wish', 'hold', $wishid); } } // Points for wish author (needs to be granted by another person) if ($wish->ranking > 0 && $wish->proposed_by != User::get('id') && $wish->proposed_by) { $o = User::getInstance($wish->proposed_by); if (is_object($o) && $o->get('id')) { $BTLA = new Teller($this->_db, $wish->proposed_by); $BTLA->deposit($wish->ranking, Lang::txt('Your wish #%s on list #%s was granted', $wishid, $wish->wishlist), 'wish', $wishid); } } }