Utilisation du modèle Prefs

This commit is contained in:
Michael RICOIS 2012-01-06 11:43:52 +00:00
parent 5e88c47e88
commit c48243d49c
8 changed files with 52 additions and 149 deletions

View File

@ -36,11 +36,22 @@ class IndexController extends Zend_Controller_Action
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$prefsModel = new Application_Model_Prefs();
$prefsModel->setLogin($user->username);
$prefsModel->setJson($this->fields->getNotActivated($fields));
$prefsMapper = new Application_Model_PrefsMapper();
$prefsMapper->save($prefsModel);
$db = Zend_Registry::get('db');
$prefsModel = new Application_Model_Prefs($db);
$data = array(
'login' => $user->username,
'json' => $this->fields->getNotActivated($fields),
);
$sql = $prefsModel->select()->where('login = ?', $user->username);
$rows = $prefsModel->fetchAll($sql);
if (count($rows)>0) {
$prefsModel->update($data, 'login = '.$user->unsername);
} else {
$prefsModel->insert($data);
}
$this->_redirect('./');
}

View File

@ -10,11 +10,20 @@ class PreferencesController extends Zend_Controller_Action
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$prefsModel = new Application_Model_Prefs();
$prefsModel->setLogin($user->username);
$prefsModel->setJson(json_encode($this->getNotActivated(json_decode($fields))));
$prefsMapper = new Application_Model_PrefsMapper();
$prefsMapper->save($prefsModel);
$db = Zend_Registry::get('db');
$prefsModel = new Application_Model_Prefs($db);
$data = array(
'login' => $user->username,
'json' => json_encode($this->getNotActivated(json_decode($fields))),
);
$sql = $prefsModel->select()->where('login = ?', $user->username);
$rows = $prefsModel->fetchAll($sql);
if (count($rows)>0) {
$prefsModel->update($data, 'login = '.$user->unsername);
} else {
$prefsModel->insert($data);
}
$this->_redirect('./');
}

View File

@ -3,7 +3,14 @@ class PrefsController extends Zend_Controller_Action
{
public function indexAction()
{
$prefs = new Application_Model_PrefsMapper();
$this->view->entries = $prefs->fetchAll();
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$db = Zend_Registry::get('db');
$prefs = new Application_Model_Prefs($db);
$sql = $prefs->select()->where('login = ?', $user->username);
$this->view->entries = $prefs->fetchAll($sql);
}
}

View File

@ -1,5 +0,0 @@
<?php
class Application_Model_DbTable_Prefs extends Zend_Db_Table_Abstract
{
protected $_name = 'prefs';
}

View File

@ -1,63 +1,5 @@
<?php
class Application_Model_Prefs
class Application_Model_Prefs extends Zend_Db_Table_Abstract
{
protected $_login;
protected $_json;
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
return ($this->$method());
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return ($this);
}
public function setJson($json)
{
$this->_json = (string) $json;
return ($this);
}
public function getJson()
{
return ($this->_json);
}
public function setLogin($login)
{
$this->_login = (string) $login;
}
public function getLogin()
{
return ($this->_login);
}
protected $_name = 'prefs';
}

View File

@ -1,67 +0,0 @@
<?php
class Application_Model_PrefsMapper
{
protected $_dbTable;
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable()
{
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Prefs');
}
return $this->_dbTable;
}
public function save(Application_Model_Prefs $prefs)
{
$data = array(
'login' => $prefs->getLogin(),
'json' => $prefs->getJson()
);
if (null === ($login = $prefs->getLogin())) {
$this->getDbTable()->insert($data);
} else {
$this->getDbTable()->update($data, array('login = ?' => $login));
}
}
public function delete(Application_Model_Prefs $prefs)
{
$this->getDbTable()->delete('where login= ?', $prefs->getLogin());
}
public function find($login, Application_Model_Prefs $prefs)
{
$result = $this->getDbTable()->find($login);
if (0 == count($result)) {
return;
}
$row = $result->current();
$prefs->setLogin($row->login);
$prefs->setJson($row->json);
}
public function fetchAll()
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Prefs();
$entry->setLogin($row->login)
->setJson($row->json);
$entries[] = $entry;
}
return $entries;
}
}

View File

@ -2,4 +2,4 @@ CREATE TABLE IF NOT EXISTS `prefs` (
`login` varchar(255) NOT NULL,
`json` text NOT NULL,
PRIMARY KEY (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
) DEFAULT CHARSET=utf8;

View File

@ -572,10 +572,16 @@ Class Fields
{
require_once('Scores/SessionCiblage.php');
$session = new SessionCiblage();
$prefsModel = new Application_Model_Prefs();
$prefsMapper = new Application_Model_PrefsMapper();
$prefs = $prefsMapper->find($login, $prefsModel);
$json = $prefsModel->getJson();
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$db = Zend_Registry::get('db');
$prefsModel = new Application_Model_Prefs($db);
$prefs = $prefsModel->find($login);
$json = $prefs->json;
foreach($this->fields as $name => $valeur) {
if(!empty($json))
{