shared with Droplr
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

class WishController extends Controller {

	public static $allowed_actions = array(
        'index','vote','view','MakeAWishForm','RegistrationForm','stats'
    );

    public function init() {
        parent::init();

    }
    
    public function PageTitle(){
	    return "The Unofficial Pebble Feature Wish List";
    } 
    
    public function getSort() {
    	// Default sort:
	    $sort = array("label"=>"created", "sql" => "Created DESC");
     	
     	if(isset($_GET['sort'])) {
	     	if ($_GET['sort'] == 'votes') {
		     	
		     	$sort = array("label"=>"votes", "sql" => "NoVotes DESC");
	     	} 
	     }
	     return $sort;
    }	
    
    public function isSortedBy($field = null) {
    	$sortedBy = $this->getSort();
	    if($field == $sortedBy['label']) {
		    return "active";
	    }
	    return null;
    }
    
     public function index() {
	     $sortedBy = $this->getSort();
         $wishes = Wish::get()->sort($sortedBy['sql']);
         $data = array(
         	"Wishes" => $wishes
         );
	  return $this->customise($data)->renderWith(array('WishController','Page'));
    }
    
    public function vote() {
    	if (Member::currentUserID() && $this->UserVotesLeftCount() > 0) {
	    	$canvote = true;
	    	
    	} else {
	    	$canvote = false;
    	}
    	
    	
    	if ($canvote) {
		    $wishId = $this->request->param('ID');
		    
		    $vote = new Vote();
			$vote->WishID = $wishId;
			$vote->MemberID = Member::currentUserID();
			$vote->write();
			
			$wish = Wish::get()->byID($wishId);
			$wish->updateVoteCount();
			$wish->write();
			
			$this->setFlash("Thank you for your vote!","success");
			Controller::redirect($this->request->getHeader('Referer')."#vote".$wish->ID);
		} else {
			$this->setFlash("Sorry, you've run out of votes! You'll be topped up next month.","alert");
			Controller::redirectBack();
		}
		
    }
            
    public function MakeAWishForm() {
    
    	$dropDownField = new DropdownField(
            'WishCategoryID',
            'Where does this wish need to be implemented?',
            WishCategory::get()->map()
        );
    	
	    $fields = new FieldList(
	    	TextField::create("Title","Wish Title"),
        	TextareaField::create("Description","Wish Description"),
        	$dropDownField
	    );
        $actions = new FieldList(
        	 FormAction::create("SubmitAWish")->setTitle("Submit Wish")
        );
        
        $validator = new RequiredFields(array('Title','Description'));
        
        $form = new Form($this, 'MakeAWishForm', $fields, $actions, $validator);
        
        $fields->dataFieldByName('Title')->addExtraClass('required');
        $fields->dataFieldByName('Description')->addExtraClass('required');
        
        return $form;
    }
    
    public function SubmitAWish($data, Form $form) {
	    $wish = new Wish();
	    $wish->Title = $data['Title'];
	    $wish->Description = $data['Description'];
	    $wish->CategoryID = $data['CategoryID'];
	    $wish->MemberID = Member::currentUserID();
	    $wish->write();
	    $this->setFlash("Thank you for your wish!","success");
	    Controller::redirect($wish->Link());
    }
    
    public function view() {
	    if($wishId = $this->request->param('ID')) {
		    if($wish = Wish::get()->byID($wishId)) {
			    $wish = $wish;
		    }
		    return $this->customise(array(
		    	"Wish"=>$wish,
		    	"Link"=>$wish->Link(),
		    	"PageTitle" => $wish->Title
		    ))->renderWith(array("WishController_view","Page"));
	    }
    }
    
    
    
    public function UserVoteCount() {
	    return Vote::get()->filter(array("MemberID" => Member::currentUserID()))->count();
    }
    
    public function UserVotesLeftCount() {
    	$userVotes = Vote::get()->filter(array("MemberID" => Member::currentUserID()))->where("YEAR(Created) = YEAR(CURDATE()) AND MONTH(Created) = MONTH(CURDATE())")->count();
    	$allowedVotes = 10;
	    return $allowedVotes - $userVotes;
    }
    
    public function DaysLeft() {
	    $days = date('t') - date('j') + 1;
	    if ($days == 1) {
	    	return $days." day";
	    } else {
		    return $days." days";
	    }
    }
    
    public function stats() {
	    $noUsers = Member::get()->count();
	    $noVotes = Vote::get()->count();
	    $noWishes = Wish::get()->count();
	    $noComments = Comment::get()->count();
	    return $noUsers." users<br />".$noVotes." votes<br />".$noWishes." wishes<br />".$noComments." comments";
    }
    
    public function LoggedIn() {
	    return Member::currentUserID() ? true : false ;
    }  
        
    function Link() {
	    return "/";
    }
	
}