Last active
August 29, 2015 14:26
-
-
Save sb-relaxt-at/36acdad235e64faa6773 to your computer and use it in GitHub Desktop.
silverstripe-codecompetition-2015
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Allows visitors to like each Page once per IP | |
// Code submitted by relaxt confusion labs | |
class Page extends SiteTree { | |
private static $has_many = array( | |
'Likes' => 'Page_Like' | |
); | |
} | |
class Page_Controller extends ContentController { | |
// Enables liking a Page by appending "likethispage" to a Page's url | |
private static $allowed_actions = array( | |
'likethispage' | |
); | |
public function likethispage(SS_HTTPRequest $request) { | |
$ip = $request->getIP(); | |
if($this->Likes()->filter('IP', $ip)->Count() == 0) { | |
$pageLike = Page_Like::create(); | |
$pageLike->IP = $ip; | |
$this->Likes()->add($pageLike); | |
} | |
$this->redirectBack(); | |
} | |
} | |
class Page_Like extends DataObject { | |
private static $db = array( | |
'IP' => 'Varchar(45)' | |
); | |
private static $has_one = array( | |
'LikedPage' => 'Page' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment