/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; } }
/** * 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); }
/** * Var method to get the list of product categories * * @return array */ public function product_categories() { return AutoModeler_ORM::factory('vendo_product_category')->full_tree(); }
/** * 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); }
/** * 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); }