newsletter authors
| Project: | Simplenews |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | joehudson |
| Status: | needs work |
Jump to:
hi, I wrote this and thought someone might find it useful. (I've not included a patch file, because my version of simplenews is highly customized in other areas) Basically the idea is to allow different authors (users) for different newsletters rather than all the newsletters being available for all users with a role which allows access to the newsletter settings pages (as is the current behaviour).
I added these functions to simplenews.module
function simplenews_set_authorable($uid, $tid){
if($uid && $tid){
db_query("INSERT IGNORE INTO {simplesnews_authors} SET uid = %d, tid = %d", $uid, $tid);
}
}
function simplenews_unset_authorable($uid, $tid){
if($uid && $tid){
db_query("DELETE FROM {simplesnews_authors} WHERE uid = %d AND tid = %d", $uid, $tid);
}
}
function simplenews_is_authorable($uid, $tid){
$authorable = simplenews_get_authorable($uid);
if(isset($authorable[$tid])){
$authorable[$tid];
}else{
return FALSE;
}
}
function simplenews_get_authorable($uid){
static $authorable = array();
if(isset($authorable[$uid])){
return $authorable[$uid];
}else{
$vid = variable_get('simplenews_vid', '');
if($uid != 1){
$result = db_query("SELECT a.tid, t.name FROM {simplenews_authors} AS a, {term_data} AS t WHERE a.uid = %d AND a.tid = t.tid AND t.vid = %d ORDER BY t.name ASC", $uid, $vid);
}else{ // admin
$result = db_query("SELECT t.tid, t.name FROM {term_data} AS t WHERE t.vid = %d ORDER BY t.name ASC", $vid);
}
$authorable[$uid] = array();
while($ns = db_fetch_object($result)){
$authorable[$uid][$ns->tid] = $ns->name;
}
return $authorable[$uid];
}
}and this to the update to simplenews.install
function simplenews_update_6010(){
$ret = array();
...
db_create_table($ret, 'simplesnews_authors', array(
'description' => t('Associate particular users with newsletters as their authors.'),
'fields' => array(
'aid' => array(
'description' => t('The id of this link between newsletter and author.'),
'type' => 'serial',
),
'tid' => array(
'description' => t('The taxonomy term id of this newsletter.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'uid' => array(
'description' => t('The user which created this newsletter or is entitled to author issues of it.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('aid'),
'index' => array(
'tid' => array('tid'),
'uid' => array('uid'),
),
'unique key' => array('tid', 'uid'),
)
);
return $ret;
}then everywhere where you have a display of newsletters to choose from, instead of using taxonomy_get_tree($vid) you call simplenews_get_authorable($uid) (and alter the foreach loop accordingly)
in simplesnews_admin.inc add
global $user;
simplenews_set_authorable($user->uid, $termdata['tid']);in the 'simplenews_admin_types_form_submit($form, &$form_state)' function, so that user becomes the only author (aside from admin) of this newsletter
and
global $user;
simplenews_unset_authorable($user->uid, $tid);in 'simplenews_admin_types_delete_submit($form, &$form_state)'.
cheers.

#1
ps. of course the call to 'simplenews_set_authorable($user->uid, $termdata['tid']);' has to be after the call to 'taxonomy_save_term($termdata)'