/** * Override __set() to handle datetime. * * @param string $key * @param mixed $value */ public function __set($key, $value) { if ($key == 'stamp_begin' && !is_numeric($value)) { $value = strtotime(is_array($value) ? $value['date'] . ' ' . $value['time'] : $value); } parent::__set($key, $value); }
/** * Checks if a session is active. * * @param mixed role name string, role ORM object, or array with role names * @return boolean */ public function logged_in($role = NULL) { $status = FALSE; // Get the user from the session $user = $this->get_user(); if (is_object($user) and $user instanceof Model_User and $user->id) { // Everything is okay so far $status = TRUE; if (!empty($role)) { // Multiple roles to check if (is_array($role)) { // Check each role foreach ($role as $_role) { if (!is_numeric($_role)) { $_role = AutoModeler_ORM::factory('role')->load(db::select()->where('name', '=', $_role))->id; } // If the user doesn't have the role if (!$user->has('roles', $_role)) { // Set the status false and get outta here $status = FALSE; break; } } } else { if (!is_numeric($role)) { // Load the role $role = AutoModeler_ORM::factory('role')->load(db::select()->where('name', '=', $role))->id; } // Check that the user has the given role $status = $user->has('roles', $role); } } } return $status; }
/** * Load role * * @param integer|string $id */ public function __construct($id = null) { parent::__construct(); if ($id !== null) { $this->load(DB::select_array($this->fields())->where(is_numeric($id) ? 'id' : 'name', '=', $id)); } }
/** * Override __set() to handle time. * * @param string $key * @param mixed $value */ public function __set($key, $value) { if ($key === 'size_time' && !is_numeric($value)) { $value = Num::seconds($value); } parent::__set($key, $value); }
/** * Gets a list of roles for the user to select. This should go away once the * application is in a usable state. * * @return array */ public function roles() { $roles = array(); foreach (AutoModeler_ORM::factory('role')->fetch_all() as $role) { $roles[] = array('id' => $role->id, 'name' => $role->name); } return $roles; }
/** * Returns all the products for demo purposes right now * * @return array */ public function products() { $products = array(); foreach (AutoModeler_ORM::factory('vendo_product')->fetch_all() as $product) { $products[] = $product->as_array(); } return $products; }
/** * Variable method to fetch all the photos * * @return array */ public function photos() { $photos = array(); foreach (AutoModeler_ORM::factory('vendo_photo')->fetch_all() as $photo) { $photos[] = array('id' => $photo->id, 'filename' => $photo->filename, 'uri' => $photo->uri()); } return $photos; }
/** * Returns all the images to display in the template * * @return array */ public function photos() { $photos = array(); foreach (AutoModeler_ORM::factory('vendo_photo')->fetch_all() as $photo) { $photos[] = array('id' => $photo->id, 'filename' => $photo->filename, 'uri' => $photo->uri(), 'checked' => $this->product->has('vendo_photos', $photo->id), 'checked_primary' => $this->product->primary_photo_id == $photo->id); } return $photos; }
/** * Creates or updates the current exif data. * * @return boolean */ public function save($validation = null) { // If new EXIF data, try to read from image if (!$this->loaded()) { $this->read(); } // If was new and no exif data was found it will not be saved return parent::save(); }
/** * Returns a full tree of nested product categories starting at a category * * @param int $start the category to start at * @param bool $remove_this should this category be included? * @param object $product an optional product to compare checked against * * @return array */ public function full_tree($start = NULL, $remove_this = FALSE, $product = NULL) { $tree = array(); $compare_object = NULL == $product ? $this : $product; foreach (AutoModeler_ORM::factory('vendo_product_category')->fetch_where(array(array('parent_id', '=', $start))) as $category) { $sub_tree = $this->full_tree($category->id, $remove_this, $product); if (!$remove_this or !$this->id or $remove_this and $this->id != $category->id) { $tree[] = array('id' => $category->id, 'name' => $category->name, 'has_category' => $compare_object->has_category($category->id), 'has_children' => (bool) count($sub_tree), 'children' => $sub_tree); } } return $tree; }
/** * Constructor to load the object by an email address * * @param mixed $id the id to load by. A numerical ID or an email address * * @return null */ public function __construct($id = NULL) { if (!is_numeric($id) and NULL != $id) { // try and get a row with this ID $data = db::select_array(array_keys($this->_data))->from($this->_table_name)->where('email', '=', $id)->execute($this->_db); // try and assign the data if (count($data) == 1 and $data = $data->current()) { foreach ($data as $key => $value) { $this->_data[$key] = $value; } } } else { parent::__construct($id); } }
/** * Magic setter * * @param string $key * @param mixed $value */ public function __set($key, $value) { switch ($key) { // Legacy status <-> type case 'status': if ($value == self::STATUS_LOCKED && $this->type < 10) { $this->type += 10; } else { if ($value !== self::STATUS_LOCKED && $this->type >= 10) { $this->type -= 10; } } break; } parent::__set($key, $value); }
/** * Load token * * @param integer|string $id */ public function __construct($id = null) { parent::__construct(); if ($id !== null) { $this->load(DB::select_array($this->fields())->where(is_numeric($id) ? 'id' : 'token', '=', $id)); // Expired token? if ($this->loaded() && $this->expires < time()) { $this->delete(); } } // Garbace collection /* if (mt_rand(1, 100) === 1) { self::gc(); } */ }
/** * Override save() to set date_created if this is a new object * * @param object $validation a validation object to save with * * @return int */ public function save($validation = NULL) { if ($this->id) { throw new Kohana_Exception('Saved orders cannot be modified!'); } if (!$this->id) { $this->date_created = time(); } // Save the related items if this is a new item if (!$this->id and $status = parent::save($validation)) { foreach ($this->_order_products as $product) { $order_product = new Model_Order_Product(); $order_product->order_id = $this->id; $order_product->product_id = $product['product']->id; $order_product->quantity = $product['quantity']; $order_product->save(); } } return $status; }
/** * Helper method to determin if this product has a category * * @return bool */ public function has_category($category_id) { return AutoModeler_ORM::factory('vendo_product_category', $category_id)->has('vendo_products', $this->id); }
/** * Processes the shopping cart with the payment processor, saves the user's * cart, clears it and redirects to a success method. * * @return null */ public function action_process() { $order = Auth::instance()->get_user()->cart(); $user_post = arr::get($_POST, 'user', array()); $user = arr::get($_POST, 'user', Auth::instance()->get_user()); if (!is_object($user)) { $temp = $user; $user = AutoModeler_ORM::factory('user'); $user->set_fields($temp); } $address = AutoModeler_ORM::factory('vendo_address'); $address->set_fields(arr::get($_POST, 'address', array())); // Build the contact model $contact = new Model_Contact(); $contact->set_fields(array('email' => $user->email, 'first_name' => $user->first_name, 'last_name' => $user->last_name)); // Build the credit card model $credit_card_post = arr::get($_POST, 'payment'); $credit_card = new Model_Credit_Card(arr::get($credit_card_post, 'card_number'), arr::get($credit_card_post, 'months') . arr::get($credit_card_post, 'years'), arr::get($credit_card_post, 'card_code'), $contact, $address); $errors = array(); // Check for a new user registration, and make a user if so if ($this->should_create_account($user)) { $status = $this->process_new_account($user, $user_post, $address); if (!$status) { return; } $contact->save(); $order->user_id = $user->id; } else { $user_address = $user->address->as_array(); unset($user_address['id']); if ($user_address != $address and TRUE === $address->is_valid()) { $address->save(); $user->address_id = $address->id; } elseif (TRUE !== $address->is_valid()) { $errors += $address->errors('form_errors'); } else { $address = $user->address; } $contact->address_id = $address->id; if (TRUE !== $contact->is_valid()) { $errors += $contact->errors('form_errors'); } if (Auth::instance()->logged_in()) { $order->user_id = $user->id; } } // Verify the credit card is valid if (TRUE !== ($cc_errors = $credit_card->validate())) { $errors += $cc_errors; } if ($errors) { // If we've failed, and we aren't registering a new user, delete // the address if (!$user->id) { $address->delete(); } $this->request->response = new View_Checkout_Index(); $this->request->response->set(array('user' => $user->as_array(), 'address' => $address->as_array(), 'cart' => Auth::instance()->get_user()->cart(), 'credit_card' => $credit_card)); $errors = (string) View::factory('form_errors')->set(array('errors' => $errors)); $this->request->response->errors = $errors; return; } $order->credit_card = $credit_card; // Process the credit card try { $status = Payment::process($order); if (1 != $status->response_code) { throw new Payment_Exception('Problem processing your payment.'); } // Persist the order $contact->save(); $order->contact_id = $contact->id; $order->address_id = $address->id; $order->save(); Auth::instance()->get_user()->cart(new Model_Order()); // Show success message! $this->request->response = new View_Checkout_Process(); } catch (Payment_Exception $e) { // If we've failed, and we aren't registering a new user, delete // the address if (!$user->id) { $address->delete(); } $this->request->response = new View_Checkout_Index(); $this->request->response->set(array('user' => $user->as_array(), 'address' => $address->as_array(), 'cart' => Auth::instance()->get_user()->cart(), 'credit_card' => $credit_card)); $errors = (string) View::factory('form_errors')->set(array('errors' => array('general' => $e->getMessage()))); $this->request->response->errors = $errors; return; } }
/** * Creates or updates the current image. * * @param Validation $validation a manual validation object to combine the model properties with * @return integer * * @throws Kohana_Exception */ public function save(Validation $validation = null) { $new = !(bool) $this->id; // Validate new image if ($new) { $path = Kohana::$config->load('image.upload_path'); // Download remote files if ($this->remote && !$this->file) { $this->file = Request::factory($this->remote)->download(null, $path); } if (!$this->file || !$this->remote && !Upload::not_empty($this->file)) { throw new Kohana_Exception(__('No image')); } else { if (!Upload::size($this->file, Kohana::$config->load('image.filesize'))) { throw new Kohana_Exception(__('Image too big (limit :size)', array(':size' => Kohana::$config->load('image.filesize')))); } else { if (!Upload::type($this->file, Kohana::$config->load('image.filetypes')) && !in_array($this->file['type'], Kohana::$config->load('image.mimetypes'))) { throw new Kohana_Exception(__('Invalid image type (use :types)', array(':types' => implode(', ', Kohana::$config->load('image.filetypes'))))); } } } $upload = $this->file; if ($this->remote && !is_uploaded_file($upload['tmp_name'])) { // As a remote file is no actual file field, manually set the filename $this->file = basename($upload['tmp_name']); } else { if (is_uploaded_file($upload['tmp_name'])) { // Sanitize the filename $upload['name'] = preg_replace('/[^a-z0-9-\\.]/', '-', mb_strtolower($upload['name'])); // Strip multiple dashes $upload['name'] = preg_replace('/-{2,}/', '-', $upload['name']); // Try to save upload if (false !== ($this->file = Upload::save($upload, null, $path))) { // Get new filename $this->file = basename($this->file); } } } } try { parent::save(); } catch (Validation_Exception $e) { if ($new && $this->file) { unlink($path . $this->file); } throw $e; } // Some magic on created images only if ($new) { // Make sure we have the new target directory $new_path = Kohana::$config->load('image.path') . URL::id($this->id); if (!is_dir($new_path)) { mkdir($new_path, 0777, true); chmod($new_path, 0777); } if (is_writable($new_path)) { $new_path = rtrim($new_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } else { throw new Kohana_Exception(get_class($this) . ' can not write to directory'); } // New file name with some random postfix for hard to guess filenames !$this->postfix and $this->postfix = Text::random('alnum', 8); $new_file = $this->id . '_' . $this->postfix . Kohana::$config->load('image.postfix_original') . '.jpg'; // Rename and move to correct directory using image id $old_file = $this->file; if (!rename($path . $old_file, $new_path . $new_file)) { unlink($path . $old_file); throw new Kohana_Exception(get_class($this) . ' could not move uploaded image'); } $this->file = $new_file; // Start creating images $this->_generate_images($new_path . $new_file); parent::save(); } return $this; }
/** * Returns a nested array of product categories * * @return array */ public function product_categories() { return AutoModeler_ORM::factory('vendo_product_category')->full_tree(NULL, TRUE, $this->product); }
public function __get($key) { if (strpos($key, 'is_') === 0 && isset($this->_data[$key])) { return $this->_data[$key] == 't'; } return parent::__get($key); }
/** * Var method to get the list of product categories * * @return array */ public function product_categories() { return AutoModeler_ORM::factory('vendo_product_category')->full_tree(); }
/** * Overload load() to use with() * * @return $this when loading one object * @return Database_MySQL_Result when loading multiple results */ public function load(Database_Query_Builder_Select $query = NULL, $limit = 1) { if ($query == NULL) { $query = db::select_array(array_keys($this->_data)); } if ($this->_load_with !== NULL) { if (is_array($this->_load_with)) { $model = current(array_keys($this->_load_with)); $alias = current(array_values($this->_load_with)); } else { $model = $this->_load_with; $alias = $this->_load_with; } $fields = array(); foreach ($this->fields() as $field) { $fields[] = array($field, str_replace($this->_table_name . '.', '', $field)); } foreach (AutoModeler_ORM::factory($model)->fields() as $field) { $fields[] = array($field, str_replace('.', ':', $field)); } $query->select_array($fields, TRUE); $join_model = Model::factory($model); $join_table = $join_model->get_table_name(); $query->join($join_table)->on($join_table . '.id', '=', $this->_table_name . '.' . $alias . '_id'); } return parent::load($query, $limit); }
/** * Save model and expire caches. * * @param mixed $validation * @return integer */ public function save($validation = null) { if ($result = parent::save($validation)) { self::expire_caches($this->id); } return $result; }
/** * Overload delete method to delete file * * @return int */ public function delete() { if ($foo = parent::delete()) { unlink($this->path() . $this->filename); // Todo: delete straggler directory return $foo; } }