Skip to content

Commit

Permalink
v1.3 beta 1
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfieZero committed Oct 12, 2011
1 parent dd21af7 commit b5e8988
Show file tree
Hide file tree
Showing 17 changed files with 708 additions and 292 deletions.
7 changes: 7 additions & 0 deletions css/admin.css
@@ -0,0 +1,7 @@
.error{
background: rgba(175,0,0,0.6);

}
body{
background: red;
}
162 changes: 89 additions & 73 deletions lib/admin.php
Expand Up @@ -6,10 +6,16 @@ class SimplyPollAdmin extends SimplyPoll{

public function __construct(){

parent::__construct(false);
add_action('admin_menu', array($this, 'addSimplyPollMenu'));
add_action('admin_init', array($this, 'enqueueFiles'));

}

public function enqueueFiles(){
wp_enqueue_script('jquery');
wp_enqueue_script('jSimplyPollAdmin', plugins_url('/js/simplypoll-admin.js', SP_FILE));
}

/**
* Add menu items to admin
Expand Down Expand Up @@ -49,16 +55,19 @@ public function getAdminPageDelete(){
}


/**
* Add New Poll
/**************************************************************************
* Add or Edit a Poll
*
* @param $pollEdit
* @param array $pollData
* @return array
*/
public function setEdit($pollData){

$question = $pollData['question'];
$answers = $pollData['answers'];
$posted = $pollData;
$countAnswers = 0;
$error = array();
$newPoll = false;
$editPoll = false;

Expand All @@ -75,111 +84,124 @@ public function setEdit($pollData){

// Check to see if all required fields are entered


// Does question have a value?
if($question){
if( $question ) {

foreach($answers as $key => $aData) {
$pollForDB['question'] = $question;
$pollForDS['question'] = stripcslashes($question);
unset($pollData['question']);

} else {
$error[] = 'No question given';
}


// Do we have answers
if( $answers ) {

$cntAnswers = 0;

// Sort the data out
foreach($answers as $key => $answer) {

// Unset either way to clean the array
unset($pollData['answers'][$key]);

if($aData['answer']){
if($answer['answer']){
// We have an answer so build that back into the array with new values
++$countAnswers;
++$cntAnswers;

if(isset($aData['vote'])){
$vote = $aData['vote'];
// Add vote node if not there already
if(isset($answer['vote'])){
$vote = $answer['vote'];
} else {
$vote = 0;
}

$pollData['answers'][$key]['answer'] = $aData['answer'];
$pollData['answers'][$key]['vote'] = $vote;
$pollForDB['answers'][$cntAnswers]['answer'] = $answer['answer'];
$pollForDB['answers'][$cntAnswers]['vote'] = $vote;
$pollForDS['answers'][$cntAnswers]['answer'] = stripcslashes($answer['answer']);
$pollForDS['answers'][$cntAnswers]['vote'] = $vote;
}

}

// Quick clean of the array node
unset($pollData['answers']);

if($countAnswers > 1) {
$poll = $pollData;

if(isset($addPoll)){
$poll['added'] = time();
$poll['active'] = true;
$poll['totalvotes'] = 0;
unset($poll['addPoll']);

} else {
unset($poll['editPoll']);

}

$poll['updated'] = time();

} else {
// I like humor, what of it?
if($countAnswers == 1){
$error = 'Not enough answers; a question isn\'t a question with one answer!';
} else {
$error = 'No answers; what the sound of one tree clapping?';
}
// Do we have enough answers
if( $cntAnswers <= 1 ) {
$error[] = 'Need at least 2 answers';
}

} else {
$error = 'No question given; how can somebody answer this question, this ain\'t Jepoardy!';
$error[] = 'No answers given';
}

if(isset($error)) {
$return = $error;

} else {
if(isset($addPoll)) {
if($this->addPollToDB($poll, $editPoll)) {
$return = 'success';
} else {
$return = 'adding to the DB failed';
}

// If we have no error then all good to go
if( count($error) == 0 ) {

if( isset($addPoll) ) {

} elseif($editPoll) {
if($this->addPollToDB($poll, $editPoll)) {
$return = '';
$pollID = $this->addPollToDB($pollForDB, false);
if($pollID) {
header('/admin.php?page=sp-edit&id='.$pollID);
} else {
$return = 'adding to the DB failed';
$error = 'adding to the DB failed';
}

} else {
unset($poll['editPoll']);
}

$poll['updated'] = time();


return $pollForDS;

} else {

$pollForDS['error'] = $error;
return $pollForDS;

}

$this->pollEdit['poll'] = $posted;
$this->pollEdit['response'] = $return;


}
/**
* Add New Poll Return


/**************************************************************************
* Delete Poll
*
* @return string
* $param int $id
*/
public function getEdit(){
return $this->pollEdit;
}

public function deletePoll($id){
global $wpdb;

$wpdb->query("DELETE FROM `".SP_TABLE."` WHERE `id`='".$id."'");

$wpdb->query('
DELETE FROM
`'.SP_TABLE.'`
WHERE `id`="'.$id.'"
');
return true;
}

/**

/**************************************************************************
* Add New Poll to DB
*
* @param $poll
* @return bool
* @return bool or int
*/
private function addPollToDB($poll,$editPoll){
private function addPollToDB($poll, $editPoll){

$pollData = parent::getPollDB();

$poll['added'] = time();
$poll['active'] = true;
$poll['totalvotes'] = 0;

$pollData['polls'][] = $poll;

if ($editPoll == true) {
Expand All @@ -190,10 +212,4 @@ private function addPollToDB($poll,$editPoll){
return parent::newPollDB($poll);
}
}


public function enqueueFiles(){
wp_enqueue_script('jquery');
wp_enqueue_script('jSimplyPollAdmin', plugins_url('/js/simplypoll-admin.js', SP_FILE));
}
}

0 comments on commit b5e8988

Please sign in to comment.