/** * Function for easy update a ORM object * * @param ORM $object ORM object to update * @param array $messages Array of custom messages */ public function update(ORM $object, array $messages = array()) { // Check if is a valid object if (!$object->loaded()) { Messages::warning(isset($messages['warning']) ? $messages['warning'] : 'El elemento que intentas modificar no existe o fue eliminado.'); $this->go(); } // Only if Request is POST if ($this->request->method() == Request::POST) { // Catch ORM_Validation try { // Set object values and update $object->values($this->request->post())->update(); // If object is saved.... if ($object->saved()) { // Success message & redirect Messages::success(isset($messages['success']) ? $messages['success'] : 'El elemento fue modificado correctamente.'); $this->go(); } } catch (ORM_Validation_Exception $e) { // Error message if (isset($messages['error'])) { Messages::error($messages['error']); } // Validation messages Messages::validation($e); } } }
function row_exist(ORM $orm, $field_name, $field_value, array $where = array()) { $search_orm = clone $orm; $search_orm->clear(); foreach ($where as $_conf) { if ($_conf[0] === 'and') { $search_orm->where($_conf[1], $_conf[2], $_conf[3]); } elseif ($_conf[0] === 'or') { $search_orm->or_where($_conf[1], $_conf[2], $_conf[3]); } } if ($orm->loaded()) { $orm = $search_orm->where($field_name, '=', $field_value)->and_where('id', '!=', $orm->id)->find(); } else { $orm = $search_orm->where($field_name, '=', $field_value)->find(); } return $orm->loaded(); }
/** * Make sure the user has permission to do the action on this object * * Similar to [Comment::access] but this return TRUE/FALSE instead of exception * * @param string $action The action `view|edit|delete` default `view` * @param ORM $comment The comment object * @param Model_User $user The user object to check permission, defaults to loaded in user * @param string $misc The misc element usually `id|slug` for logging purpose * * @return boolean * * @throws HTTP_Exception_404 * * @uses User::active_user * @uses Module::event */ public static function comment($action = 'view', ORM $comment, Model_User $user = NULL, $misc = NULL) { if (!in_array($action, array('view', 'edit', 'delete', 'add', 'list'), TRUE)) { // If the $action was not one of the supported ones, we return access denied. Log::notice('Unauthorized attempt to access non-existent action :act.', array(':act' => $action)); return FALSE; } if (!$comment->loaded()) { // If the $action was not one of the supported ones, we return access denied. throw HTTP_Exception::factory(404, 'Attempt to access non-existent comment.'); } // If no user object is supplied, the access check is for the current user. if (is_null($user)) { $user = User::active_user(); } if (self::check('bypass comment access', $user)) { return TRUE; } // Allow other modules to interact with access Module::event('comment_access', $action, $comment); if ($action === 'view') { if ($comment->status === 'publish' and self::check('access comment', $user)) { return TRUE; } elseif ($comment->status != 'publish' and $comment->author == (int) $user->id and $user->id != 1) { return TRUE; } elseif (self::check('administer comment', $user)) { return TRUE; } else { return FALSE; } } if ($action === 'edit') { if (self::check('edit own comment') and $comment->author == (int) $user->id and $user->id != 1) { return TRUE; } elseif (self::check('administer comment', $user)) { return TRUE; } else { return FALSE; } } if ($action === 'delete') { if ((self::check('delete own comment') or self::check('delete any comment')) and $comment->author == (int) $user->id and $user->id != 1) { return TRUE; } elseif (self::check('administer comment', $user)) { return TRUE; } else { return FALSE; } } return TRUE; }
/** * Sign-up using data from OAuth provider. * * Override this method to add your own sign up process. * * @param ORM $user * @param array $data * @param string $provider * @return ORM */ public function sso_signup(ORM $user, array $data, $provider_field) { if (!$user->loaded()) { // Add user $user->{$provider_field} = $data['id']; $user->user_type = 0; $user->username = $provider_field . $data['id']; $user->user_password = md5($user->username . microtime(TRUE)); if ($provider_field == 'vkontakte_id' or $provider_field == 'facebook_id') { $user->email = $user->username . '@bt-lady.com.ua'; $user->firstname = $data['first_name']; $user->lastname = $data['last_name']; if (array_key_exists('birthday', $data) or array_key_exists('bdate', $data)) { $date_key = isset($data['bdate']) ? 'bdate' : 'birthday'; $date = NULL; try { $_date = new DateTime($data[$date_key]); $date = $_date->format('d-m-Y'); } catch (Exception $e) { } $user->birthday = $date; } if (array_key_exists('email', $data)) { $user->email = $data['email']; } if (array_key_exists('photo_big', $data)) { $user->avatar = $data['photo_big']; } if (array_key_exists('location', $data) or array_key_exists('hometown', $data)) { $key = isset($data['location']) ? 'location' : 'hometown'; if (is_array($data[$key]) and array_key_exists('name', $data[$key])) { $user->user_from = $data[$key]['name']; } } if (array_key_exists('city', $data) and array_key_exists('country', $data)) { $user->city_id = ORM::factory('city', array('vkontakte_cid' => $data['city'])); } } elseif ($provider_field == 'twitter_id') { $user->email = $data['screen_name'] . '@twitter.com'; } // Save user $user->save(); $user->add('roles', ORM::factory('role', array('name' => $provider_field))); $user->add('roles', ORM::factory('role', array('name' => 'social'))); $user->update(); } elseif ($user->loaded() and empty($user->{$provider_field})) { // If user is found, but provider id is missing add it to details. // We can do this merge, because this means user is found by email address, // that is already confirmed by this OAuth provider, so it's considered trusted. $user->{$provider_field} = $data['id']; // Save user $user->save(); } // Return user return $user; }
/** * Internal helper function for generating the final filtering query * * @param ORM $orm_instance * @param Database_Query_Select $sub_query * @param array $filters * @param string $filter_type * * @return Database_Query_Select */ private static function _build_final_filter_query($orm_instance, $sub_query, $filters, $filter_type) { $final_query = DB::select(array('droplets.id', 'droplet_id')); if ($sub_query) { $final_query->union($sub_query, TRUE); } $final_query->from('droplets'); // Check the filter type if ($filter_type === 'tags') { $final_query->join('droplets_tags', 'INNER')->on('droplets_tags.droplet_id', '=', 'droplets.id')->join('tags', 'INNER')->on('droplets_tags.tag_id', '=', 'tags.id'); } elseif ($filter_type === 'places') { $final_query->join('droplets_places', 'INNER')->on('droplets_places.droplet_id', '=', 'droplets.id')->join('places', 'INNER')->on('droplets_places.place_id', '=', 'places.id'); } // Check the ORM instance class if ($orm_instance instanceof Model_River and $orm_instance->loaded()) { $final_query->join('rivers_droplets', 'INNER')->on('rivers_droplets.droplet_id', '=', 'droplets.id')->where('rivers_droplets.river_id', '=', $orm_instance->id); } elseif ($orm_instance instanceof Model_Bucket and $orm_instance->loaded()) { $final_query->join('buckets_droplets', 'INNER')->on('buckets_droplets.droplet_id', '=', 'droplets.id')->where('buckets_droplets.bucket_id', '=', $orm_instance->id); } if ($filter_type === 'tags') { $final_query->where('tags.tag_canonical', 'IN', $filters['tags']); } elseif ($filter_type === 'places') { $final_query->where('places.place_name_canonical', 'IN', $filters['places']); } return $final_query; }
public function photo(ORM $object, $size = NULL, $type = 'cropr') { if ($object->loaded()) { if ($object instanceof Model_Photo or $object instanceof Model_Abstract_Page and $object->get_alias() == 'photo') { $url = array('/' . ($size === NULL ? 'uploads' : 'thumbnails')); if (preg_match('#^([a-f0-9]{32,})\\.(gif|jpe?g|png)$#ui', $object->photo)) { $url[] = substr($object->photo, 0, 2); $url[] = substr($object->photo, 2, 4); $filename = $object->photo; } else { $url = array('/' . ($size === NULL ? 'images' : 'thumbnails') . '/articles'); $photo = explode('/', $object->photo); $photo = array_filter($photo); $filename = array_splice($photo, -1, 1); $filename = end($filename); $url = array_merge($url, $photo); } if ($size !== NULL) { $url[] = "{$type}_{$size}"; } $url[] = $filename; $url = array_filter($url); return implode('/', $url); } elseif ($object instanceof Model_Media) { $url = array('/' . ($size === NULL ? 'images' : 'thumbnails') . '/articles'); $photo = explode('/', basename($object->name)); $photo = array_filter($photo); $filename = array_splice($photo, -1, 1); $filename = end($filename); $url = array_merge($url, $photo); if ($size !== NULL) { $url[] = "{$type}_{$size}"; } $url[] = $filename; $url = array_filter($url); return implode('/', $url); } elseif ($object->photo) { $url = array('/' . ($size === NULL ? 'images' : 'thumbnails') . '/articles'); $photo = explode('/', $object->photo); $photo = array_filter($photo); $filename = array_splice($photo, -1, 1); $url = array_merge($url, $photo); if ($size !== NULL) { $url[] = $type . '_' . $size; } $url[] = end($filename); return implode('/', $url); } } return '/i/placeholder.gif'; }