Skip to content

Commit

Permalink
#116: Moved PHPList logic to User model for the events Delete and Sav…
Browse files Browse the repository at this point in the history
…e. Removed it from AdminController and ProfileController.
  • Loading branch information
JuanMenendezBuitrago committed Apr 1, 2015
1 parent 59e0f8d commit 38c33fa
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 194 deletions.
2 changes: 2 additions & 0 deletions TGD/protected/config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
const PHPLIST_APPLIED_LIST = 0;
const PHPLIST_PRE_ACCEPTED_LIST = 0;
const PHPLIST_ACCEPTED_LIST = 0;
const PHPLIST_PRE_ACCEPTED_OPTED_OUT_LIST = 0;
const PHPLIST_ACCEPTED_OPTED_OUT_LIST = 0;
const PHPLIST_DENIED_LIST = 0;
const PHPLIST_LEFT_LIST = 0;
const PHPLIST_EXPELLED_LIST = 0;
Expand Down
15 changes: 3 additions & 12 deletions TGD/protected/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,23 +590,14 @@ public function actionAddToPHPList(){
$phplist = new PHPList(PHPLIST_HOST, PHPLIST_DB, PHPLIST_LOGIN, PHPLIST_PASSWORD);
$result = $phplist->addUserToList($email, $list);

if($result > 0){
if($result){
$result_data = array(
'result'=>'success',
'message'=>'The user with email ' . $email . ' has been added to the list ' . $list . '.'
);
}else{
$result_data = array('result'=>'fail');
if($result == -1){
$result_data['message'] = 'User with email ' . $email . ' couldn\'t be created.';
}else if($result == -2){
$result_data['message'] = 'There\'s no list called ' . $list . '.';
}else if($result == -3){
$result_data['message'] = 'User with email ' . $email . ' is already in list ' . $list . '.';
}else if($result === false){
$result_data['message'] = 'There has been an error processing the request.';
}

$result_data = array('result'=>'fail',
'message' => 'There has been an error processing the request.');
}

$this->_sendResponse(200, CJSON::encode($result_data), 'application/json');
Expand Down
213 changes: 131 additions & 82 deletions TGD/protected/extensions/PHPList.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ private function _getConnection(){
return $dbh;
}

/**
* Get the user id
* @param string $email User email address
* @return mixed User id or false if not found.
*/
private function _getUserId($email){

$db = $this->_db;
$sql = "SELECT * FROM phplist_user_user WHERE email = :email";

try{
$db = $this->_db;
$stmt = $db->prepare($sql);
$stmt->bindParam("email", $email);
$stmt->execute();
Expand All @@ -83,57 +90,61 @@ private function _getUserId($email){
$row = $stmt->fetch();
return $row['id'];
}else{
return -1;
return false;
}
}catch(PDOException $e){
return false;
}
}

/**
* Finds the Id of on existing list, givent its name.
* @param string $list The list name.
* @return mixed The list id if it exists, false if it doesn't and
* -1 in case of an error processing the request.
* Finds the Id of the lists a user is in.
* @param int $userId The user id.
* @return array The list ids
* false in case of an error processing the request.
*/
private function _getListId($list){
private function _getUserLists($userId){

$db = $this->_db;
$list = array();
$sql = "SELECT * FROM phplist_listuser WHERE userid = :id";

$sql = "SELECT * FROM phplist_list WHERE lower(name) = :list";
try{
$db = $this->_db;
$stmt = $db->prepare($sql);
$list = strtolower($list);
$stmt->bindParam("list", $list);
$stmt->bindParam("id", $userId);
$stmt->execute();
$rowCount = $stmt->rowCount();

if($stmt->rowCount() > 0){
$row = $stmt->fetch();
return $row['id'];

}else{
return -1;
while($row = $stmt->fetch()){
$list[] = $row['listid'];
}

return sizeof($list)? $list : false;

}catch(PDOException $e){
return false;
}
}

/**
* Creates a PHPList user with a set of provided properties. If a user with the given
* email address already exists, it returns her id.
* @param array $data Properties with which the user will be created.
* @return int The user id or -1 if there was an error processing the request.
* @return int The user id
* false otherwise.
*/
private function _createUser($data){

$id = $this->_getUserId($data['email']);

$db = $this->_db;
$userId = $this->_getUserId($data['email']);

if($id == -1){

if($userId === false){
// User doesn't exist. Create it.
$sql = "INSERT INTO phplist_user_user (email, confirmed, htmlemail, rssfrequency, password, passwordchanged, disabled, entered, uniqid) VALUES (:email, :confirmed, :htmlemail, :rssfrequency, :password, now(), :disabled, now(), :uniqid);";

try {
$uniqid = md5(uniqid(mt_rand()));
$db = $this->_db;
$stmt = $db->prepare($sql);
$stmt->bindParam("email", $data['email']);
$stmt->bindParam("confirmed", $data['confirmed']);
Expand All @@ -146,125 +157,163 @@ private function _createUser($data){

// Assign id of recently created user.
if($stmt->rowCount() > 0){
$id = $db->lastInsertId();
return $db->lastInsertId();
}else{
$id = -1;
return false;
}
} catch(PDOException $e) {
return false;
}
}

return $id;
return $userId;
}

/**
* Adds a user to a list.
* @param string $email User email.
* @param string $list List name.
* @return mixed false if there was an exception thrown,
* 1 if the user was added successfully
* -1 if user wasn't found,
* -2 if list wasn't found,
* -3 if user already was in the list.
* Deletes a PHPList user.
* @param int $id User id.
* @return int The amount of deleted users
* false if there was an error processing the request.
*/
public function addUserToList( $email, $list ){
private function _deleteUser($userId){

$user_id = $this->_createUser(array('email' =>$email, 'confirmed'=>1));
$list_id = is_int($list)? $list : (ctype_digit($list) ? intval($list) : $this->_getListId($list));
$db = $this->_db;
$sql = "DELETE FROM phplist_user_user WHERE id=:id;";

if( $user_id && $list_id === false ) {
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $userId);
$stmt->execute();
return $stmt->rowCount() > 0;

} catch(PDOException $e) {
return false;
}
}

/**
* Deletes user from some lists
* @param int $user_id User Id.
* @param array $lists Lists to remove the user from.
* @return boolean True if no error was found, false if there was.
*/
private function _deleteUserFromList($userId, $listId){

$db = $this->_db;

if($list_id < 0) return -2;
if($user_id < 0) return -1;

$sql = "INSERT INTO phplist_listuser (userid, listid, entered, modified) VALUES (:user_id, :list_id, now(), now());";
$sql = "DELETE FROM phplist_listuser WHERE userid=:user_id AND listid=:list_id;";

try {
$db = $this->_db;
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $user_id );
$stmt->bindParam("list_id", $list_id );
$stmt->bindParam("user_id", $userId );
$stmt->bindParam("list_id", $listId );
$stmt->execute();

return $stmt->rowCount();
return $stmt->rowCount() >= 0;
} catch(PDOException $e) {
if ($e->getCode() == '23000'){
return -3;
}

return false;
}
}
}

/**
* Removes a user from a list.
* @param string $email User email.
* @param string $list List name.
* @return mixed false if there was an exception thrown,
* -1 if user wasn't found,
* -2 if list wasn't found.
* Delete user.
* @param string $email User email.
* @return boolean
*/
public function removeUserFromList( $email, $list ){
public function deleteUser($email){
$userId = $this->_getUserId($email);
if(!$userId){
return true;
}
$lists = $this->_getUserLists($userId);

$listsDeleted = true;
foreach($lists as $listId){
$listsDeleted = $listsDeleted && $this->_deleteUserFromList($userId, $listId);
}

$user_id = $this->_createUser(array('email' =>$email, 'confirmed'=>1));
$list_id = is_int($list)? $list : (ctype_digit($list) ? intval($list) : $this->_getListId($list));
return $this->_deleteUser($userId);
}

/**
* Adds a user to a list.
* @param string $email User email.
* @param string $list List id.
* @return boolean false if fail,
* true if the user was added successfully
*/
public function addUserToList( $email, $listId ){

if( $user_id && $list_id === false ) {
$db = $this->_db;
$userId = $this->_createUser(array('email' =>$email, 'confirmed'=>1));
if( $userId === false ) {
return false;
}


if($list_id < 0) return -2;
if($user_id < 0) return -1;

$sql = "DELETE FROM phplist_listuser WHERE userid=:user_id AND listid=:list_id;";
$sql = "INSERT INTO phplist_listuser (userid, listid, entered, modified) VALUES (:user_id, :list_id, now(), now());";

try {
$db = $this->_db;
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $user_id );
$stmt->bindParam("list_id", $list_id );
$stmt->bindParam("user_id", $userId );
$stmt->bindParam("list_id", $listId );
$stmt->execute();

return $stmt->rowCount();
return $stmt->rowCount() > 0;
} catch(PDOException $e) {
if ($e->getCode() == '23000'){
return true;
}
return false;
}
}

/**
* Removes a user from a list.
* @param string $email User email.
* @param int $from List origin.
* @param int $to List destination.
* @return mixed false if there was an exception thrown,
* 1 if the user list was updated.
* 0 if no user list was updated.
* @param string $email User email.
* @param string $list List name.
* @return mixed true if it worked
* false if there was an exception thrown,
*/
public function moveUser( $email, $from, $to ){
public function deleteUserFromList( $email, $listId ){

$user_id = $this->_createUser(array('email' =>$email, 'confirmed'=>1));
$userId = $this->_getUserId($email);
if(!$userId){
// there's no user with that email. Fair enough. Return OK.
return true;
}

if( $user_id === false ) {
return $this->_deleteUserFromList($userId, $listId);
}

/**
* Moves a user from a list to another.
* @param string $email User email.
* @param int $from List origin.
* @param int $to List destination.
* @return mixed true if it worked.
* false if there's not such user,
* no user-list combination was found
* or there was an exception thrown,
*/
public function moveUser( $email, $from, $to ){

$db = $this->_db;
$user_id = $this->_getUserId($email);

if(!$user_id) {
return false;
}

$sql = "UPDATE phplist_listuser SET listid=:to_list_id WHERE userid=:user_id AND listid=:from_list_id;";

try {
$db = $this->_db;
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $user_id );
$stmt->bindParam("from_list_id", $from );
$stmt->bindParam("to_list_id", $to );
$stmt->execute();

return $stmt->rowCount();
return $stmt->rowCount() > 0;

} catch(PDOException $e) {
echo $e->getMessage();
return false;
Expand Down
Loading

0 comments on commit 38c33fa

Please sign in to comment.