function mysite_type_blog($get_options = TRUE) {
if (module_exists('blog')) {
$type = array(
'name' => t('Blogs'),
'description' => t('<b>Blog Posts</b>: Blog posts from a specific user.'),
'include' => 'blog',
'prefix' => t(''),
'suffix' => t('blog posts'),
'category' => t('Content'),
'weight' => 0,
'form' => FALSE,
'label' => t('Add User Blog'),
'help' => t('You can choose blog postings by specific users. Type a user name in the search box, or choose from the list of active bloggers.'),
'search' => TRUE
);
$basic_settings = variable_get('mysite_basic_blog_settings', array());
$type = array_merge($type, $basic_settings);
if ($get_options) {
$type['options'] = mysite_type_blog_options();
}
return $type;
}
}
function mysite_type_blog_active($type) {
$result = db_query("SELECT perm FROM {permission}");
$check = '';
while ($perms = db_fetch_object($result)) {
$check .= $perms->perm;
}
if (stristr($check, 'edit own blog')) {
return array($type => TRUE);
}
else {
return array($type => FALSE, 'message' => l(t('There are no users with blog permissions.'), 'admin/user/access'));
}
}
function mysite_type_blog_options() {
$options = array();
$sql = "SELECT DISTINCT(u.uid), u.name, u.picture FROM {users} u INNER JOIN {node} n ON u.uid = n.uid WHERE n.status = 1 AND n.type = 'blog' ORDER BY u.name";
$result = db_query($sql);
$blogs = array();
while ($item = db_fetch_object($result)) {
$blogs[] = $item;
}
foreach ($blogs as $key => $value) {
$options['name'][] = mysite_type_blog_title($value->uid, $value->name);
$options['type_id'][] = $value->uid;
$options['type'][] = 'blog';
$icon = mysite_get_icon('blog', $value->uid);
if (empty($icon)) {
$icon = array('path' => file_directory_path() .'/'. variable_get('user_picture_path', 'pictures'), 'file' => $value->picture); }
$options['icon'][] = $icon;
}
return $options;
}
function mysite_type_blog_title($type_id = NULL, $title = NULL) {
if (is_numeric($type_id)) { if ($type_id == 0) {
$title = variable_get('anonymous', 'anonymous user');
}
if (is_null($title)) {
$blog = user_load(array('uid' => $type_id));
$title = $blog->name;
}
$type = mysite_type_blog(FALSE);
$title = $type['prefix'] .' '. $title . t("'s") .' '. $type['suffix'];
$title = trim(rtrim($title));
return $title;
}
drupal_set_message(t('Could not find blog title'), 'error');
return;
}
function mysite_type_blog_data($type_id = NULL, $settings = NULL) {
if (!empty($type_id)) {
$sql = db_rewrite_sql("SELECT n.nid, n.changed FROM {node} n WHERE n.type = 'blog' AND n.status = 1 AND n.uid = %d ORDER BY n.changed DESC");
$result = db_query_range($sql, $type_id, 0, variable_get('mysite_elements', 5));
$data = array(
'base' => 'blog/'. $type_id,
'xml' => 'blog/'. $type_id .'/feed',
);
$items = array();
$i = 0;
$type = mysite_type_blog(FALSE);
while ($nid = db_fetch_object($result)) {
$node = node_load($nid->nid);
$items[$i]['type'] = $node->type;
$items[$i]['link'] = l($node->title, 'node/'. $nid->nid, array('target' => $type['link_target']));
$items[$i]['title'] = check_plain($node->title);
$items[$i]['subtitle'] = NULL;
$items[$i]['date'] = $node->changed;
$items[$i]['uid'] = $node->uid;
$items[$i]['author'] = check_plain($node->name);
$items[$i]['teaser'] = mysite_teaser($node);
$items[$i]['nid'] = $node->nid;
$i++;
}
$data['items'] = $items;
return $data;
}
drupal_set_message(t('Could not find blog data'), 'error');
return;
}
function mysite_type_blog_block($arg, $op = 'view') {
global $user;
if (user_access('access content') && ($arg[0] == 'blog' || $arg[0] == 'user') && is_numeric($arg[1]) && user_access('edit own blog', user_load(array('uid' => $arg[1])))) {
$blog = user_load(array('uid' => $arg[1]));
$data = array();
$data['uid'] = $user->uid;
$data['type'] = 'blog';
$data['type_id'] = $blog->uid;
$content = mysite_block_handler($data);
return $content;
}
}
function mysite_type_blog_block_node($nid, $type) {
if ($type == 'blog') {
global $user;
$sql = "SELECT DISTINCT(u.uid), u.name FROM {users} u INNER JOIN {node} n ON u.uid = n.uid WHERE n.nid = %d AND n.status = 1 AND n.type = '%s'";
$blog = db_fetch_object(db_query($sql, $nid, $type));
$data = array();
$data['uid'] = $user->uid;
$data['type'] = $type;
$data['type_id'] = $blog->uid;
$content = mysite_block_handler($data);
return $content;
}
}
function mysite_type_blog_search($uid = NULL) {
if (!is_null($uid)) {
$output .= drupal_get_form('mysite_type_blog_search_form', $uid);
return $output;
}
}
function mysite_type_blog_search_form($uid) {
$form['add_blog']['blog_title'] = array('#type' => 'textfield',
'#title' => t('Search by user name'),
'#maxlength' => 64,
'#size' => 40,
'#description' => t('The user name of the blog you wish to add.'),
'#required' => FALSE, '#autocomplete_path' => 'autocomplete/mysite/blog'
);
$form['add_blog']['uid'] = array('#type' => 'hidden', '#value' => $uid);
$form['add_blog']['type'] = array('#type' => 'hidden', '#value' => 'blog');
$form['add_blog']['submit'] = array('#type' => 'submit', '#value' => t('Add User Blog'));
return $form;
}
function mysite_type_blog_search_form_submit($form_id, $form_values) {
$result = db_query(db_rewrite_sql("SELECT DISTINCT(u.uid), u.name FROM {users} u INNER JOIN {node} n ON u.uid = n.uid WHERE n.status = 1 AND n.type = 'blog' AND LOWER(u.name) LIKE LOWER('%s%%')"), $form_values['blog_title']);
$count = 0;
while ($blog = db_fetch_object($result)) {
$data[$count]['uid'] = $form_values['uid'];
$data[$count]['type'] = $form_values['type'];
$data[$count]['type_id'] = $blog->uid;
$data[$count]['title'] = mysite_type_blog_title($blog->uid, $blog->name);
$data[$count]['description'] = t('The blog posts of @user', array('@user' => $blog->name));
$count++;
}
mysite_search_handler($data, 'blog');
return;
}
function mysite_type_blog_autocomplete($string) {
$matches = array();
$result = db_query_range(db_rewrite_sql("SELECT DISTINCT(u.uid), u.name FROM {users} u INNER JOIN {node} n ON u.uid = n.uid WHERE n.status = 1 AND n.type = 'blog' AND LOWER(u.name) LIKE LOWER('%s%%')"), $string, 0, 10);
while ($blog = db_fetch_object($result)) {
$matches[$blog->name] = check_plain($blog->name);
}
return $matches;
}
function mysite_type_blog_clear($type) {
$sql = "SELECT mid, uid, type_id, title FROM {mysite_data} WHERE type = '%s'";
$result = db_query($sql, $type);
$data = array();
while ($item = db_fetch_array($result)) {
$sql = "SELECT DISTINCT(u.uid) FROM {users} u INNER JOIN {node} n ON u.uid = n.uid WHERE n.status = 1 AND n.type = 'blog' AND u.uid = %d";
$check = db_fetch_object(db_query($sql, $item['type_id']));
if (empty($check->uid)) {
$data[$item['mid']] = $item;
}
}
return $data;
}