View source
<?php
function pollim_entity_info() {
$return['pollim'] = array(
'label' => t('Pollim'),
'entity class' => 'Pollim',
'controller class' => 'PollimController',
'base table' => 'pollim',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pollim_id',
'bundle' => 'type',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'creation callback' => 'pollim_create',
'access callback' => 'pollim_access',
'module' => 'pollim',
'admin ui' => array(
'path' => 'admin/content/pollims',
'file' => 'pollim.admin.inc',
'controller class' => 'PollimUIController',
'menu wildcard' => '%pollim',
),
);
$return['pollim_type'] = array(
'label' => t('Pollim Type'),
'entity class' => 'PollimType',
'controller class' => 'PollimTypeController',
'base table' => 'pollim_type',
'fieldable' => FALSE,
'bundle of' => 'pollim',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'pollim_type_access',
'module' => 'pollim',
'admin ui' => array(
'path' => 'admin/structure/pollim_types',
'file' => 'pollim_type.admin.inc',
'controller class' => 'PollimTypeUIController',
),
);
$return['pollim_vote'] = array(
'label' => t('Pollim Vote'),
'entity class' => 'PollimVote',
'controller class' => 'EntityAPIController',
'base table' => 'pollim_vote',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'vote_id',
),
);
return $return;
}
function pollim_entity_info_alter(&$entity_info) {
foreach (pollim_get_types() as $type => $info) {
$entity_info['pollim']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/pollim_types/manage/%pollim_type',
'real path' => 'admin/structure/pollim_types/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array(
'administer pollim types',
),
),
);
}
}
function pollim_permission() {
$permissions = array(
'administer pollim types' => array(
'title' => t('Administer pollim types'),
'description' => t('Create and delete fields for pollim types, and set their permissions.'),
),
'administer pollims' => array(
'title' => t('Administer pollims'),
'description' => t('Edit and delete all polls.'),
),
'view pollim polls' => array(
'title' => t('View polls'),
'description' => t('View all published polls'),
),
'vote pollim polls' => array(
'title' => t('Vote on polls'),
'description' => t('Vote on all published polls.'),
),
);
foreach (pollim_get_types() as $type) {
$type_name = check_plain($type->type);
$permissions += array(
"edit any {$type_name} pollim" => array(
'title' => t('%type_name: Edit any pollim', array(
'%type_name' => $type->label,
)),
),
"view any {$type_name} pollim" => array(
'title' => t('%type_name: View any pollim', array(
'%type_name' => $type->label,
)),
),
);
}
return $permissions;
}
function pollim_access($op, $pollim = NULL, $account = NULL) {
if (user_access('administer pollims', $account)) {
return TRUE;
}
if (isset($pollim) && is_object($pollim) && ($type_name = $pollim->type)) {
$op = $op == 'view' ? 'view' : 'edit';
if (user_access("{$op} any {$type_name} pollim", $account)) {
return TRUE;
}
}
return FALSE;
}
function pollim_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer pollim types', $account);
}
function pollim_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('pollim_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
function pollim_type_load($type) {
return pollim_get_types($type);
}
function pollim_load($pollim_id, $reset = FALSE) {
$pollims = pollim_load_multiple(array(
$pollim_id,
), array(), $reset);
return reset($pollims);
}
function pollim_load_multiple($pollim_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('pollim', $pollim_ids, $conditions, $reset);
}
function pollim_delete(Pollim $pollim) {
$pollim
->delete();
}
function pollim_delete_multiple(array $pollim_ids) {
entity_get_controller('pollim')
->delete($pollim_ids);
}
function pollim_create($values = array()) {
return entity_get_controller('pollim')
->create($values);
}
function pollim_save(Pollim $pollim) {
return $pollim
->save();
}
function pollim_type_save(PollimType $type) {
$type
->save();
}
function pollim_type_delete(PollimType $type) {
$type
->delete();
}
function pollim_uri(Pollim $pollim) {
return array(
'path' => 'pollim/' . $pollim->pollim_id,
);
}
function pollim_page_title(Pollim $pollim) {
return $pollim->name;
}
function pollim_page_view($pollim, $view_mode = 'full') {
$controller = entity_get_controller('pollim');
$content = $controller
->view(array(
$pollim->pollim_id => $pollim,
));
drupal_set_title($pollim->name);
return $content;
}
function pollim_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'pollim') . '/views',
);
}
function pollim_theme() {
return array(
'pollim_add_list' => array(
'variables' => array(
'content' => array(),
),
'file' => 'pollim.admin.inc',
),
'pollim' => array(
'render element' => 'elements',
'template' => 'pollim',
),
'pollim_sample_data' => array(
'variables' => array(
'pollim_sample_data',
'pollim' => NULL,
),
'template' => 'pollim-sample-data',
),
);
}
function pollim_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/content/pollims') {
$item = menu_get_item('admin/content/pollims/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function pollim_menu() {
$items = array();
$items['pollim/ajax/vote/%pollim'] = array(
'title' => '',
'type' => MENU_CALLBACK,
'page callback' => 'pollim_ajax_vote',
'page arguments' => array(
3,
),
'access arguments' => array(
'vote pollim polls',
),
);
$items['admin/config/content/pollim'] = array(
'title' => t('Poll Improved'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pollim_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'pollim.settings.admin.inc',
);
return $items;
}
function pollim_ajax_vote($pollim) {
if (!pollim_ip_check($pollim
->internalIdentifier(), ip_address())) {
print "<p class='messages error'>" . t('IP address blocked') . "</p>";
exit;
}
$choice = $_REQUEST['data'];
$entity = entity_create('pollim_vote', array(
'pollim_id' => $pollim->pollim_id,
'timestamp' => time(),
'hostname' => ip_address(),
'data' => $choice,
));
$entity
->save();
$controller = entity_get_controller('pollim');
$content = $controller
->view(array(
$pollim->pollim_id => $pollim,
));
$output = render($content);
$output .= '<p class="message">' . t('Thanks for your vote!') . '</p>';
print $output;
exit;
}
function pollim_ip_check($pollim_id, $ip_address) {
$ok = TRUE;
if (variable_get('pollim_threshold_count', 10) == 0) {
return TRUE;
}
$votes_for_this_ip = db_query("SELECT COUNT(*) FROM {pollim_vote} " . "WHERE pollim_id = :pollim_id AND hostname = :hostname AND timestamp > :timestamp", array(
':pollim_id' => $pollim_id,
':hostname' => $ip_address,
':timestamp' => time() - variable_get('pollanon_threshold_time', 600),
))
->fetchField(0);
if ($votes_for_this_ip >= variable_get('pollim_threshold_count', 10)) {
$ok = FALSE;
drupal_set_message(t('Voting temporarily closed for your remote address.'));
}
return $ok;
}
function pollim_block_info() {
$blocks['pollim'] = array(
'info' => t('Latest Poll'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
class Pollim extends Entity {
public function __construct($values = array()) {
parent::__construct($values, 'pollim');
}
protected function defaultLabel() {
return $this->name;
}
protected function defaultUri() {
return array(
'path' => 'pollim/' . $this->pollim_id,
);
}
}
class PollimType extends Entity {
public $type;
public $label;
public function __construct($values = array()) {
parent::__construct($values, 'pollim_type');
}
}
class PollimVote extends Entity {
}
class PollimController extends EntityAPIController {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$values += array(
'pollim_id' => '',
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'data' => '',
);
$pollim = parent::create($values);
return $pollim;
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
return $content;
}
}
class PollimTypeController extends EntityAPIControllerExportable {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$values += array(
'id' => '',
'is_new' => TRUE,
'data' => '',
);
$pollim_type = parent::create($values);
return $pollim_type;
}
}