Index: blogclient.install =================================================================== RCS file: blogclient.install diff -N blogclient.install --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ blogclient.install 12 Jul 2006 21:35:40 -0000 @@ -0,0 +1,45 @@ +url, $blog->remote_id, l('edit', "blogclient/edit/$blog->blogid")); + $rows[] = array($blog->title, $blog->remote_id, l('edit', "blogclient/edit/$blog->blogid")); } - print theme('page', theme('table', array(), $rows, array('width' => '100%'))); + return theme('table', array(), $rows, array('width' => '100%')); } -function blogclient_edit($blogid = 0) { - global $user; - $edit = array(); +function blogclient_edit($blogid = 0, $form_values = array()) { + + $blogid = arg(2); if ($blogid) { - $edit = db_fetch_array(db_query('SELECT * FROM {blogs} WHERE blogid = %d', $blogid)); + $form_values = db_fetch_array(db_query('SELECT * FROM {blogs} WHERE blogid = %d', $blogid)); if (!user_access('admin blogclient') && $edit['uid'] != $user->uid) { - drupal_goto('blogclient/edit'); + drupal_goto('blogclient'); } } - $submit = t('Save'); - $autodiscover = t('Autodiscover'); - if (isset($_POST['op'])) { - $edit = $_POST['edit']; - switch ($_POST['op']) { - case $submit: + $form_values = array_merge($form_values,$_POST['edit']); + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#default_value' => $form_values['title'], + '#size' => 40, + '#maxlength' => 255, + '#description' => t('Name of this blog'), + ); + $form['url'] = array( + '#type' => 'textfield', + '#title' => t('URL'), + '#default_value' => $form_values['url'], + '#size' => 40, + '#maxlength' => 255, + '#description' => t('The URL of your blog'), + ); + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Username'), + '#default_value' => $form_values['name'], + '#size' => 15, + '#maxlength' => 64, + );$form['pass'] = array( + '#type' => 'password', + '#title' => t('Password'), + '#default_value' => $form_values['pass'], + '#size' => 15, + '#maxlength' => 64, + ); + $form['endpoint'] = array( + '#type' => 'textfield', + '#title' => t('Endpoint'), + '#default_value' => $form_values['endpoint'], + '#size' => 40, + '#maxlength' => 255, + '#description' => t('The endpoint we should post to, for example https://www.blogger.com/atom/1234567890. Try pressing autodiscover below if you do not know the value of this.'), + ); + $form['remote_id'] = array( + '#type' => 'textfield', + '#title' => t('Remote id'), + '#default_value' => $form_values['remote_id'], + '#size' => 15, + '#maxlength' => 64, + ); + $form['type'] = array( + '#type' => 'select', + '#title' => t('Type'), + '#default_value' => $form_values['type'], + '#options' => drupal_map_assoc(array('atom', 'metaweblog', 'blogger')), + ); + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + $form['autodiscover'] = array( + '#type' => 'button', + '#value' => t('Autodiscover'), + ); if ($blogid) { - db_query("UPDATE {blogs} SET url = '%s', name = '%s', pass = '%s', remote_id = '%s', type = '%s', title = '%s', endpoint = '%s' WHERE blogid = %d", $edit['url'], $edit['name'], $edit['pass'], $edit['remote_id'], $edit['type'], $edit['title'], $edit['endpoint'], $blogid); - } - else { - $blogid = db_next_id('{blogs}_blogid'); - db_query("INSERT INTO {blogs} (blogid, url, name, pass, remote_id, type, uid, title, endpoint) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s')", $blogid, $edit['url'], $edit['name'], $edit['pass'], $edit['remote_id'], $edit['type'], $user->uid, $edit['title'], $edit['endpoint']); + $form['blogid'] = array( + '#type' => 'hidden', + '#value' => $blogid, + ); } - drupal_goto('blogclient'); - break; - case $autodiscover: - $endpoint = blogclient_discover($edit['url'], $edit['name'], $edit['pass']); - $edit['endpoint'] = $endpoint['endpoint']; - $edit['type'] = $endpoint['type']; - $edit['remote_id'] = $endpoint['remote_id']; - if (!$edit['title']) { - $edit['title'] = $endpoint['title']; + + if ($_POST['op'] == t('Autodiscover')) { + $endpoint = blogclient_discover($form_values['url'], $form_values['name'], $form_values['pass']); + $form['endpoint']['#value'] = $endpoint['endpoint']; + $form['type']['#value'] = $endpoint['type']; + $form['remote_id']['#value'] = $endpoint['remote_id']; + + if (!$form_values['title']) { + $form['title']['#value'] = $endpoint['title']; } - break; } + return drupal_get_form('blogclient_edit', $form); } - $form = ''; - $form .= form_textfield(t('Name'), 'title', $edit['title'], 40, 255, t('Name of this blog')); - $form .= form_textfield(t('URL'), 'url', $edit['url'], 40, 255, t('The URL of your blog')); - $form .= form_textfield(t('Username'), 'name', $edit['name'], 15, 64); - $form .= form_password(t('Password'), 'pass', $edit['pass'], 15, 64); - $form .= form_textfield(t('Endpoint'), 'endpoint', $edit['endpoint'], 40, 255, t('The endpoint we should post to, for example https://www.blogger.com/atom/1234567890. Try pressing autodiscover below if you do not know the value of this.')); - $form .= form_textfield(t('Remote id'), 'remote_id', $edit['remote_id'], 15, 64); - $form .= form_select(t('Type'), 'type', $edit['type'], drupal_map_assoc(array('atom', 'metaweblog', 'blogger'))); - $form .= form_submit($autodiscover); - $form .= form_submit($submit); - print theme('page', form($form)); + +function blogclient_edit_submit($form_id, $form_values) { + global $user; + if ($form_values['blogid']) { + db_query("UPDATE {blogs} SET url = '%s', name = '%s', pass = '%s', remote_id = '%s', type = '%s', title = '%s', endpoint = '%s' WHERE blogid = %d", $form_values['url'], $form_values['name'], $form_values['pass'], $form_values['remote_id'], $form_values['type'], $form_values['title'], $form_values['endpoint'], $form_values['blogid']); + } + else { + $blogid = db_next_id('{blogs}_blogid'); + db_query("INSERT INTO {blogs} (blogid, url, name, pass, remote_id, type, uid, title, endpoint) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s')", $blogid, $form_values['url'], $form_values['name'], $form_values['pass'], $form_values['remote_id'], $form_values['type'], $user->uid, $form_values['title'], $form_values['endpoint']); } + return 'blogclient'; +} + function _blogclient_discover($url, $name, $pass) { $data = _blogclient_discover_curl($url, $name, $pass); @@ -188,22 +251,36 @@ function blogclient_nodeapi($node, $op) { global $user; switch ($op) { - case 'form post': - if ($node->nid) return; - $blogs = db_query('SELECT blogid, title FROM {blogs} WHERE uid = %d', $user->uid); - while ($blog = db_fetch_object($blogs)) { - $options[$blog->blogid] = $blog->title; - $checked[] = $blog->blogid; - } - return form_checkboxes(t('Post to blogs'), 'blogs', $checked, $options); case 'insert': + case 'update': foreach ($node->blogs as $blogid) { - blogclient_post_node($node->nid, $blogid); + $ret = blogclient_post_node($node->nid, $blogid); + if ($ret !== TRUE) { + drupal_set_message(t('blogclient: %errormessage', array('%errormessage'=>$ret)), 'error'); + } } break; } } +function blogclient_form_alter($form_id, &$form) { + if (strstr($form_id, 'node_form') === FALSE) { + return; + } + global $user; + $blogs = db_query('SELECT blogid, title FROM {blogs} WHERE uid = %d', $user->uid); + while ($blog = db_fetch_object($blogs)) { + $options[$blog->blogid] = $blog->title; + $checked[] = $blog->blogid; + } + $form['blogs'] = array( + '#type' => 'checkboxes', + '#title' => t('Post to blogs'), + '#default_value' => $checked, + '#options' => $options, + ); +} + function blogclient_post_node($nid, $blogid) { global $user; $node = node_load(array('nid' => $nid)); @@ -221,6 +298,7 @@ $body = $node->body ? $node->body : $node->details; $body = str_replace(array('[q]', '[/q]'), '', $body); $blog_id = $blog->remote_id; + /* FIXME */ $generator_url = 'http://nowpublic.com'; $generator = 'NowPublic.com'; @@ -237,7 +315,7 @@ CONTENT; // Use curl to post to your blog. $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $blog->url); // "https://www.blogger.com/atom/". $blog_id + curl_setopt($ch, CURLOPT_URL, $blog->endpoint); // "https://www.blogger.com/atom/". $blog_id curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); @@ -250,11 +328,11 @@ function _blogclient_blogger_post_node($node, $blog) { $body = $node->body ? $node->body : $node->details; - xmlrpc($blog->url, 'blogger.newPost', '0123456789ABCDEF', $blog->remote_id, $blog->name, $blog->pass, "$node->title$body", TRUE); + xmlrpc($blog->endpoint, 'blogger.newPost', '0123456789ABCDEF', $blog->remote_id, $blog->name, $blog->pass, "$node->title$body", TRUE); return xmlrpc_errno() ? xmlrpc_error_msg() : TRUE; } function _blogclient_metaweblog_post_node($node, $blog) { $body = $node->body ? $node->body : $node->details; - xmlrpc($blog->url, 'metaWeblog.newPost', $blog->remote_id, $blog->name, $blog->pass, array('title' => $node->title, 'description' => $body), TRUE); + xmlrpc($blog->endpoint, 'metaWeblog.newPost', $blog->remote_id, $blog->name, $blog->pass, array('title' => $node->title, 'description' => $body), TRUE); return xmlrpc_errno() ? xmlrpc_error_msg() : TRUE; }