Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2011 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1473421 to your computer and use it in GitHub Desktop.
Save anonymous/1473421 to your computer and use it in GitHub Desktop.
Beginnings of a ProcessWire Vote module
<?php
/**
* ProcessWire VoteUpDown Fieldtype
*
* A field that allows user's to vote on a page with a simple up or down
*
* For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php
* /wire/core/FieldtypeMulti.php
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
/**
* ProcessWire VoteUpDown Fieldtype
*
* A field stores an up or down vote for a page
*
*/
class FieldtypeVoteUpDown extends FieldtypeMulti {
public static function getModuleInfo() {
return array(
'title' => 'Vote Up/Down',
'version' => 101,
'summary' => 'Field that stores up or down votes for a page',
'permanent' => false,
);
}
/**
* Sanitize value for storage
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
return $value;
}
/**
* Return either a blank Page or a blank PageArray
*
*/
public function getBlankValue(Page $page, Field $field) {
return null;
}
/**
* Return the associated Inputfield
* I think this gets the field used when viewing the page in the admin. Would be vote tally here.
*/
public function getInputfield(Page $page, Field $field) {
return null;
}
/**
* When renderMapMarkup is on, this will generate the markup for the map and return it, ready to output.
*
* When renderMapMarkup is off, this will just return the raw data.
*
*/
public function formatValue(Page $page, Field $field, $value) {
// this is where we put the HTML that will render the voting mechanism
$out = '<p>' . $field->prompt . '</p>';
$out .= '<form action="./" method="post" name="vote_form"><input type="submit" name="submit" /><input type="hidden" name="vote" value="1" /></form>';
return $out;
}
/**
* Schema for the VoteUpDown Fieldtype
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['id'] = "int unsigned NOT NULL auto_increment";
$schema['vote'] = "tinyint(3) NOT NULL default '0'";
$schema['created'] = "int unsigned NOT NULL";
$schema['keys']['primary'] = "PRIMARY KEY (`id`)";
return $schema;
}
/**
* Return the fields required to configure an instance of FieldtypeVoteUpDown
*
*/
public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field);
$name = 'prompt';
$f = $this->fuel('modules')->get('InputfieldText');
$f->attr('name', $name);
$f->attr('value', $field->$name);
$f->label = 'Enter the text you\'d like to use as the prompt for the voting';
$f->description = "For example 'Did this content help you understand [topic]?'.";
$inputfields->append($f);
return $inputfields;
}
/**
* Per the Fieldtype interface, Save the given Field from the given Page to the database
*
* @param Page $page
* @param Field $field
* @return bool
*
*/
public function ___savePageField(Page $page, Field $field) {
$sql = "INSERT INTO `{$field->table}` SET pages_id={$page->id}, vote = 1";
if(!$result = $this->db->query($sql)) $this->error("Error saving item");
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment