2015-04-02 23:10:41 +02:00
< ? php
namespace Framadate\Repositories ;
2015-04-03 00:11:36 +02:00
use Framadate\FramaDB ;
2015-04-02 23:10:41 +02:00
use Framadate\Utils ;
2015-04-03 00:11:36 +02:00
use PDO ;
2015-04-02 23:10:41 +02:00
2015-04-02 23:23:34 +02:00
class PollRepository extends AbstractRepository {
2015-04-02 23:10:41 +02:00
2015-04-02 23:23:34 +02:00
function __construct ( FramaDB $connect ) {
parent :: __construct ( $connect );
2015-04-02 23:10:41 +02:00
}
public function insertPoll ( $poll_id , $admin_poll_id , $form ) {
$sql = 'INSERT INTO `' . Utils :: table ( 'poll' ) . ' `
( id , admin_id , title , description , admin_name , admin_mail , end_date , format , editable , receiveNewVotes , receiveNewComments )
VALUES ( ? , ? , ? , ? , ? , ? , FROM_UNIXTIME ( ? ), ? , ? , ? , ? ) ' ;
2015-04-02 23:23:34 +02:00
$prepared = $this -> prepare ( $sql );
2015-04-02 23:10:41 +02:00
$prepared -> execute ( array ( $poll_id , $admin_poll_id , $form -> title , $form -> description , $form -> admin_name , $form -> admin_mail , $form -> end_date , $form -> format , $form -> editable , $form -> receiveNewVotes , $form -> receiveNewComments ));
}
2015-04-02 23:23:34 +02:00
function findById ( $poll_id ) {
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'poll' ) . '` WHERE id = ?' );
2015-04-02 23:10:41 +02:00
2015-04-02 23:23:34 +02:00
$prepared -> execute ( array ( $poll_id ));
$poll = $prepared -> fetch ();
$prepared -> closeCursor ();
2015-04-02 23:10:41 +02:00
2015-04-02 23:23:34 +02:00
return $poll ;
}
public function existsById ( $poll_id ) {
$prepared = $this -> prepare ( 'SELECT 1 FROM `' . Utils :: table ( 'poll' ) . '` WHERE id = ?' );
$prepared -> execute ( array ( $poll_id ));
return $prepared -> rowCount () > 0 ;
}
2015-04-02 23:10:41 +02:00
2015-04-02 23:23:34 +02:00
function update ( $poll ) {
2015-04-07 23:17:26 +02:00
$prepared = $this -> prepare ( 'UPDATE `' . Utils :: table ( 'poll' ) . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=?, hidden=? WHERE id = ?' );
2015-04-02 23:10:41 +02:00
2015-04-07 23:17:26 +02:00
return $prepared -> execute ([ $poll -> title , $poll -> admin_name , $poll -> admin_mail , $poll -> description , $poll -> end_date , $poll -> active , $poll -> editable , $poll -> hidden , $poll -> id ]);
2015-04-02 23:10:41 +02:00
}
2015-04-03 00:11:36 +02:00
function deleteById ( $poll_id ) {
$prepared = $this -> prepare ( 'DELETE FROM `' . Utils :: table ( 'poll' ) . '` WHERE id = ?' );
return $prepared -> execute ([ $poll_id ]);
}
/**
* Find old polls . Limit : 20.
*
* @ return array Array of old polls
*/
public function findOldPolls () {
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'poll' ) . '` WHERE DATE_ADD(`end_date`, INTERVAL ' . PURGE_DELAY . ' DAY) < NOW() AND `end_date` != 0 LIMIT 20' );
$prepared -> execute ([]);
return $prepared -> fetchAll ();
}
/**
* Search polls in databse .
*
2015-10-28 13:06:02 +01:00
* @ param array $search Array of search : [ 'id' =>... , 'title' =>... , 'name' =>... , 'mail' =>... ]
2015-09-13 00:53:03 +02:00
* @ param int $start The number of first entry to select
* @ param int $limit The number of entries to find
2015-04-03 00:11:36 +02:00
* @ return array The found polls
*/
2015-09-13 00:53:03 +02:00
public function findAll ( $search , $start , $limit ) {
2015-04-03 00:11:36 +02:00
// Polls
$prepared = $this -> prepare ( '
SELECT p .* ,
( SELECT count ( 1 ) FROM `' . Utils::table('vote') . '` v WHERE p . id = v . poll_id ) votes
FROM `' . Utils::table('poll') . '` p
WHERE ( : id = " " OR p . id LIKE : id )
AND ( : title = " " OR p . title LIKE : title )
AND ( : name = " " OR p . admin_name LIKE : name )
2015-10-28 13:06:02 +01:00
AND ( : mail = " " OR p . admin_mail LIKE : mail )
2015-04-03 00:11:36 +02:00
ORDER BY p . title ASC
2015-09-13 00:53:03 +02:00
LIMIT : start , : limit
2015-04-03 00:11:36 +02:00
' );
$poll = $search [ 'poll' ] . '%' ;
$title = '%' . $search [ 'title' ] . '%' ;
$name = '%' . $search [ 'name' ] . '%' ;
2015-10-28 13:06:02 +01:00
$mail = '%' . $search [ 'mail' ] . '%' ;
2015-04-03 00:11:36 +02:00
$prepared -> bindParam ( ':id' , $poll , PDO :: PARAM_STR );
$prepared -> bindParam ( ':title' , $title , PDO :: PARAM_STR );
$prepared -> bindParam ( ':name' , $name , PDO :: PARAM_STR );
2015-10-28 13:06:02 +01:00
$prepared -> bindParam ( ':mail' , $mail , PDO :: PARAM_STR );
2015-09-13 00:53:03 +02:00
$prepared -> bindParam ( ':start' , $start , PDO :: PARAM_INT );
$prepared -> bindParam ( ':limit' , $limit , PDO :: PARAM_INT );
2015-04-03 00:11:36 +02:00
$prepared -> execute ();
return $prepared -> fetchAll ();
}
2015-04-08 21:52:09 +02:00
/**
* Find all polls that are created with the given admin mail .
*
* @ param string $mail Email address of the poll admin
* @ return array The list of matching polls
*/
public function findAllByAdminMail ( $mail ) {
$prepared = $this -> prepare ( 'SELECT * FROM `' . Utils :: table ( 'poll' ) . '` WHERE admin_mail = :admin_mail' );
$prepared -> execute ( array ( 'admin_mail' => $mail ));
return $prepared -> fetchAll ();
}
2015-04-03 00:11:36 +02:00
/**
* Get the total number of polls in databse .
*
2015-09-13 00:53:03 +02:00
* @ param array $search Array of search : [ 'id' =>... , 'title' =>... , 'name' =>... ]
2015-04-03 00:11:36 +02:00
* @ return int The number of polls
*/
2015-09-13 00:53:03 +02:00
public function count ( $search = null ) {
2015-04-03 00:11:36 +02:00
// Total count
2015-09-13 00:53:03 +02:00
$prepared = $this -> prepare ( '
SELECT count ( 1 ) nb
FROM `' . Utils::table('poll') . '` p
WHERE ( : id = " " OR p . id LIKE : id )
AND ( : title = " " OR p . title LIKE : title )
AND ( : name = " " OR p . admin_name LIKE : name )
ORDER BY p . title ASC ' );
$poll = $search == null ? '' : $search [ 'poll' ] . '%' ;
$title = $search == null ? '' : '%' . $search [ 'title' ] . '%' ;
$name = $search == null ? '' : '%' . $search [ 'name' ] . '%' ;
$prepared -> bindParam ( ':id' , $poll , PDO :: PARAM_STR );
$prepared -> bindParam ( ':title' , $title , PDO :: PARAM_STR );
$prepared -> bindParam ( ':name' , $name , PDO :: PARAM_STR );
$prepared -> execute ();
$count = $prepared -> fetch ();
/* echo '---' ;
print_r ( $count );
echo '---' ;
exit ; */
2015-04-03 00:11:36 +02:00
return $count -> nb ;
}
2015-04-02 23:10:41 +02:00
}