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; }
/** * Create new model * * @param Jelly_Meta $meta */ public static function initialize(Jelly_Meta $meta) { $meta->fields(array('id' => new Field_Primary(), 'token' => new Field_String(array('unique' => true, 'rules' => array('max_length' => array(32)))), 'user' => new Field_BelongsTo(), 'user_agent' => new Field_String(), 'created' => new Field_Timestamp(array('auto_now_create' => true)), 'expires' => new Field_Timestamp())); // Garbace collection if (mt_rand(1, 100) === 1) { Jelly::delete('user_token')->where('expires', '<', time())->execute(); } }
/** * Implementation for Jelly_Field_Behavior_Saveable. * * @param Jelly $model * @param mixed $value * @return void */ public function save($model, $value, $loaded) { // Find all current records so that we can calculate what's changed $in = $loaded ? $this->_in($model, true) : array(); // Find old relationships that must be deleted if ($old = array_diff($in, (array) $value)) { Jelly::delete($this->through['model'])->where($this->through['columns'][0], '=', $model->id())->where($this->through['columns'][1], 'IN', $old)->execute(Jelly::meta($model)->db()); } // Find new relationships that must be inserted if (!empty($value) && ($new = array_diff((array) $value, $in))) { foreach ($new as $new_id) { if (!is_null($new_id)) { Jelly::insert($this->through['model'])->columns($this->through['columns'])->values(array($model->id(), $new_id))->execute(Jelly::meta($model)->db()); } } } }
/** * Implementation for Jelly_Field_Behavior_Saveable. * * @param Jelly $model * @param mixed $value * @return void */ public function save($model, $value, $loaded) { // Find all current records so that we can calculate what's changed $in = $loaded ? $this->_in($model, TRUE) : array(); // Find old relationships that must be deleted if ($old = array_diff($in, (array) $value)) { Jelly::delete($this->through['model'])->where($this->through['columns'][0], '=', $model->id())->where($this->through['columns'][1], 'IN', $old)->execute(Jelly::meta($model)->db()); } // Find new relationships that must be inserted if ($new = array_diff((array) $value, $in)) { foreach ($new as $new_id) { // Not sure why an array with NULL as the first value was coming through when you didn't submit anything // on the manytomany relationship but this avoids the problem for now... if ($new_id == NULL) { continue; } /* if (class_exists(Jelly::class_name($this->through['model']))) { Jelly::factory( $this->through['model'], array( $this->through['columns'][0] => $model->id(), $this->through['columns'][1] => $new_id ) )->save(); } else { */ Jelly::insert($this->through['model'])->columns($this->through['columns'])->values(array($model->id(), $new_id))->execute(Jelly::meta($model)->db()); //} } } }
/** * Deletes a single record. * * @param $key A key to use for non-loaded records * @return boolean **/ public function delete($key = NULL) { $result = FALSE; // Are we loaded? Then we're just deleting this record if ($this->_loaded or $key) { if ($this->_loaded) { $key = $this->id(); } $result = Jelly::delete($this)->where(':unique_key', '=', $key)->execute(); } // Clear the object so it appears deleted anyway $this->clear(); return (bool) $result; }
/** * Deletes a single record. * * @param $key A key to use for non-loaded records * @return boolean **/ public function delete($key = NULL) { $result = FALSE; // Are we loaded? Then we're just deleting this record if ($this->_loaded or $key) { if ($this->_loaded) { $key = $this->id(); } // Call delete on each individual field so they can clean up after themselves foreach ($this->_meta->fields() as $field) { $field->delete($this); } $result = Jelly::delete($this)->where(':unique_key', '=', $key)->execute(); } // Clear the object so it appears deleted anyway $this->clear(); return (bool) $result; }
/** * Log a user out and remove any auto-login Cookies. * * @param boolean completely destroy the session * @param boolean remove all tokens for user * @return boolean */ public function logout($destroy = FALSE, $logout_all = FALSE) { if ($token = Cookie::get('authautologin')) { // Delete the autologin Cookie to prevent re-login Cookie::delete('authautologin'); // Clear the autologin token from the database $token = Jelly::select('user_token')->where('token', '=', $token)->limit(1)->load(); if ($token->loaded() and $logout_all) { Jelly::delete('user_token')->where('user_id', '=', $token->user->id)->execute(); } elseif ($token->loaded()) { $token->delete(); } } return parent::logout($destroy); }
/** * Log out a user by removing the related session variables. * * @param boolean $destroy Completely destroy the session * @param boolean $logout_all Logout all browsers * @return boolean */ public function logout($destroy = false, $logout_all = false) { // Delete the autologin cookie to prevent re-login if ($token = Cookie::get($this->_config['cookie_name'])) { Cookie::delete($this->_config['cookie_name']); $token = Jelly::select('user_token')->where('token', '=', $token)->limit(1)->load(); if ($token->loaded() && $logout_all) { Jelly::delete('user_token')->where('user_id', '=', $token->user->id)->execute(); } else { if ($token->loaded()) { $token->delete(); } } } // Logout 3rd party? /* if (FB::enabled() && Visitor::instance()->get_provider()) { $this->session->delete($this->config['session_key'] . '_provider'); try { FB::instance()->expire_session(); } catch (Exception $e) { } } */ // Destroy the session completely? if ($destroy === true) { $this->_session->destroy(); } else { // Remove the user from the session $this->_session->delete($this->_config['session_key']); // Regenerate session_id $this->_session->regenerate(); } // Double check return !$this->logged_in(); }