Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1998

Přidáno uživatelem David Raška před více než 11 roky(ů)

Upravy:
- Prejmenovani controlleru a modelu oblibenych stranek uzivatele

Zobrazit rozdíly:

freenetis/branches/1.1/application/controllers/favourites.php
<?php
defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is released under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/
/**
* Allows user to manage his favourite pages
*
* @package Controller
*/
class Favourites_Controller extends Controller
{
/**
* Function adds, edits or removes users faouvrite page
*/
public function toggle()
{
// get page title and address
$title = @$_GET['title'];
$page = @$_GET['page'];
// stop if not set
if (!$title || !$page)
{
Controller::warning(PARAMETER);
}
// create model
$favourite = new Favourite_Model();
// create new form
$form = new Forge();
$form->set_attr('class', 'form nopopup');
$input = $form->input('name')
->value(substr(htmlspecialchars_decode($title), 0, 50))
->rules('required|length[1,50]');
$checkbox = $form->checkbox('default')
->label(__('Default page') . help::hint('default_page'));
// edit or delete
if ($favourite->is_users_favourite($this->user_id, $page))
{
$form->checkbox('remove')
->label('Remove from favourites');
$fav = $favourite->get_favourite_page($this->user_id, $page);
$input->value($fav->title);
$checkbox->checked($fav->default_page);
$form->submit('Edit favourites');
$title = __('Edit favourites');
}
else // add to favourites
{
$form->submit('Add to favourites');
$title = __('Add to favourites');
}
// validate form
if ($form->validate())
{
$form_data = $form->as_array();
$default = !empty($form_data['default']);
$title = $form_data['name'];
$remove = !empty($form_data['remove']);
// remove or edit
if ($favourite->is_users_favourite($this->user_id, $page))
{
if ($remove) // remove from favourites
{
if ($favourite->remove_page_from_favourites($this->user_id, $page))
{
status::success('Page has been removed from favourites.');
}
else
{
status::warning('Page has not been removed from favourites.');
}
}
else // edit favourites
{
if ($favourite->edit_favourites($this->user_id, $page, $title, $default))
{
status::success('Favourite page has been saved.');
}
else
{
status::warning('Favourite page has not been saved.');
}
}
}
else // add to favourites
{
if ($favourite->add_page_to_favourite($this->user_id, $page, $title, $default))
{
status::success('Page has been added to favourites.');
}
else
{
status::warning('Page has not been added to favourites.');
}
}
url::redirect($page);
}
// show form
$view = new View('main');
$view->title = $title;
$view->content = new View('form');
$view->content->headline = $title;
$view->content->form = $form;
$view->render(TRUE);
}
}
freenetis/branches/1.1/application/controllers/login.php
}
else
{
$favourite = ORM::factory('favourite')->get_user_default_page($user_id);
$favourite = ORM::factory('user_favourite_pages')->get_user_default_page($user_id);
if ($favourite)
{
......
// check if is logged in
if (isset($_SESSION['username']))
{
$favourite = ORM::factory('favourite')->get_user_default_page($_SESSION['user_id']);
$favourite = ORM::factory('user_favourite_pages')->get_user_default_page($_SESSION['user_id']);
if ($favourite)
{
freenetis/branches/1.1/application/controllers/user_favourite_pages.php
<?php
defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is released under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/
/**
* Allows user to manage his favourite pages
*
* @package Controller
*/
class User_favourite_pages_Controller extends Controller
{
/**
* Function adds, edits or removes users faouvrite page
*/
public function toggle()
{
// get page title and address
$title = @$_GET['title'];
$page = @$_GET['page'];
// stop if not set
if (!$title || !$page)
{
Controller::warning(PARAMETER);
}
// create model
$favourite = new User_favourite_pages_Model();
// create new form
$form = new Forge();
$form->set_attr('class', 'form nopopup');
$input = $form->input('name')
->value(substr(htmlspecialchars_decode($title), 0, 50))
->rules('required|length[1,50]');
$checkbox = $form->checkbox('default')
->label(__('Default page') . help::hint('default_page'));
// edit or delete
if ($favourite->is_users_favourite($this->user_id, $page))
{
$form->checkbox('remove')
->label('Remove from favourites');
$fav = $favourite->get_favourite_page($this->user_id, $page);
$input->value($fav->title);
$checkbox->checked($fav->default_page);
$form->submit('Edit favourites');
$title = __('Edit favourites');
}
else // add to favourites
{
$form->submit('Add to favourites');
$title = __('Add to favourites');
}
// validate form
if ($form->validate())
{
$form_data = $form->as_array();
$default = !empty($form_data['default']);
$title = $form_data['name'];
$remove = !empty($form_data['remove']);
// remove or edit
if ($favourite->is_users_favourite($this->user_id, $page))
{
if ($remove) // remove from favourites
{
if ($favourite->remove_page_from_favourites($this->user_id, $page))
{
status::success('Page has been removed from favourites.');
}
else
{
status::warning('Page has not been removed from favourites.');
}
}
else // edit favourites
{
if ($favourite->edit_favourites($this->user_id, $page, $title, $default))
{
status::success('Favourite page has been saved.');
}
else
{
status::warning('Favourite page has not been saved.');
}
}
}
else // add to favourites
{
if ($favourite->add_page_to_favourite($this->user_id, $page, $title, $default))
{
status::success('Page has been added to favourites.');
}
else
{
status::warning('Page has not been added to favourites.');
}
}
url::redirect($page);
}
// show form
$view = new View('main');
$view->title = $title;
$view->content = new View('form');
$view->content->headline = $title;
$view->content->form = $form;
$view->render(TRUE);
}
}
freenetis/branches/1.1/application/libraries/MY_Controller.php
->count_all_unread_inbox_messages_by_user_id($this->user_id);
// is this page favourite
$this->is_favourite = ORM::factory('favourite')->is_users_favourite($this->user_id, url_lang::current());
$this->is_favourite = ORM::factory('user_favourite_pages')->is_users_favourite($this->user_id, url_lang::current());
// count registered members if enabled
if (Settings::get('self_registration'))
......
/*********************** FAVOURITES ***********************/
$favourites = ORM::factory('favourite')->get_users_favourites($this->user_id);
$favourites = ORM::factory('user_favourite_pages')->get_users_favourites($this->user_id);
if ($favourites->count())
{
freenetis/branches/1.1/application/models/favourite.php
<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/
/**
* Pivot table for connecting users and theirs contacts, also containts
* whitelisted column for defining whitelist of contact in notification.
*
* @package Model
*/
class Favourite_Model extends Model
{
/**
* Checks if user have given page in his favourites
*
* @param int $user_id
* @param string $page
* @return bool TRUE if page is favourite
*/
public function is_users_favourite($user_id, $page)
{
$result = $this->db->query("
SELECT page
FROM users_favourites
WHERE user_id = ? AND
page = ?
", $user_id, $page);
return ($result && $result->count() == 1);
}
/**
* Insert page to users favourites
*
* @param int $user_id User ID
* @param string $title Favourite page title
* @param string $page Page address
* @param int $default Is default page
* @return boolean TRUE if insert was successfull
*/
public function add_page_to_favourite($user_id, $page, $title, $default)
{
// remove default tag from other favourites
if ($default)
{
$this->remove_user_default_page($user_id);
}
$result = $this->db->query("
INSERT INTO users_favourites (user_id , title, page , default_page)
VALUES ( ? , ? , ? , ? )
", $user_id, $title, $page, $default);
return $result != NULL;
}
/**
* Removes page from users favourites
*
* @param int $user_id User ID
* @param string $page Page address
* @return ORM object
*/
public function remove_page_from_favourites($user_id, $page)
{
$result = $this->db->query("
DELETE FROM users_favourites
WHERE user_id = ? AND
page = ?
", $user_id, $page);
return $result;
}
/**
* Returns all favourites of given user
*
* @param int $user_id User ID
* @return ORM object
*/
public function get_users_favourites($user_id)
{
return $this->db->query("
SELECT * FROM users_favourites
WHERE user_id = ?
ORDER BY title ASC
", $user_id);
}
/**
* Updates user favourite page details
*
* @param int $user_id User ID
* @param string $page Page address
* @param string $title Favourite page title
* @param boolean $default Is default page
* @return boolean
*/
public function edit_favourites($user_id, $page, $title, $default)
{
// remove default tag from other favourites
if ($default)
{
$this->remove_user_default_page($user_id);
}
// update data
$result = $this->db->query("
UPDATE users_favourites
SET title=?,
default_page=?
WHERE user_id = ? AND
page = ?
", $title, $default, $user_id, $page);
return $result != NULL;
}
/**
* Removes default page tag from users favourites
*
* @param int $user_id User Id
* @return ORM object
*/
public function remove_user_default_page($user_id)
{
return $this->db->query("
UPDATE users_favourites
SET default_page=0
WHERE user_id = ?
", $user_id);
}
/**
* Get favourite page details
*
* @param int $user_id User Id
* @param string $page Page URL
* @return ORM object
*/
public function get_favourite_page($user_id, $page)
{
$result = $this->db->query("
SELECT *
FROM users_favourites
WHERE user_id = ? AND
page = ?
", $user_id, $page);
if ($result && $result->count() == 1)
{
return $result->current();
}
return null;
}
/**
* Get users default page
*
* @param int $user_id User Id
* @return ORM object
*/
public function get_user_default_page($user_id)
{
$result = $this->db->query("
SELECT *
FROM users_favourites
WHERE user_id = ? AND
default_page = 1
", $user_id);
if ($result && $result->count() == 1)
{
return $result->current();
}
return null;
}
}
freenetis/branches/1.1/application/models/user_favourite_pages.php
<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/
/**
* Pivot table for connecting users and theirs contacts, also containts
* whitelisted column for defining whitelist of contact in notification.
*
* @package Model
*/
class User_favourite_pages_Model extends Model
{
/**
* Checks if user have given page in his favourites
*
* @param int $user_id
* @param string $page
* @return bool TRUE if page is favourite
*/
public function is_users_favourite($user_id, $page)
{
$result = $this->db->query("
SELECT page
FROM user_favourite_pages
WHERE user_id = ? AND
page = ?
", $user_id, $page);
return ($result && $result->count() == 1);
}
/**
* Insert page to users favourites
*
* @param int $user_id User ID
* @param string $title Favourite page title
* @param string $page Page address
* @param int $default Is default page
* @return boolean TRUE if insert was successfull
*/
public function add_page_to_favourite($user_id, $page, $title, $default)
{
// remove default tag from other favourites
if ($default)
{
$this->remove_user_default_page($user_id);
}
$result = $this->db->query("
INSERT INTO user_favourite_pages (user_id , title, page , default_page)
VALUES ( ? , ? , ? , ? )
", $user_id, $title, $page, $default);
return $result != NULL;
}
/**
* Removes page from users favourites
*
* @param int $user_id User ID
* @param string $page Page address
* @return ORM object
*/
public function remove_page_from_favourites($user_id, $page)
{
$result = $this->db->query("
DELETE FROM user_favourite_pages
WHERE user_id = ? AND
page = ?
", $user_id, $page);
return $result;
}
/**
* Returns all favourites of given user
*
* @param int $user_id User ID
* @return ORM object
*/
public function get_users_favourites($user_id)
{
return $this->db->query("
SELECT * FROM user_favourite_pages
WHERE user_id = ?
ORDER BY title ASC
", $user_id);
}
/**
* Updates user favourite page details
*
* @param int $user_id User ID
* @param string $page Page address
* @param string $title Favourite page title
* @param boolean $default Is default page
* @return boolean
*/
public function edit_favourites($user_id, $page, $title, $default)
{
// remove default tag from other favourites
if ($default)
{
$this->remove_user_default_page($user_id);
}
// update data
$result = $this->db->query("
UPDATE user_favourite_pages
SET title=?,
default_page=?
WHERE user_id = ? AND
page = ?
", $title, $default, $user_id, $page);
return $result != NULL;
}
/**
* Removes default page tag from users favourites
*
* @param int $user_id User Id
* @return ORM object
*/
public function remove_user_default_page($user_id)
{
return $this->db->query("
UPDATE user_favourite_pages
SET default_page=0
WHERE user_id = ?
", $user_id);
}
/**
* Get favourite page details
*
* @param int $user_id User Id
* @param string $page Page URL
* @return ORM object
*/
public function get_favourite_page($user_id, $page)
{
$result = $this->db->query("
SELECT *
FROM user_favourite_pages
WHERE user_id = ? AND
page = ?
", $user_id, $page);
if ($result && $result->count() == 1)
{
return $result->current();
}
return null;
}
/**
* Get users default page
*
* @param int $user_id User Id
* @return ORM object
*/
public function get_user_default_page($user_id)
{
$result = $this->db->query("
SELECT *
FROM user_favourite_pages
WHERE user_id = ? AND
default_page = 1
", $user_id);
if ($result && $result->count() == 1)
{
return $result->current();
}
return null;
}
}
freenetis/branches/1.1/application/views/main.php
}
// add to favourites page canot be added to favourites
if (url_lang::current() != 'favourites/toggle')
if (url_lang::current() != 'user_favourite_pages/toggle')
{
echo ' | ' . html::anchor('favourites/toggle?title=' . urlencode(strip_tags($title)) . '&page=' . urlencode(url_lang::current()) , html::image(array
echo ' | ' . html::anchor('user_favourite_pages/toggle?title=' . urlencode(strip_tags($title)) . '&page=' . urlencode(url_lang::current()) , html::image(array
(
'src' => $src,
'alt' => $caption,
freenetis/branches/1.1/db_upgrades/upgrade_1.1.0~alpha37.php
*/
$upgrade_sql['1.1.0~alpha37'] = array
(
"CREATE TABLE `users_favourites` (
"CREATE TABLE `user_favourite_pages` (
`user_id` INT(30) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`page` VARCHAR(255) NOT NULL,

Také k dispozici: Unified diff