Advertisement
edutrul

Hidden author module

Jul 22nd, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  * Shows you the option to publish a node as anonymous depending on permissions.
  5.  */
  6.  
  7. /**
  8.  * Implements hook_form_BASE_FORM_ID_alter().
  9.  */
  10. function hidden_author_form_node_form_alter(&$form, $form_state) {
  11.   // Get weight.
  12.   $weight = variable_get('hidden_author_node_weight', -4);
  13.   // Get content types enabled that were saved in hidden_author settings.
  14.   $content_types = variable_get('hidden_author_content_types', array('article' => 'article'));
  15.  
  16.   if (isset($content_types[$form['#node']->type])) {
  17.     if (user_access('post anonymous nodes')) {
  18.       $form['anonymous'] = array(
  19.         '#type' => 'checkbox',
  20.         '#title' => t('Post anonymously'),
  21.         '#weight' => $weight,
  22.         '#default_value' => hidden_author_get_author_id('node', $form['#node']) ? TRUE : FALSE,
  23.       );
  24.     }
  25.   }
  26. }
  27.  
  28. /**
  29.  * Implements hook_node_insert().
  30.  */
  31. function hidden_author_node_insert($node) {
  32.   // Checks if anonymous is enabled.
  33.   if ($node->anonymous) {
  34.     db_insert('hidden_author')
  35.       ->fields(array(
  36.         'entity_type' => 'node',
  37.         'bundle' => $node->type,
  38.         'entity_id' => $node->nid,
  39.         'revision_id' => $node->vid,
  40.         'uid' => $node->hidden_author,
  41.       ))
  42.       ->execute();
  43.   }
  44. }
  45.  
  46. /**
  47.  * Implements hook_node_update().
  48.  */
  49. function hidden_author_node_update($node) {
  50.   // Checks if anonymous is enabled.
  51.   if (!$node->anonymous) {
  52.     hidden_author_node_delete($node);
  53.   }
  54.   elseif (!hidden_author_get_author_id('node', $node)) {
  55.     hidden_author_node_insert($node);
  56.   }
  57. }
  58.  
  59. /**
  60.  * Implements hook_node_presave().
  61.  */
  62. function hidden_author_node_presave($node) {
  63.   // Checks if anonymous is enabled.
  64.   $is_hidden = hidden_author_get_author_id('node', $node);
  65.   if (!$is_hidden && $node->anonymous) {
  66.   $node->hidden_author = $node->uid;
  67.   $node->uid = 0;
  68.   $node->name = t('Anonymous');
  69.   }
  70.   elseif ($is_hidden) {
  71.   if ($node->anonymous) {
  72.     $node->hidden_author = $is_hidden['uid'];
  73.     }
  74.   else {
  75.     $author = user_load($is_hidden['uid']);
  76.       $node->uid = $author->uid;
  77.       $node->name = $author->name;
  78.     }
  79.   }
  80. }
  81.  
  82. /**
  83.  * Implements hook_node_delete().
  84.  */
  85. function hidden_author_node_delete($node) {
  86.   // Deletes a single row in hidden_author table.
  87.   db_delete('hidden_author')
  88.     ->condition('entity_type', 'node', '=')
  89.     ->condition('bundle', $node->type, '=')
  90.     ->condition('entity_id', $node->nid, '=')
  91.     ->condition('revision_id', $node->vid, '=')
  92.     ->execute();
  93. }
  94.  
  95. /**
  96.  * Get the author uid and entity id for given entity.
  97.  */
  98. function hidden_author_get_author_id($entity_type, $object) {
  99.   if ($entity_type == 'node' && empty($object->nid)) {
  100.     return;
  101.   }
  102.  
  103.   // Build query.
  104.   $res = db_select('hidden_author', 'e')
  105.     ->fields('e', array('entity_id', 'uid'))
  106.     ->condition('entity_type', $entity_type, '=')
  107.   ;
  108.  
  109.   if ($entity_type == 'node') {
  110.     $res
  111.       ->condition('bundle', $object->type, '=')
  112.       ->condition('entity_id', $object->nid, '=')
  113.       ->condition('revision_id', $object->vid, '=');
  114.   }
  115.  
  116.   $db_result = $res
  117.     ->execute()
  118.     ->fetchAssoc();
  119.  
  120.   return $db_result;
  121. }
  122.  
  123. /**
  124.  * Implements hook_menu().
  125.  */
  126. function hidden_author_menu() {
  127.   $items['admin/config/content/hidden_author-rules'] = array(
  128.     'title' => 'hidden author settings',
  129.     'description' => 'This page provides hidden author settings.',
  130.     'page callback' => 'drupal_get_form',
  131.     'page arguments' => array('hidden_author_list_content_type'),
  132.     'access callback' => 'user_access',
  133.     'access arguments' => array('administer hidden_author'),
  134.     'file' => 'hidden_author.admin.inc',
  135.     'type' => MENU_NORMAL_ITEM,
  136.   );
  137.   return $items;
  138. }
  139.  
  140. /**
  141.  * Implements hook_help().
  142.  */
  143. function hidden_author_help($path, $arg) {
  144.   switch ($path) {
  145.     case 'admin/config/content/hidden_author-rules':
  146.       return t('this is some help text for Hidden Author module');
  147.     case 'admin/help#hidden_author';
  148.       return _hidden_author_help();
  149.   }
  150. }
  151.  
  152. /**
  153.  * Callback function for hidden_author_help().
  154.  */
  155. function _hidden_author_help() {
  156.   $file = drupal_get_path('module', 'hidden_author') . '/README.txt';
  157.   if (file_exists($file)) {
  158.     return _filter_autop(check_plain(file_get_contents($file)));
  159.   }
  160. }
  161.  
  162. /**
  163.  * Implements hook_permission().
  164.  */
  165. function hidden_author_permission() {
  166.   return array(
  167.     'administer hidden author' => array(
  168.       'title' => t('Administer Hidden Author'),
  169.       'description' => t('Allows you to configure Hidden Author settings.'),
  170.     ),
  171.     'post anonymous nodes' => array(
  172.       'title' => t('Post anonymous nodes'),
  173.       'description' => t('Allows you to post anonymous nodes.'),
  174.     ),
  175.     'view hidden author' => array(
  176.       'title' => t('View Hidden Author'),
  177.       'description' => t('Allows you to view Hidden Author.'),
  178.     ),
  179.   );
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement