/[drupal]/contributions/modules/authorship/authorship.module
ViewVC logotype

Contents of /contributions/modules/authorship/authorship.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Sat Oct 11 12:22:16 2008 UTC (13 months, 1 week ago) by karpuz
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.7: +19 -44 lines
File MIME type: text/x-php
#231227 by Matt B, upgrade to Drupal 6
1 <?php
2 // $Id: authorship.module,v 1.4.2.5 2007/07/12 22:09:08 karpuz Exp $
3
4 /**
5 * @file
6 * Implementation of authorship node extention
7 *
8 * Written under bounty for :-
9 * brashquido (http://drupal.org/user/49838)
10 * by AjK (http://drupal.org/user/39030)
11 * June 2006
12 *
13 * v6.x-dev written by matt_b
14 */
15
16 if(!defined("_AUTHORSHIP_DEBUG")) {
17 define("_AUTHORSHIP_DEBUG", 0); // 0 - disabled, 1 - enabled
18 }
19
20 /* {{{ authorship_help() */
21 /**
22 * Implementation of hook_help().
23 */
24 function authorship_help($path, $arg) {
25 switch ($path) {
26 case 'admin/modules#description':
27 return t('Node authorship extention module.');
28 }
29 }
30 /* }}} */
31
32 /* {{{ authorship_perm() */
33 /**
34 * Implementation of hook_perm()
35 */
36 function authorship_perm() {
37 return array(
38 'admin authorship',
39 'user use authorship'
40 );
41 }
42 /* }}} */
43
44 /* {{{ authorship_form_alter() */
45 /**
46 * Implementation of hook_form_alter()
47 */
48 function authorship_form_alter(&$form, &$form_state, $form_id) {
49 global $user;
50
51 _authorship_debug("authorship_form_alter(" . $form['#node']->nid . ")");
52
53 if ($form_id == 'node_type_form' && isset($form['#node_type'])) {
54 $form['workflow']['authorship']['authorship_markup_'. $form['#node_type']->type] = array(
55 '#type' => 'markup',
56 '#value' => '<b>' . t('The following controls the authorship module settings') . '</b><p/>'
57 );
58 $form['workflow']['authorship']['authorship_real_name'] = array(
59 '#type' => 'textfield',
60 '#title' => t('The profile variable name used to store the real name'),
61 '#default_value' => variable_get('authorship_real_name_'. $form['#node_type']->type, 'profile_real_name_'. $form['#node_type']->type),
62 '#description' => t('Ensure the value entered here matches the variable '.
63 'name that was given to the Real Name value in the '.
64 'profile module settings area.')
65 );
66 $form['workflow']['authorship']['authorship'] = array(
67 '#type' => 'radios',
68 '#title' => t('Enable authorship module functionality'),
69 '#default_value' => variable_get('authorship_'. $form['#node_type']->type, 0), // . $form['#node_type']->type, 0),
70 '#options' => array(t('Disabled'), t('Enabled'))
71 );
72 }
73
74 if (isset($form['#id']) && $form['#id'] == 'node-form') {
75 if (variable_get('authorship_'. $form['#node']->type, 0)) {
76 $access = (user_access('user use authorship') && $user->uid == $form['#node']->uid) ? TRUE : FALSE;
77 if ($user->uid == 1 || user_access('admin authorship') || $access) {
78 $form['author']['authorship'] = array(
79 '#type' => 'textfield',
80 '#title' => t('Authorship display setting'),
81 '#default_value' => $form['#node']->authorship,
82 '#weight' => 9,
83 '#description' => t('The name to display as the author. '.
84 'Leave blank for normal operation')
85 );
86 }
87 }
88 }
89
90 if(isset($form['#id']) && $form['#id'] == 'comment-form' ) {
91 if (variable_get('authorship_comment', 0)) {
92 $row = db_fetch_object(db_query("SELECT authorship FROM {node_authorship} WHERE cid = %d", $form['cid']['#value']));
93 $authorship = $row->authorship;
94
95 if ($user->uid == 1 || user_access('admin authorship') || user_access('user use authorship')) {
96 $form['authoring'] = array(
97 '#type' => 'fieldset',
98 '#title' => t('Authoring information'),
99 '#weight' => 9,
100 '#collapsible' => TRUE,
101 '#collapsed' => FALSE,
102 );
103 $form['authoring']['authorship'] = array(
104 '#type' => 'textfield',
105 '#title' => t('Authorship display setting'),
106 '#default_value' => $authorship,
107 '#description' => t('The name to display as the author. '.
108 'Leave blank for normal operation')
109 );
110 }
111 }
112 }
113
114 if(isset($form['#id']) && $form['#id'] == 'comment-admin-settings' ) {
115 $form['posting_settings']['authorship_real_name_comment'] = array(
116 '#type' => 'textfield',
117 '#title' => t('The profile variable name used to store the real name'),
118 '#default_value' => variable_get('authorship_real_name_comment', 'profile_real_name_comment'),
119 '#description' => t('Ensure the value entered here matches the variable '.
120 'name that was given to the Real Name value in the '.
121 'profile module settings area.')
122 );
123 $form['posting_settings']['authorship_comment'] = array(
124 '#type' => 'radios',
125 '#title' => t('Enable authorship module functionality'),
126 '#default_value' => variable_get('authorship_comment', 0),
127 '#options' => array(t('Disabled'), t('Enabled')),
128 );
129 }
130 }
131 /* }}} */
132
133 /* {{{ authorship_nodeapi() */
134 /**
135 * Implementation of hook_nodeapi()
136 */
137 function authorship_nodeapi(&$node, $op, $teaser, $page) {
138
139 _authorship_debug("authorship_nodeapi(" . $node->nid . ", $op)");
140
141 switch ($op) {
142 case 'load':
143 $sql = "SELECT authorship, profile_real_name ".
144 "FROM {node_authorship} WHERE nid = %d AND cid = 0";
145 $row = db_fetch_object(db_query($sql, $node->nid));
146 $node->authorship = $row->authorship;
147 $node->profile_real_name = $row->profile_real_name;
148 break;
149 case 'insert':
150 case 'update':
151 db_query("DELETE FROM {node_authorship} WHERE nid = %d AND cid = 0 LIMIT 1", $node->nid);
152 if($node->authorship != "" || $node->profile_real_name != "") db_query("INSERT INTO {node_authorship} (nid, authorship, profile_real_name) VALUES (%d, '%s', %d)",
153 $node->nid, $node->authorship, $node->profile_real_name);
154 break;
155 case 'delete':
156 db_query("DELETE FROM {node_authorship} WHERE nid = %d AND cid = 0 LIMIT 1", $node->nid);
157 break;
158 case 'view':
159 if ($page || $teaser) {
160 if ($name_rewrite = _authorship_name_rewrite($node)) {
161 $temp = $node->name;
162 $node->name = $name_rewrite;
163 $node->authorship = $temp;
164 }
165 }
166 break; // end 'view'
167 case 'update index':
168 return db_result(db_query("SELECT authorship FROM {node_authorship} WHERE nid = %d", $node->nid));
169 break;
170 case 'search result':
171 if ($node->authorship) return array('user' => $node->authorship);
172 break;
173
174 } // end switch
175 }
176 /* }}} */
177
178 /* {{{ authorship_comment() */
179 /**
180 * Implementation of hook_comment()
181 */
182 function authorship_comment(&$comment, $op) {
183 //echo "$op <pre>"; print_r($comment); echo "</pre><hr/>";
184
185 switch ($op) {
186 case 'insert':
187 case 'update':
188 db_query("DELETE FROM {node_authorship} WHERE cid = %d LIMIT 1", $comment['cid']);
189 if($comment['authorship'] != "" || $comment['profile_real_name'] != "") db_query("INSERT INTO {node_authorship} (nid, cid, authorship, profile_real_name) VALUES (%d, %d, '%s', %d)",
190 $comment['nid'], $comment['cid'], $comment['authorship'], $comment['profile_real_name']);
191 break;
192 case 'delete':
193 db_query("DELETE FROM {node_authorship} WHERE cid = %d LIMIT 1", $comment->cid);
194 break;
195 case 'view':
196 // no 'load' option for $op in hook_comment, therefore we get the authorship info from the DB here
197 if(!isset($comment->authorship) && $comment->cid != "" ) {
198 $sql = "SELECT authorship, profile_real_name ".
199 "FROM {node_authorship} WHERE cid = %d";
200 $row = db_fetch_object(db_query($sql, $comment->cid));
201 $comment->authorship = $row->authorship;
202 $comment->profile_real_name = $row->profile_real_name;
203 }
204 $comment->type = 'comment';
205 if ($name_rewrite = _authorship_name_rewrite($comment)) {
206 $temp = $comment->name;
207 $comment->name = $name_rewrite;
208 $comment->authorship = $temp;
209 }
210 break; // end 'view'
211 } // end switch
212 }
213 /* }}} */
214
215 /**
216 * Implement hook_views_api().
217 */
218 function authorship_views_api() {
219 return array(
220 'api' => 2.0,
221 );
222 }
223
224 /*=========================*/
225 /* Helper functions follow */
226 /*=========================*/
227
228 /* {{{ _authorship_name_rewrite() */
229 /**
230 * _authorship_name_rewrite()
231 *
232 * Decide if a node->name needs rewriting
233 *
234 * @param $node
235 * node object
236 *
237 * @return mixed
238 * string to use for rewrite
239 * false on no rewrite to take place
240 */
241 function _authorship_name_rewrite($node) {
242
243 $local_name_rewrite = FALSE; // prepare default is false
244
245 if (!(int)variable_get('authorship_' . $node->type, 0)) {
246 return FALSE; // module not enabled for this content-type
247 }
248
249 $node_user = user_load(array('uid' => $node->uid));
250 $field = variable_get('authorship_real_name_'. $node->type, 'profile_real_name_'. $node->type);
251
252 if (isset($node->authorship) && strlen($node->authorship) > 0) {
253 $local_name_rewrite = $node->authorship;
254 }
255 elseif(isset($node_user->$field) && strlen($node_user->$field) > 0) {
256 $local_name_rewrite = $node_user->$field;
257 }
258
259 return $local_name_rewrite;
260 }
261 /* }}} */
262
263 /* {{{ _authorship_debug() */
264 function _authorship_debug($s) {
265 if (defined("_AUTHORSHIP_DEBUG") && _AUTHORSHIP_DEBUG) {
266 error_log($s);
267 }
268 }
269 /* }}} */
270
271

  ViewVC Help
Powered by ViewVC 1.1.2