Skip to content

Instantly share code, notes, and snippets.

@sb-relaxt-at
Last active August 29, 2015 14:26
Show Gist options
  • Save sb-relaxt-at/36acdad235e64faa6773 to your computer and use it in GitHub Desktop.
Save sb-relaxt-at/36acdad235e64faa6773 to your computer and use it in GitHub Desktop.
silverstripe-codecompetition-2015
<?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