| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: comment_og.module,v 1.7 2008/04/03 09:42:37 rapsli Exp $
|
| 4 |
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Implementation of hook_node_info()
|
| 8 |
*
|
| 9 |
* @return array
|
| 10 |
*/
|
| 11 |
function comment_og_info() {
|
| 12 |
return array(
|
| 13 |
'comment_og' => array(
|
| 14 |
'name' => t('Comment OG'),
|
| 15 |
'module' => 'comment_og',
|
| 16 |
'description' => t('Displays comments only for og members.'),
|
| 17 |
)
|
| 18 |
);
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_menu
|
| 23 |
*
|
| 24 |
* @param boolean $may_cache
|
| 25 |
* @return array
|
| 26 |
*/
|
| 27 |
function comment_og_menu($may_cache){
|
| 28 |
$items = array();
|
| 29 |
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_perm
|
| 35 |
*
|
| 36 |
* @return array
|
| 37 |
*/
|
| 38 |
function comment_og_perm(){
|
| 39 |
return array('use comment og');
|
| 40 |
}
|
| 41 |
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Form that is being added to the comment form
|
| 45 |
*
|
| 46 |
* @return array
|
| 47 |
*/
|
| 48 |
function comment_og_form(){
|
| 49 |
global $user;
|
| 50 |
$form['comment_og'] = array(
|
| 51 |
'#type' => 'fieldset',
|
| 52 |
'#title' => t('OG Groups Settings'),
|
| 53 |
'#tree' => true,
|
| 54 |
'#description' => t('Select the visibility for the comment. If you don\'t select anything it will automatically be published publicly.'),
|
| 55 |
);
|
| 56 |
|
| 57 |
$options = array('0' => t('Public'));
|
| 58 |
if(!empty($user->og_groups)){
|
| 59 |
foreach ($user->og_groups as $group) {
|
| 60 |
$options[$group['nid']]=$group['title'];
|
| 61 |
$form['comment_og']['visibility'][$group['nid']] = array(
|
| 62 |
'#type' => 'checkbox',
|
| 63 |
'#title' => $group['title'],
|
| 64 |
);
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
$form['comment_og']['visibility'][0] = array(
|
| 69 |
'#type' => 'checkbox',
|
| 70 |
'#title' => t('Public'),
|
| 71 |
);
|
| 72 |
|
| 73 |
return $form;
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* validate the form
|
| 78 |
*
|
| 79 |
* @param array $comment
|
| 80 |
* @return boolean
|
| 81 |
*/
|
| 82 |
function comment_og_form_validate($comment){
|
| 83 |
$comment_og = $comment['comment_og']['visibility'];
|
| 84 |
$tmp = array();
|
| 85 |
foreach ($comment_og as $key => $value) {
|
| 86 |
if($key != 0){
|
| 87 |
$tmp[] = $value;
|
| 88 |
}
|
| 89 |
}
|
| 90 |
if(in_array(1,$tmp) && $comment_og[0] == 1){
|
| 91 |
form_set_error('comment_og_perm',t('You can either select Public or in a group'));
|
| 92 |
return false;
|
| 93 |
}
|
| 94 |
return true;
|
| 95 |
}
|
| 96 |
|
| 97 |
|
| 98 |
/**
|
| 99 |
* implementation of hook_form_alter
|
| 100 |
*
|
| 101 |
* @param int $form_id
|
| 102 |
* @param array $form
|
| 103 |
*/
|
| 104 |
function comment_og_form_alter($form_id,&$form){
|
| 105 |
//debugger($form_id);
|
| 106 |
switch ($form_id) {
|
| 107 |
case 'comment_form':
|
| 108 |
$form = array_merge($form,comment_og_form());
|
| 109 |
break;
|
| 110 |
|
| 111 |
default:
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* implement comment hook and check the publish status
|
| 118 |
*/
|
| 119 |
function comment_og_comment($comment, $op) {
|
| 120 |
global $user;
|
| 121 |
//debugger('ogc '.$op);
|
| 122 |
switch ($op) {
|
| 123 |
case 'validate':
|
| 124 |
// debugger($comment);
|
| 125 |
return comment_og_form_validate($comment);
|
| 126 |
break;
|
| 127 |
case 'view':
|
| 128 |
$comment = comment_og_display_comment($comment);
|
| 129 |
|
| 130 |
if($comment == -1){
|
| 131 |
$comment->name = "private";
|
| 132 |
}
|
| 133 |
break;
|
| 134 |
case 'form':
|
| 135 |
break;
|
| 136 |
|
| 137 |
case 'delete':
|
| 138 |
$sql = "DELETE FROM {comment_og} WHERE cid = %d";
|
| 139 |
db_query($sql, $comment->cid);
|
| 140 |
break;
|
| 141 |
case 'update':
|
| 142 |
|
| 143 |
$sql = "SELECT count(cid) FROM {comment_og} WHERE cid=%d";//incase a comment is edited but is not in the db yet, it's being added
|
| 144 |
$result = db_query($sql,$comment['cid']);
|
| 145 |
$row = db_fetch_array($result);
|
| 146 |
if($row['count(cid)']==0){
|
| 147 |
_comment_og_insert($comment['cid'],$comment['comment_og']['visibility']);
|
| 148 |
break;
|
| 149 |
}
|
| 150 |
$ser_groups = _comment_serialize_ogs($comment['comment_og']['visibility']);
|
| 151 |
$sql = "UPDATE {comment_og} set visibility=%d, ogs='%s' WHERE cid = %d";
|
| 152 |
db_query($sql, $ser_groups['vis'],$ser_groups['serOgs'],$comment['cid']);
|
| 153 |
// print"te";debugger($ser_groups);exit("te");
|
| 154 |
break;
|
| 155 |
case 'insert':
|
| 156 |
//debugger($comment);
|
| 157 |
_comment_og_insert($comment['cid'],$comment['comment_og']['visibility']);
|
| 158 |
break;
|
| 159 |
}
|
| 160 |
}
|
| 161 |
|
| 162 |
/**
|
| 163 |
* prepare all the need values for insertion in db: serialize the groups the comment is published in
|
| 164 |
* and then make the flag if it's public or not.
|
| 165 |
*
|
| 166 |
* @param unknown_type $comment_og
|
| 167 |
* @return array
|
| 168 |
*/
|
| 169 |
function _comment_serialize_ogs($comment_og){
|
| 170 |
$ogs = array();
|
| 171 |
$public = 0;
|
| 172 |
|
| 173 |
if($comment_og[0]==1){ //this is the form with the public if it's selected we don't have to care about the rest.
|
| 174 |
$public = 1;
|
| 175 |
}else{
|
| 176 |
foreach ($comment_og as $key => $value) {
|
| 177 |
if($value == 1){
|
| 178 |
$ogs[] = $key;
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
if (empty($ogs)) {//if nothing is checked
|
| 183 |
$public = 1;
|
| 184 |
}
|
| 185 |
$ar_return['serOgs'] = serialize($ogs);
|
| 186 |
$ar_return['vis'] = $public;
|
| 187 |
return $ar_return;
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* helper function. Inserts a new visibility for a comment
|
| 192 |
*
|
| 193 |
* @param array $comment
|
| 194 |
*/
|
| 195 |
function _comment_og_insert($cid,$ogs){
|
| 196 |
//debugger($cid);
|
| 197 |
$ogs = _comment_serialize_ogs($ogs);
|
| 198 |
$sql = "INSERT INTO {comment_og}
|
| 199 |
(cid,visibility,ogs)
|
| 200 |
VALUES (%d,%d,'%s')";
|
| 201 |
db_query($sql,$cid,$ogs['vis'],$ogs['serOgs']);
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Before a comment is displayed this function is called.
|
| 206 |
* It checks if a comment is public or if it belongs to a group
|
| 207 |
*
|
| 208 |
* @param object $comment
|
| 209 |
* @return object $comment
|
| 210 |
*/
|
| 211 |
function comment_og_display_comment($comment){
|
| 212 |
global $user;
|
| 213 |
$og_groups = $user->og_groups;
|
| 214 |
|
| 215 |
$sql = "SELECT visibility,ogs FROM {comment_og} WHERE cid=%d";
|
| 216 |
$result = db_query($sql,$comment->cid);
|
| 217 |
$row = db_fetch_object($result);
|
| 218 |
|
| 219 |
if(db_num_rows($result) == 0){//incase it's an old comment that isn't in the comment_og table it will be public automatically.
|
| 220 |
$comment->visibility = 1;
|
| 221 |
return '';
|
| 222 |
}
|
| 223 |
|
| 224 |
if (empty($og_groups) && !comment_og_is_public($comment) && $comment->uid != $user->uid) {
|
| 225 |
$comment->comment = t('This comment is private.');
|
| 226 |
$comment->subject = t('private');
|
| 227 |
$comment->name = t('private');
|
| 228 |
$comment->visibility = -1;
|
| 229 |
return '';
|
| 230 |
}
|
| 231 |
if(is_array($og_groups)){
|
| 232 |
$gid = array(); //collect all the groups of a user
|
| 233 |
foreach ($og_groups as $group) {
|
| 234 |
$gid[] = $group['nid'];
|
| 235 |
}
|
| 236 |
|
| 237 |
$comment->visibility = $row->visibility;
|
| 238 |
// $comment->comment_og->visibility = unserialize($row->ogs);
|
| 239 |
$ar_og_visibility = $comment->comment_og->visibility;
|
| 240 |
|
| 241 |
$ar_tmp = _comment_og_reform_og_groups_array($user->og_groups);
|
| 242 |
|
| 243 |
if(is_array($ar_og_visibility)){
|
| 244 |
$res = array_diff($ar_og_visibility,$ar_tmp);
|
| 245 |
}else{
|
| 246 |
$res = array();
|
| 247 |
}
|
| 248 |
|
| 249 |
if($comment->visibility == 1 || $row->visibility == ''){
|
| 250 |
$comment->visibility = 1;
|
| 251 |
}
|
| 252 |
if(empty($res) || sizeof($res) < sizeof($ar_tmp)){
|
| 253 |
$comment->visibility = 1;
|
| 254 |
}else{
|
| 255 |
$comment->comment= t('This comment is private');
|
| 256 |
$comment->title = t('private');
|
| 257 |
$comment->visibility = -1;
|
| 258 |
}
|
| 259 |
|
| 260 |
}
|
| 261 |
}
|
| 262 |
|
| 263 |
/**
|
| 264 |
* Helper function. Just resorts the array and puts it into the proper order.
|
| 265 |
*
|
| 266 |
* @param array $array
|
| 267 |
* @return array
|
| 268 |
*/
|
| 269 |
function _comment_og_reform_og_groups_array($array){
|
| 270 |
$return = array();
|
| 271 |
foreach ($array as $key => $value) {
|
| 272 |
$return[] = $key;
|
| 273 |
}
|
| 274 |
return $return;
|
| 275 |
}
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
/**
|
| 280 |
* check if comment is public
|
| 281 |
* 1 = public
|
| 282 |
* 0 = only for groups
|
| 283 |
*
|
| 284 |
* @param comment $comment
|
| 285 |
* @return int visibility
|
| 286 |
*/
|
| 287 |
function comment_og_is_public($comment){
|
| 288 |
|
| 289 |
$sql = "SELECT visibility FROM {comment_og} WHERE cid=%d";
|
| 290 |
$result = db_query($sql,$comment->cid);
|
| 291 |
$row = db_fetch_object($result);
|
| 292 |
return $row->visibility;
|
| 293 |
}
|