2010-09-04 01:25:52 +02:00
< ? php
/**
* @ copyright Copyright ( c ) 2010 , The volkszaehler . org project
* @ package default
* @ license http :// www . opensource . org / licenses / gpl - license . php GNU Public License
*/
/*
* This file is part of volkzaehler . org
*
* volkzaehler . org is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* any later version .
*
* volkzaehler . org is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler . org . If not , see < http :// www . gnu . org / licenses />.
*/
namespace Volkszaehler\Controller ;
2010-09-25 00:42:07 +02:00
use Volkszaehler\Definition ;
2010-09-04 01:25:52 +02:00
use Volkszaehler\Util ;
use Volkszaehler\Model ;
/**
* Entity controller
*
* @ author Steffen Vogel < info @ steffenvogel . de >
* @ package default
*/
2010-09-22 02:28:09 +02:00
class EntityController extends Controller {
2010-09-04 01:25:52 +02:00
/**
2010-09-19 20:47:29 +02:00
* Get entity
2010-09-04 01:25:52 +02:00
*
2010-09-22 02:28:09 +02:00
* @ param string $identifier
2010-09-04 01:25:52 +02:00
*/
2010-09-22 23:18:08 +02:00
public function get ( $uuid ) {
if ( ! Util\UUID :: validate ( $uuid )) {
throw new \Exception ( 'Invalid UUID: ' . $uuid );
}
2010-09-22 02:28:09 +02:00
$dql = ' SELECT a , p
2010-10-10 19:54:59 +02:00
FROM Volkszaehler\Model\Entity a
LEFT JOIN a . properties p
WHERE a . uuid = ? 1 ' ;
2010-09-22 02:28:09 +02:00
$q = $this -> em -> createQuery ( $dql );
2010-09-22 23:18:08 +02:00
$q -> setParameter ( 1 , $uuid );
2010-09-22 02:28:09 +02:00
try {
return $q -> getSingleResult ();
} catch ( \Doctrine\ORM\NoResultException $e ) {
2010-09-22 23:18:08 +02:00
throw new \Exception ( 'No entity found with UUID: ' . $uuid );
2010-09-22 02:28:09 +02:00
}
}
2010-09-04 01:25:52 +02:00
/**
2010-09-19 20:47:29 +02:00
* Delete entity by uuid
2010-09-04 01:25:52 +02:00
*/
2010-09-19 20:47:29 +02:00
public function delete ( $identifier ) {
$entity = $this -> get ( $identifier );
2010-09-04 01:25:52 +02:00
2010-09-19 20:47:29 +02:00
$this -> em -> remove ( $entity );
$this -> em -> flush ();
2010-09-04 01:25:52 +02:00
}
/**
2010-09-19 20:47:29 +02:00
* Edit entity properties
2010-09-04 01:25:52 +02:00
*/
2010-09-19 20:47:29 +02:00
public function edit ( $identifier ) {
$entity = $this -> get ( $identifier );
2010-10-10 19:54:59 +02:00
$this -> setProperties ( $entity , $this -> view -> request -> getParameters ());
2010-09-28 15:46:30 +02:00
$this -> em -> flush ();
return $entity ;
}
2010-09-28 18:17:21 +02:00
/**
* Adds an entity to the uuids cookie
* @ param Model\Entity $entity
*/
2010-09-28 15:46:30 +02:00
protected function setCookie ( Model\Entity $entity ) {
if ( $uuids = $this -> view -> request -> getParameter ( 'uuids' , 'cookies' )) {
$uuids = Util\JSON :: decode ( $uuids );
}
else {
$uuids = new Util\JSON ();
}
// add new UUID
$uuids -> append ( $entity -> getUuid ());
// remove duplicates
$uuids -> exchangeArray ( array_unique ( $uuids -> getArrayCopy ()));
// send new cookie to browser
2010-10-10 19:54:59 +02:00
setcookie ( 'uuids' , $uuids -> encode (), 0 , '/' ); // TODO correct path
2010-09-28 15:46:30 +02:00
}
2010-09-04 01:25:52 +02:00
2010-09-28 18:17:21 +02:00
/**
* Removes an entity from the uuids cookie
* @ param Model\Entity $entity
*/
2010-09-28 15:46:30 +02:00
protected function unsetCookie ( Model\Entity $entity ) {
if ( $uuids = $this -> view -> request -> getParameter ( 'uuids' , 'cookies' )) {
$uuids = Util\JSON :: decode ( $uuids );
}
else {
$uuids = new Util\JSON ();
}
// remove old UUID
$uuids -> exchangeArray ( array_filter ( $uuids -> getArrayCopy , function ( $uuid ) use ( $entity ) {
return $uuid != $entity -> getUuid ();
}));
// send new cookie to browser
2010-09-28 18:17:21 +02:00
setcookie ( 'uuids' , $uuids -> encode (), 0 , '/' ); // TODO correct path
2010-09-28 15:46:30 +02:00
}
2010-10-10 19:54:59 +02:00
/**
* Update / set / delete properties of properties
*/
protected function setProperties ( Model\Entity $entity , $parameters ) {
foreach ( $parameters as $parameter => $value ) {
2010-09-25 00:42:07 +02:00
if ( Definition\PropertyDefinition :: exists ( $parameter )) {
2010-09-19 20:47:29 +02:00
if ( $value == '' ) {
$entity -> unsetProperty ( $parameter , $this -> em );
}
else {
$entity -> setProperty ( $parameter , $value );
2010-09-06 08:25:20 +02:00
}
}
2010-09-19 20:47:29 +02:00
}
2010-09-04 01:25:52 +02:00
}
2010-10-10 19:54:59 +02:00
/**
* Filter entites by properties
*/
public function filter ( array $properties ) {
$dql = ' SELECT a , p
FROM Volkszaehler\Model\Entity a
LEFT JOIN a . properties p ' ;
$sqlWhere = array ();
$i = 0 ;
foreach ( $properties as $property => $value ) {
switch ( Definition\PropertyDefinition :: get ( $property ) -> getType ()) {
case 'string' :
case 'text' :
case 'multiple' :
$value = " ' " . $value . " ' " ;
break ;
case 'boolean' :
$value = ( $value ) ? 1 : 0 ;
}
2010-10-19 22:37:47 +02:00
$sqlWhere [] = 'EXISTS (SELECT p' . $i . ' FROM \Volkszaehler\Model\Property p' . $i . ' WHERE p' . $i . '.name = \'' . $property . '\' AND p' . $i . '.value = ' . $value . ' AND p' . $i . '.entity = a)' ;
2010-10-10 19:54:59 +02:00
$i ++ ;
}
if ( count ( $sqlWhere ) > 0 ) {
$dql .= ' WHERE ' . implode ( ' AND ' , $sqlWhere );
}
$q = $this -> em -> createQuery ( $dql );
return $q -> getSingleResult ();
}
}
?>