public function action_index() { $username = $this->request->param('username'); $this->view = new View_Message_Create(); $this->view->username = $username; if ($this->request->method() == HTTP_Request::POST) { try { $post = $this->request->post(); $receiver = ORM::factory('User')->where('username', '=', $post['receiver'])->find(); if (!$receiver->loaded()) { return Hint::error('Cannot find a user with the username: '******'You cannot send a message to yourself!'); } $message_data = Arr::merge($this->request->post(), array('sender_id' => $this->user->id, 'receiver_id' => $receiver->id)); $message = ORM::factory('Message')->create_message($message_data, array('receiver_id', 'subject', 'content', 'sender_id')); $message_data_sent = Arr::merge($this->request->post(), array('receiver_id' => $this->user->id, 'sender_id' => $receiver->id, 'sent' => 1, 'read' => 1)); ORM::factory('Message')->create_message($message_data_sent, array('receiver_id', 'subject', 'content', 'sender_id', 'sent', 'read')); Hint::success('You have sent a message'); $this->redirect(Route::get('message.inbox')->uri()); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } }
public function action_index() { $settings = new Settings(); $settings->add_setting(new Setting_Preferences($this->user)); $settings->add_setting(new Setting_Profile($this->user)); $settings->add_setting(new Setting_Account($this->user)); // Run the events. Event::fire('user.settings', array($this->user, $settings)); if ($this->request->method() == HTTP_Request::POST) { $setting = $settings->get_by_id($this->request->post('settings-tab')); if ($setting) { $post = $this->request->post(); $validation = $setting->get_validation($post); if ($validation->check()) { try { $setting->save($post); Hint::success('Updated ' . $setting->title . '!'); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } else { Hint::error($validation->errors()); } } else { Hint::error('Invalid settings id!'); } } $this->view = new View_User_Settings(); $this->view->settings = $settings; }
public function action_buy() { $shop_id = $this->request->param('id'); $shop = ORM::factory('Shop', $shop_id); if (!$shop->loaded()) { Hint::error('You can\'t buy an item from a shop that does not exist.'); } elseif ($shop->status == 'closed') { Hint::error('You\'re trying to buy an item from a closed shop.'); } else { $item_id = $this->request->post('id'); $item = ORM::factory('Shop_Inventory')->where('shop_id', '=', $shop->id)->where('item_id', '=', $item_id)->find(); if (!$item->loaded()) { Hint::error('The item you tried to buy has already been sold.'); } elseif ($item->price > $this->user->get_property('points')) { Hint::error('You don\'t have enough points to buy ' . $item->item->name); } else { // retract the points $this->user->set_property('points', $this->user->get_property('points') - $item->price); $this->user->save(); // send over the item Item::factory($item->item)->to_user($this->user, 'shops.' . $shop_id); // remove from shop if needed if ($shop->stock_type != 'steady') { if ($item->stock - 1 == 0) { $item->delete(); } else { $item->stock -= 1; $item->save(); } } Hint::success('You\'ve successfully bought ' . $item->item->name); } } $this->redirect(Route::get('item.shops.view')->uri(array('id' => $shop_id))); }
public function action_index() { if ($this->request->method() == HTTP_Request::POST) { try { $post = $this->request->post(); if ($post['action'] == 'delete') { foreach ($post['messages'] as $message) { $message = ORM::factory('Message')->where('id', '=', $message)->where('receiver_id', '=', $this->user->id)->where('sent', '=', 1)->find(); if ($message->loaded()) { $message->delete(); } } Hint::success('You have deleted the selected messages'); } $this->redirect(Route::url('message.outbox')); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view = new View_Message_Outbox(); // TODO: Add pagination $messages = ORM::factory('Message')->where('receiver_id', '=', $this->user->id)->where('sent', '=', 1)->order_by('created', 'DESC'); $paginate = Paginate::factory($messages)->execute(); $this->view->pagination = $paginate->render(); $this->view->messages = $paginate->result(); $this->view->outbox = 1; }
public function action_index() { if ($this->request->method() == HTTP_Request::POST) { try { if ($this->request->post('active')) { $pet = ORM::factory('User_Pet')->where('user_pet.id', '=', $this->request->post('active'))->where('user_id', '=', $this->user->id)->find(); $pet->active = time(); $pet->save(); Hint::success($pet->name . ' is now your active pet.'); } if ($this->request->post('abandon')) { $pet = ORM::factory('User_Pet')->where('user_pet.id', '=', $this->request->post('abandon'))->where('user_id', '=', $this->user->id)->find(); $pet->user_id = NULL; $pet->abandoned = time(); $pet->save(); Hint::success('You have abandoned ' . $pet->name . '.'); } $this->redirect(Route::get('pets')->uri()); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view = new View_Pet_Index(); $pets = ORM::factory('User_Pet')->where('user_id', '=', $this->user->id)->order_by('active', 'desc'); $paginate = Paginate::factory($pets)->execute(); $this->view->pagination = $paginate->render(); $this->view->pets = $paginate->result(); $this->view->pets_count = count($pets); $this->view->href = array('create' => Route::url('pet.create'), 'adopt' => Route::url('pet.adopt')); }
public function action_move() { $items = $this->request->post('items'); if (count($items) > 0) { foreach ($items as $id => $item) { if ($item['amount'] > 0) { $i = ORM::factory('User_Item', $id); if ($i->loaded() and $i->location == 'safe' and $i->user_id == $this->user->id) { if ($item['amount'] > $i->amount) { Hint::error(__('You can\'t move :name, you only have :amount.', array(':amount' => $i->amount, ':name' => $i->item->name($item['amount'])))); } elseif ($item['location'] == 'shop') { $shop = ORM::factory('User_Shop')->where('user_id', '=', $this->user->id)->find(); $shop_item = ORM::factory('User_Item')->where('user_id', '=', $this->user->id)->where('location', '=', 'shop')->where('item_id', '=', $i->item_id)->find(); if (!$shop->loaded()) { Hint::error('You don\'t have a shop yet.'); } elseif (!$shop->inventory_space() and !$shop_item->loaded()) { Hint::error('Your shop is already full.'); } else { $i->move('shop', $item['amount']); Hint::success(__('You\'ve moved :items to your shop.', array(':items' => $i->item->name($item['amount'])))); } } elseif ($item['location'] == 'inventory') { $i->move('inventory', $item['amount']); Hint::success(__('You\'ve moved :items to your inventory.', array(':items' => $i->item->name($item['amount'])))); } } } } } $this->redirect(Route::get('item.safe')->uri()); }
/** * Retrieve the news feed items. First try from cache, otherwise load it from the website. * @return array */ private function _get_news_feed() { $benchmark = Profiler::start('Admin Dashboard', __FUNCTION__); $cache = Cache::instance(); // Attempt to load feed from cache otherwise get it from the website. if (!($feed = $cache->get('admin.dashboard.news_feed', FALSE))) { try { $feed = Feed::parse($this->_news_feed_url); $cache->set('admin.dashboard.news_feed', $feed, 360); } catch (Exception $e) { Hint::error($e); } } Profiler::stop($benchmark); return $feed; }
/** * Create new topic. */ public function action_create() { $this->logged_in_required(); if (!$this->user->can('Forum_Topic_Create', array('category' => $this->category))) { throw HTTP_Exception::factory('403', 'Category is locked'); } if ($this->request->method() == HTTP_Request::POST) { try { $topic = new Model_Forum_Topic(); $topic->create_topic(array('category_id' => $this->category->id, 'user_id' => $this->user->id, 'title' => $this->request->post('title'), 'content' => $this->request->post('content')), array('category_id', 'user_id', 'title')); Hint::success('You have created a topic'); $this->redirect(Route::get('forum.topic')->uri(array('id' => $topic->id))); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view = new View_Forum_Topic_Create(); $this->view->category = $this->category; }
/** * Display AND handle the register form. */ public function action_index() { $this->_not_logged_in(); if ($this->request->method() == HTTP_Request::POST) { if ($this->_honeypot_empty()) { try { $user = $this->_create_user($this->request->post()); $this->_send_welcome_email($user); // Log in the user, and send him to his dashboard. $this->auth->force_login($user); $this->redirect(Route::get('user')->uri()); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } else { Hint::error(Kohana::message('user', 'register.honeypot')); } } $this->view = new View_User_Register(); }
public function action_index() { if ($this->request->method() == HTTP_Request::POST) { try { $array = Arr::merge($this->request->post(), array('user_id' => $this->user->id, 'active' => time())); $new_pet = ORM::factory('User_Pet')->create_pet($array, array('user_id', 'specie_id', 'colour_id', 'gender', 'name', 'active')); Hint::success('You have created a pet named ' . $new_pet->name); $this->redirect(Route::get('pets')->uri()); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $species = ORM::factory('Pet_Specie')->find_all(); $this->view = new View_Pet_Create(); $colours = ORM::factory('Pet_Colour')->where('locked', '=', 0)->find_all(); $this->view->colours = $colours; $this->view->species = $species; $this->view->default_specie = $species[0]->dir; $this->view->default_colour = $colours[0]->image; $this->view->href = array('adopt' => Route::url('pet.adopt')); }
/** * Display the login page AND handle login attempts. */ public function action_index() { $this->_not_logged_in(); if ($this->request->method() == HTTP_Request::POST) { $post = $this->request->post(); if ($this->auth->login($post['username'], $post['password'], isset($post['remember']))) { Hint::success(Kohana::message('user', 'login.success')); // Redirect the page to ?page= value if local url. if ($page = $this->request->query('page')) { // Ensure the url is local, we don't want the user to change site. if (strpos($page, '://') === FALSE) { $this->redirect($page); } } $this->redirect(Route::get('user.dashboard')->uri()); } else { Hint::error(Kohana::message('user', 'login.incorrect')); } } $this->view = new View_User_Login(); }
/** * Enter new password, accessed if token is in the url. */ public function action_token() { $tokens = ORM::factory('User_Property')->where('key', '=', 'reset_token')->find_all(); $token = $this->_get_token($tokens); if (!$token) { Hint::error('Incorrect token, perhaps it expired?'); $this->redirect(); } if ($this->request->method() == HTTP_Request::POST) { $user = $token->user; try { $user->update_user($this->request->post(), array('password')); // Delete the token. $token->delete(); // Confirm and redirect the user. Hint::success('Password changed, please login.'); $this->redirect(Route::get('user.login')->uri()); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view = new View_User_Reset_Enter(); }
public function action_delete() { $post = $this->post; if (!$this->user->can('Forum_Post_Delete', array('post' => $post))) { throw HTTP_Exception::factory('403', 'Permission denied to delete post'); } if ($this->request->method() == HTTP_Request::POST) { $topic_redirect = Route::get('forum.topic')->uri(array('id' => $post->topic->id)); try { // First post? delete the topic. if ($post->id == $post->topic->posts->limit(1)->find()->id) { $topic_redirect = Route::get('forum.category')->uri(array('id' => $post->topic->category)); $post->topic->delete(); } $post->delete(); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } Hint::success('Deleted post'); $this->redirect($topic_redirect); } $this->view = new View_Forum_Post_Delete(); $this->view->post = $post; }
public function action_index() { $this->view = new View_Game_LuckyWheel(); $can_play = $this->can_play(); $this->view->can_play = $can_play; $this->view->has_price = $this->user->get_property('points') >= $this->price; if ($this->request->method() == HTTP_Request::POST) { try { $post = $this->request->post(); if (isset($post['collect']) and $this->game->winnings) { $this->game->collect_winnings(FALSE); Hint::success('You have collected your winnings'); $this->redirect(Route::url('games.lucky-wheel')); } if ($can_play) { $play = $this->play($this->game); $this->view->play = $play; } } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view->game = $this->game; }
public function action_index() { $this->view = new View_Game_RockPaperScissors(); $can_play = $this->can_play(); $this->view->can_play = $can_play; if ($this->request->method() == HTTP_Request::POST and $can_play) { try { $post = $this->request->post(); if (isset($post['collect']) and $this->game->winnings) { $this->game->collect_winnings(TRUE); Hint::success('You have collected your winnings'); $this->redirect(Route::url('games.rock-paper-scissors')); } $validation = Validation::factory($post)->rule('move', 'not_empty')->rule('move', 'in_array', array(':value', array('rock', 'paper', 'scissors'))); if ($validation->check()) { $play = $this->play($post['move'], $this->game); $this->view->play = $play; } } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } } $this->view->game = $this->game; }
public function action_consume() { $item = ORM::factory('User_Item', $this->request->param('id')); $action = $this->request->post('action'); $errors = array(); if (!$item->loaded()) { Hint::error('You can\'t use an item that does not exist'); } elseif ($item->user_id != $this->user->id) { Hint::error('You can\'t access another player\'s item'); } elseif ($item->location != 'inventory') { Hint::error('The item you want to view is not located in your inventory'); } elseif ($action == NULL) { Hint::error('No action to perform has been specified'); } else { $def_cmd = Item_Command::factory($item->item->type->default_command); if (Valid::digit($action)) { // we'll want to perform an action on a pet $pet = ORM::factory('User_Pet', $action); if (!$pet->loaded()) { Hint::error('No existing pet has been specified'); } elseif ($pet->user_id != $this->user->id) { Hint::error('You can\'t let a pet comsume this item if it\'s not yours'); } elseif ($def_cmd->pets_required() == FALSE) { Hint::error('can\'t perform this item action on a pet'); } else { $commands = $item->item->commands; $results = array(); $db = Database::instance(); $db->begin(); $error = FALSE; foreach ($commands as $command) { $cmd = Item_Command::factory($command['name']); $res = $cmd->perform($item, $command['param'], $pet); if ($res == FALSE) { // the command couldn't be performed, spit out error, rollback changes and break the loop Hint::error(__(':item_name could not be used on :pet_name', array(':item_name' => $item->item->name, ':pet_name' => $pet->name))); $error = TRUE; $db->rollback(); break; } else { $results[] = $res; } } if ($error == FALSE) { $log = Journal::log('consume', 'item', ':item_name consumed', array(':item_name' => $item->item->name)); $log->notify('consume' . $item->item_id, 'item', ':item_name consumed'); if ($def_cmd->delete_after_consume == TRUE) { $item->amount('-', 1); } $db->commit(); } } } else { $results = array(); switch ($action) { case 'consume': $commands = $item->item->commands; $results = array(); $db = Database::instance(); $db->begin(); $error = FALSE; foreach ($commands as $command) { $cmd = Item_Command::factory($command['name']); $res = $cmd->perform($item, $command['param']); if ($res == FALSE) { // the command couldn't be performed, spit out error, rollback changes and break the loop Hint::error(__(':item_name could not be used', array(':item_name' => $item->name))); $db->rollback(); $error = TRUE; break; } else { $results[] = $res; } } if ($error = FALSE) { Journal::log('consume' . $item->item_id, 'item', ':item_name consumed', array(':item_name' => $item->item->name)); if ($def_cmd->delete_after_consume == TRUE) { $item->amount('-', 1); } $db->commit(); } break; case 'remove': // takes an amount $amount = $this->request->post('amount'); if ($amount == NULL) { $amount = 1; } if (!Valid::digit($amount)) { Hint::error('The amount you submitted isn\'t a number.'); } elseif ($amount <= 0 or $amount > $item->amount) { Hint::error('You only have ' . $item->name() . ', not ' . $amount); } else { if ($amount > 1) { $name = Inflector::plural($item->name(), $amount); $verb = 'were'; } else { $name = $item->item->name(1); $verb = 'was'; } $item->amount('-', $amount); Journal::log('remove.' . $item->item_id, 'item', ':item_name removed', array(':item_name' => $name)); $results = __(':item :verb deleted successfully', array(':verb' => $verb, ':item' => $name)); } break; case 'gift': // takes a username $username = $this->request->post('username'); if ($this->user->username == $username) { Hint::error('You can\'t send a gift to yourself'); } else { $user = ORM::factory('User')->where('username', '=', $username)->find(); if ($user->loaded()) { $log = $item->transfer($user); $log->notify($user, 'items.gift', array(':item_name' => $item->item->name(1))); $results = __('You\'ve successfully sent :item to :username', array(':item' => $item->item->name, ':username' => $user->username)); } else { Hint::error(__('Couldn\'t find a user named ":username"', array(':username' => $username))); } } break; default: // Moving items can take an amount if (substr($action, 0, 5) == 'move_') { $location = substr($action, 5); $cmd = Item_Command::factory('Move_' . ucfirst($location)); $amount = $this->request->post('amount'); if ($amount == NULL) { $amount = 1; } if (!Valid::digit($amount)) { Hint::error('The amount you submitted isn\'t a number.'); } elseif ($amount <= 0 or $amount > $item->amount) { Hint::error('You only have ' . $item->name() . ', not ' . $amount); } else { $results = $cmd->perform($item, $amount); } } else { Hint::error('The action you want to perform with this item does not exist'); } break; } } } $show = Kohana::$config->load('items.inventory.consume_show_results'); $output = array(); if (!is_array($results)) { $output[] = $results; } elseif ($show == 'first') { $output[] = $results[0]; } elseif (!empty($results)) { foreach ($results as $result) { $output[] = $result; } } if ($this->request->is_ajax()) { $return = array(); $return = Hint::dump(); Hint::ajax_dump(); if ($return['status'] == 'success') { $amount = $item->loaded() ? $item->name() : 0; $return = array_merge($return, array('result' => $output, 'new_amount' => $amount)); } $this->response->headers('Content-Type', 'application/json'); return $this->response->body(json_encode($return)); } if (count($output) > 0) { foreach ($output as $result) { Hint::success($result); } } $this->redirect(Route::get('item.inventory')->uri()); }
public function action_lock() { if (!$this->user->can('Forum_Topic_Lock', array('topic' => $this->topic))) { throw HTTP_Exception::factory('403', 'Permission denied to lock topic'); } try { if ($this->topic->locked) { $this->topic->locked = 0; Hint::success('You have unlocked the topic!'); } else { $this->topic->locked = time(); Hint::success('You have locked the topic!'); } $this->topic->save(); $this->redirect(Route::get('forum.topic')->uri(array('id' => $this->topic->id))); } catch (ORM_Validation_Exception $e) { Hint::error($e->errors('models')); } }
public function action_buy() { $shop = ORM::factory('User_Shop', $this->request->param('id')); // if no shop's found redirect to previous page if (!$shop->loaded()) { $this->redirect($this->request->referrer()); } if ($this->request->method() == HTTP_Request::POST) { $item_id = $this->request->post('item_id'); $item = ORM::factory('User_Item', $item_id); if (!$item->loaded() or $item->location != 'shop') { Hint::error('This item is not in stock'); } elseif ($this->user->id == $item->user->id) { Hint::error('You cannot buy items from your own shop.'); } elseif ($this->user->get_property('points') < $item->parameter) { Hint::error(__('You don\'t have enough :currency to buy a ":item_name"', array(':item_name' => $item->item->name))); } else { $this->user->set_property('points', $this->user->get_property('points') - $item->parameter); $this->user->save(); // log this action $log = Journal::log('user_shop.' . $shop->id, 'item', ':username bought 1 :item_name for :price', array('item_name' => $item->item->name, 'username' => $this->user->username, 'price' => $item->parameter)); $shop->till += $item->parameter; $shop->save(); $item->transfer($this->user); $log->notify($shop->user, 'user_shop.buy'); Hint::success(__('You\'ve successfully bought :item_name from :shop_owner for :price.', array(':shop_owner' => $shop->user->username, ':item_name' => $item->item->name('1'), ':price' => $item->parameter))); } } $this->redirect(Route::get('item.user_shop.view')->uri(array('id' => $shop->id))); }
public function action_complete() { $item = ORM::factory('User_Item', $this->request->param('id')); $action = $this->request->post('action'); $errors = array(); if (!$item->loaded()) { $errors[] = 'You can\'t use a recipe that does not exist'; } elseif ($item->user_id != $this->user->id) { $errors[] = 'You can\'t access another player\'s recipe'; } elseif ($item->location != 'cookbook') { $errors[] = 'The recipe you want to view is not located in your cookbook'; } elseif ($item->item->type->default_command != 'General_Cook') { $errors[] = 'You can\'t use this item as a recipe.'; } elseif ($action == NULL) { $errors[] = 'No action to perform has been specified'; } else { $recipe = ORM::factory('Item_Recipe')->where('item_recipe.name', '=', $item->item->commands[0]['param'])->find(); $coll = $recipe->materials->find_all(); $materials = 0; $db = Database::instance(); $db->begin(); foreach ($coll as $material) { $user_item = Item::factory($material->item)->user_has('inventory'); if ($user_item != FALSE and $user_item->amount >= $material->amount) { $user_item->amount('-', $material->amount); $materials++; } } if ($materials == count($coll)) { Item::factory($recipe->item)->to_user($this->user); $item->amount('-', 1); $db->commit(); $result = 'You\'ve successfully made ' . $recipe->item->name; } else { $db->rollback(); $errors[] = 'You don\'t have all the required ingredients for this recipe.'; } } if ($this->request->is_ajax()) { if (count($errors) > 0) { $return = array('status' => 'error', 'errors' => $errors); } else { $return = array('status' => 'success', 'result' => $result, 'item' => $item->amount); } $this->response->headers('Content-Type', 'application/json'); return $this->response->body(json_encode($return)); } elseif (count($errors) > 0) { Hint::error($errors[0]); $this->redirect(Route::get('item.cookbook')->uri()); } else { Hint::success($result); $this->redirect(Route::get('item.cookbook')->uri()); } }