/** * Uploads a file if we have a valid upload * * @param Jelly $model * @param mixed $value * @param bool $loaded * @return string|NULL */ public function save($model, $value, $loaded) { $original = $model->get($this->name, FALSE); // Upload a file? if (is_array($value) and upload::valid($value)) { if (FALSE !== ($filename = upload::save($value, NULL, $this->path))) { // Chop off the original path $value = str_replace($this->path, '', $filename); // Ensure we have no leading slash if (is_string($value)) { $value = trim($value, '/'); } // Delete the old file if we need to if ($this->delete_old_file and $original != $this->default) { $path = $this->path . $original; if (file_exists($path)) { unlink($path); } } } else { $value = $this->default; } } return $value; }
/** * Tests for Jelly_Field_BelongsTo::get() * * @dataProvider provider_get * @param Jelly $builder * @param bool $loaded * @return void */ public function test_get($builder, $loaded) { $this->assertTrue($builder instanceof Jelly_Builder); // Load the model $model = $builder->select(); // Ensure it's loaded if it should be $this->assertSame($loaded, $model->loaded()); }
/** * Implementation of Jelly_Field_Behavior_Saveable * * @param Jelly $model * @param mixed $value * @return void */ public function save($model, $value, $loaded) { // Empty relations to the default value Jelly::update($this->foreign['model'])->where($this->foreign['column'], '=', $model->id())->set(array($this->foreign['column'] => $this->default))->execute(); // Set the new relations if (!empty($value)) { // Update the ones in our list Jelly::update($this->foreign['model'])->where(':primary_key', '=', $value)->set(array($this->foreign['column'] => $model->id()))->execute(); } }
/** * Construct controller */ public function before() { // Log request Jelly::factory('api_request')->set(array('ip' => Request::$client_ip, 'request' => $this->request->uri . (empty($_GET) ? '' : '?' . http_build_query($_GET))))->save(); // Rate limit $rate_span = Kohana::config('api.rate_span'); $rate_limit = Kohana::config('api.rate_limit'); $requests = Model_API_Request::request_count(time() - $rate_span, Request::$client_ip); $requests_left = $rate_limit - $requests; if ($requests_left < 0) { throw new Controller_API_Exception('Request limit reached'); } // Check version $this->version = $this->request->param('version'); if (!in_array($this->version, self::$_versions)) { throw new Controller_API_Exception('Invalid version'); } // Check format $this->format = $this->request->param('format'); !$this->format and $this->format = self::FORMAT_JSON; if (!in_array($this->format, self::$_formats)) { throw new Controller_API_Exception('Invalid format'); } // Set result defaults $this->data = array('version' => $this->version, 'requests' => $requests, 'requests_left' => $requests_left, 'request_window' => $rate_span); return parent::before(); }
public function action_delete($id) { $post = Jelly::select('forum_post')->where('id', '=', $id)->load(); if ($post->loaded()) { $this->title = 'Forum - Post - Delete'; } else { Message::set(Message::ERROR, 'Post does not exist'); $this->request->redirect('forum'); } if ($this->user->id != $post->user->id) { Message::set(Message::ERROR, 'You are not the author of this post.'); $this->request->redirect('forum'); } else { $topic = Jelly::select('forum_topic')->where('id', '=', $post->topic->id)->load(); if ($topic->posts > 1) { $topic->posts = $topic->posts - 1; $topic->save(); $post->delete(); Message::set(Message::SUCCESS, 'Post has been deleted.'); $this->request->redirect('forum'); } if ($topic->posts == 1) { $topic->delete(); $post->delete(); Message::set(Message::SUCCESS, 'Post has been deleted.'); $this->request->redirect('forum'); } } $this->template->content = View::factory('forum/post/delete')->set('post', $post); }
/** * Create a new post. */ public function action_reply($id) { $topic = Jelly::select('forum_topic')->where('id', '=', $id)->load(); // Make sure the topic exists if (!$topic->loaded()) { Message::set(Message::ERROR, 'Topic does not exist'); $this->request->redirect('forum'); } $this->title = 'Forum - Reply to ' . $topic->title; // Validate the form input $post = Validate::factory($_POST)->filter(TRUE, 'trim')->filter(TRUE, 'htmlspecialchars', array(ENT_QUOTES))->rule('title', 'not_empty')->rule('title', 'min_length', array(3))->rule('title', 'max_length', array(20))->rule('content', 'not_empty')->rule('content', 'min_length', array(5))->rule('content', 'max_length', array(1000)); if ($post->check()) { $values = array('title' => $post['title'], 'content' => $post['content'], 'user' => $this->user->id, 'topic' => $id); $message = Jelly::factory('forum_post'); // Assign the validated data to the Jelly object $message->set($values); $message->save(); $topic_id = $id; $topic = Jelly::select('forum_topic')->where('id', '=', $topic_id)->load(); $topic->posts = $topic->posts + 1; $topic->save(); Message::set(Message::SUCCESS, 'You posted a new reply.'); $this->request->redirect('forum/topic/' . $id); } else { $this->errors = $post->errors('forum'); } if (!empty($this->errors)) { Message::set(Message::ERROR, $this->errors); } $this->template->content = View::factory('forum/post/create')->set('post', $post->as_array()); }
public static function initialize(Jelly_Meta $meta) { // Set database to connect to $meta->db(Unittest_Jelly_Testcase::$database_connection); // All fields are aliased to different columns $meta->fields(array('id' => Jelly::field('primary', array('column' => 'id-alias')), 'name' => Jelly::field('string', array('column' => 'name-alias')), 'description' => Jelly::field('string', array('column' => 'description-alias')), '_id' => 'id', '_name' => 'name', '_description' => 'description', '_bar' => 'foo')); }
public function before() { parent::before(); // Save the old action so it can be brought back on after $this->_action = $this->request->action; // Set the current action $current_action = $this->request->action; $id = $this->request->param('id', NULL); // Let's guess the action based on the params if (!in_array($this->request->action, array('edit', 'add', 'delete')) and (!is_null($id) or !empty($id))) { $current_action = 'read'; } if (!method_exists($this, 'action_' . $this->request->action)) { $model = Jelly::select(Inflector::singular($this->request->controller)); foreach ($model->get_state() as $key => $value) { $param = $this->request->param($key, NULL); if (!is_null($param)) { $model->set_state($key, $param); } } $this->request->response = Kostache::factory($this->request->controller . '/' . $current_action)->set_model($model); // Since the magic has been executed, just execute an empty action $this->request->action = 'default'; } }
public function action_update() { $this->content->bind('form', $supply); $this->content->bind('errors', $errors); $id = $this->request->param('id'); $supply = Jelly::select('supply', $id); $supply->set('product', $supply->product); // bez tego nie dziala, lol if (!$supply->loaded()) { $this->request->redirect($this->_base); } if ($_POST and !$this->session->get($_POST['seed'], FALSE)) { if (in_array($supply->status, array('done'))) { unset($_POST['status']); } if (!in_array($supply->status, array('added'))) { unset($_POST['quantity'], $_POST['supplier']); } try { $supply->set($_POST); $supply->save(); $this->session->set($_POST['seed'], TRUE); // 'seed' jest zintegrowany w formularz $this->request->redirect($this->_base); } catch (Validate_Exception $e) { $errors = $e->errors(); } } }
public static function initialize(Jelly_Meta $meta) { // Set database to connect to $meta->db(Unittest_Jelly_Testcase::$database_connection); // Define fields $meta->fields(array('id' => Jelly::field('primary'), 'name' => Jelly::field('string'))); }
public function set($values, $value = NULL) { // Accept set($_POST, array('author', 'body')); // Only $_POST['author'] and $_POST['body'] will be passed if (is_array($values) and is_array($value)) { $keys = array_flip($value); $values = array_intersect_key($values, $keys); $value = NULL; } parent::set($values, $value); if (!empty($this->_unmapped)) { $related = array(); foreach ($this->_meta->fields() as $k => $v) { if ($v instanceof Jelly_Field_Relationship) { $related[$k] = Jelly::meta($v->foreign['model']); } } if (!empty($related)) { foreach ($this->_unmapped as $k => $v) { foreach ($related as $key => $r) { if ($r->fields($k) !== NULL and $r instanceof Model) { $this->{$key}->set($k, $v); continue; } } } } } return $this; }
public function action_register() { if ($this->user) { Request::instance()->redirect(''); } // Experimental facebook connection $this->facebook = new Fb(); // User accessed from facebook! if ($this->facebook->validate_fb_params()) { $this->facebook->require_frame(); $_SESSION['fb_uid'] = $this->facebook->require_login(); } elseif (!isset($_SESSION['fb_uid'])) { Request::instance()->redirect(''); } // Check if the user got an account. $user_facebook = Jelly::select('user_facebook')->where('facebook_id', '=', $_SESSION['fb_uid'])->load(); // If we found it, log him in. if ($user_facebook->loaded()) { $this->a1->force_login($user_facebook->user->username); $_SESSION['facebook'] = 'TRUE'; // Used for verifying if logged in using facebook. Request::instance()->redirect(''); } $user = Jelly::factory('user'); // Validate the form input $post = Validate::factory($_POST)->filter(TRUE, 'trim')->rule('username', 'not_empty')->rule('username', 'min_length', array(3))->rule('username', 'max_length', array(20))->rule('username', 'alpha_numeric')->rule('email', 'email')->rule('tos', 'not_empty'); if ($post->check()) { $values = array('username' => $post['username'], 'email' => $post['email']); // Assign the validated data to the sprig object $user->set($values); // Hash the password $user->password = ''; // Set the default role for registered user. $user->role = 'facebook'; try { // Create the new user $testy = $user->save(); //print_r($testy); $user_id = mysql_insert_id(); $ufb = Jelly::factory('user_facebook'); $ufb->facebook_id = $_SESSION['fb_uid']; $ufb->user = $user_id; $ufb->save(); $this->a1->force_login($values['username']); $_SESSION['facebook'] = 'TRUE'; // Used for verifying if logged in using facebook. // Redirect the user to the login page $this->request->redirect(''); } catch (Validate_Exception $e) { // Get the errors using the Validate::errors() method $this->errors = $e->array->errors('register'); } } else { $this->errors = $post->errors('account/register'); } if (!empty($this->errors)) { Message::set(Message::ERROR, $this->errors); } $this->template->content = View::factory('facebook/register')->set('post', $post->as_array()); }
public function action_all(Params $param) { $this->content->bind('projects', $projects); $projects = Jelly::select('project'); $this->template->title = __('My works'); $this->template->active = array('portfolio' => ' class="active"'); }
public function input($prefix = 'jelly/field', $data = array()) { if (!isset($data['options'])) { $data['options'] = Jelly::select($this->foreign['model'])->join('products_suppliers', 'LEFT')->on('products_suppliers.supplier_id', '=', $this->foreign['model'] . ':primary_key')->where('products_suppliers.product_id', '=', $this->product->id)->execute()->as_array($this->foreign['model'] . ':primary_key', $this->foreign['model'] . ':name_key'); } return parent::input($prefix, $data); }
public function action_register() { // There are no errors by default $errors = FALSE; // Create an instance of Model_Auth_User $user = Jelly::factory('user'); // Check if the form was submitted if ($_POST) { /** * Load the $_POST values into our model. * * We use Arr::extract() and specify the fields to add * by hand so that a malicious user can't do (for example) * `$_POST['roles'][] = 2;` and make themselves an administrator. */ $user->set(Arr::extract($_POST, array('email', 'username', 'password', 'password_confirm'))); // Add the 'login' role to the user model $user->add('roles', 1); try { // Try to save our user model $user->save(); // Redirect to the index page $this->request->redirect(Route::get('default')->uri(array('action' => 'index'))); } catch (Validate_Exception $e) { // Load custom error messages from `messages/forms/user/register.php` $errors = $e->array->errors('forms/user/register'); } } // Set template title $this->template->title = 'Register'; // Display the 'register' template $this->template->content = View::factory('user/register')->set('user', $user)->set('errors', $errors); }
/** * Displays a selection of models to relate to * * @param string $prefix The prefix to put before the filename to be rendered * @return View **/ public function input($prefix = 'jelly/field', $data = array()) { if (!isset($data['options'])) { $data['options'] = Jelly::select($this->foreign['model'])->execute()->as_array(':primary_key', ':name_key'); } return parent::input($prefix, $data); }
public static function initialize(Jelly_Meta $meta) { // Set database to connect to $meta->db(Unittest_Jelly_Testcase::$database_connection); // Define fields $meta->fields(array('id' => Jelly::field('primary'), 'name' => Jelly::field('string'), 'test_posts' => Jelly::field('manytomany'), 'parent' => Jelly::field('belongsto', array('foreign' => 'test_category', 'column' => 'parent_id')))); }
protected function submit($task, $id, $type) { $item = Jelly::select($type, $id); $redirect = HTML::uri(array('action' => Inflector::plural($type))); switch ($task) { case 'apply': $item->set($_POST)->save(); $redirect = HTML::uri(array('action' => $type, 'task' => 'edit', 'id' => $item->id)); $this->request->redirect($redirect); break; case 'save': $item->set($_POST)->save(); $this->request->redirect($redirect); break; case 'cancel': $this->request->redirect($redirect); break; case 'delete': if ($ids = Arr::get($_POST, 'cid', NULL)) { $items = Jelly::delete($item)->where(':primary_key', 'IN', $ids)->execute(); } $this->request->redirect($redirect); break; case 'default': if (!$item->loaded()) { $this->request->redirect($redirect); } break; } return $item; }
/** * Moves the character to a new zone * * @param integer $id */ public function action_travel($id) { // Make sure id is an integer. if (!is_numeric($id)) { Message::set(Message::ERROR, 'Invalid ID'); $this->request->redirect('travel'); } if ($id == $this->character->zone->id) { Message::set(Message::ERROR, 'You cannot move to where you already are.'); $this->request->redirect('travel'); } // Load the zone $zone = Jelly::select('zone')->where('id', '=', $id)->load(); $character = $this->character; // Make sure the character got enough of engery if ($character->energy < $zone->energy) { Message::set(Message::ERROR, 'Not enough energy.'); $this->request->redirect('travel'); } // Set the new zone, and energy $character->zone = $zone->id; $character->energy = $character->energy - $zone->energy; $character->save(); $this->request->redirect('character'); }
public static function initialize(Jelly_Meta $meta) { // Set database to connect to $meta->db(Unittest_Jelly_Testcase::$database_connection); // Define fields $meta->fields(array('id' => Jelly::field('primary'), 'name' => Jelly::field('string'), 'password' => Jelly::field('password'), 'email' => Jelly::field('email'), 'test_posts' => Jelly::field('hasmany'), 'test_post' => Jelly::field('hasone'), 'permission' => Jelly::field('belongsto', array('foreign' => 'test_role', 'column' => 'test_role_id')), '_id' => 'id')); }
/** * Create a new topic. */ public function action_new_topic($id) { $this->title = 'Forum - New Topic'; $category = Jelly::select('forum_category')->where('id', '=', $id)->load(); if (!$category->loaded()) { Message::set(Message::ERROR, 'Category does not exist'); $this->request->redirect('forum'); } // Validate the form input $post = Validate::factory($_POST)->filter(TRUE, 'trim')->filter(TRUE, 'htmlspecialchars', array(ENT_QUOTES))->rule('title', 'not_empty')->rule('title', 'min_length', array(3))->rule('title', 'max_length', array(20))->rule('content', 'not_empty')->rule('content', 'min_length', array(5))->rule('content', 'max_length', array(1000)); if ($post->check()) { $topic_values = array('title' => $post['title'], 'user' => $this->user->id, 'category' => $id, 'status' => 'open', 'posts' => '1'); $topic = Jelly::factory('forum_topic'); // Assign the validated data to the sprig object $topic->set($topic_values); $topic->save(); $topic_id = $topic->id; $post_values = array('title' => $post['title'], 'content' => $post['content'], 'user' => $this->user->id, 'topic' => $topic_id); $message = Jelly::factory('forum_post'); // Assign the validated data to the sprig object $message->set($post_values); $message->save(); Message::set(Message::SUCCESS, 'You created a topic.'); $this->request->redirect('forum/category/' . $id); } else { $this->errors = $post->errors('forum'); } if (!empty($this->errors)) { Message::set(Message::ERROR, $this->errors); } $this->template->content = View::factory('forum/topic/create')->set('post', $post->as_array()); }
/** * Verify the Flickr credentials. * * @throws Kohana_Exception * @return boolean */ public function verify() { // Set the service $service = $this->_service; if (empty($service)) { MMI_Log::log_error(__METHOD__, __LINE__, 'Service not set'); throw new Kohana_Exception('Service not set in :method.', array(':method' => __METHOD__)); } // Ensure the frob is set $frob = NULL; if (array_key_exists('frob', $_GET)) { $frob = urldecode(Security::xss_clean($_GET['frob'])); } if (empty($frob)) { MMI_Log::log_error(__METHOD__, __LINE__, 'Frob parameter missing'); throw new Kohana_Exception('Frob parameter missing in :method.', array(':method' => __METHOD__)); } // Load existing data from the database $auth_config = $this->_auth_config; $username = Arr::get($auth_config, 'username'); $model; if (!empty($username)) { $model = Model_MMI_API_Tokens::select_by_service_and_username($service, $username, FALSE); } else { $model = Jelly::factory('MMI_API_Tokens'); } $success = FALSE; if ($model->loaded()) { // Check if the credentials were previously verified $previously_verified = $model->verified; if ($previously_verified) { $success = TRUE; } else { // Create a dummy verification code $verification_code = $service . '-' . time(); } // Do database update if (!$previously_verified) { // Get an access token $svc = MMI_API::factory($service); $token = $svc->get_access_token($verification_code, array('token_key' => $frob, 'token_secret' => $service . '-' . time())); // Update the token credentials in the database if (isset($token) and $svc->is_valid_token($token)) { $model->token_key = $token->key; $model->token_secret = Encrypt::instance()->encode($token->secret); $model->verified = 1; $model->verification_code = $verification_code; if (!empty($token->attributes)) { $model->attributes = $token->attributes; } $success = MMI_Jelly::save($model, $errors); if (!$success and $this->_debug) { MMI_Debug::dead($errors); } } } } return $success; }
public function action_index() { // Initialize the character class, and set the players character as the default. $char = new Character($this->character); // Load the users history, limit with 10 $history = Jelly::select('user_history')->where(':primary_key', '=', $this->user->id)->limit(10)->execute(); $this->template->content = View::factory('dashboard/index')->set('character', $this->character)->set('char', $char)->set('history', $history); }
/** * Casts the returned value to a given type. * * @param mixed $value * @return mixed */ public function set($value) { if (isset($this->cast)) { // Cast the value using the field type defined in 'cast' $value = Jelly::field($this->cast)->set($value); } return $value; }
public function action_category() { $id = $this->request->param('id'); $page = $this->request->param('page') - 1; $this->view->title = Jelly::select('category', $id)->title; $this->content->products = Jelly::select('product')->belongs_to_category($id)->page($page)->execute(); $this->content->pagination = new Pagination(array('total_items' => Jelly::select('product')->belongs_to_category($id)->count())); }
/** * Loads the user object from database using username * * @param string username * @return object User Object */ protected function _load_user($username) { $query = Jelly::select($this->_config['user_model'])->where($this->_config['columns']['username'], '=', $username); if (isset($this->_config['columns']['active'])) { $query = $query->where($this->_config['columns']['active'], '=', TRUE); } return $query->limit(1)->execute(); }
/** * Tests that models in a collection aren't returned as references to the original * object but as a distinct model. */ public function testIssue87() { $last = NULL; foreach (Jelly::select('post')->execute() as $post) { $this->assertNotEquals($post, $last); $last = $post; } }
/** * Provider for test type */ public function provider_construction() { // Set database connection name $db = parent::$database_connection; // Set result $result = DB::select()->from('test_posts'); return array(array(new Jelly_Collection($result->execute($db), 'Model_Test_Post'), 'Model_Test_Post'), array(new Jelly_Collection($result->execute($db), Jelly::factory('test_post')), 'Model_Test_Post'), array(new Jelly_Collection($result->execute($db)), FALSE), array(new Jelly_Collection($result->as_object()->execute($db)), 'stdClass'), array(new Jelly_Collection($result->execute($db), 'Model_Test_Post'), 'Model_Test_Post')); }
public function action_edit() { $this->require_login(); $this->init_template('Update profile'); $model = $this->auth->get_user(); $model = Jelly::select('kadmium_user', $model->id()); $this->show_edit_page_from_model('Profile', $model, false); }
public function action_index() { // Get the total number of users. $users = Jelly::select('user')->count(); // Get the number of active users. $active_users = Jelly::select('user')->where('last_login', '>', time() - $this->active_time)->count(); $this->template->content = View::factory('admin/index')->set('users', $users)->set('active_users', $active_users); }