| 1 |
<?php
|
| 2 |
// $Id: image_pub.common.inc,v 1.1.2.1 2009/01/31 02:00:25 egfrith Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* Generalized helper functions for tasks that require tight integration
|
| 6 |
* with other parts of Drupal, such as image.module and taxonomy.module.
|
| 7 |
* _image_pub_get_vid()
|
| 8 |
* _image_pub_base_url()
|
| 9 |
* _image_pub_image_baseurl()
|
| 10 |
* _image_pub_get_imagefilename()
|
| 11 |
* _image_pub_get_imageinfo()
|
| 12 |
* _image_pub_album_get()
|
| 13 |
* _image_pub_album_enum()
|
| 14 |
* _image_pub_album_selector()
|
| 15 |
* _image_pub_album_access()
|
| 16 |
* _image_pub_album_images()
|
| 17 |
*
|
| 18 |
* _image_pub_authenticate()
|
| 19 |
* _image_pub_album_add()
|
| 20 |
* _image_pub_image_add()
|
| 21 |
*/
|
| 22 |
|
| 23 |
require_once 'image_pub.common.inc';
|
| 24 |
|
| 25 |
function _image_pub_get_vid() {
|
| 26 |
return _image_gallery_get_vid();
|
| 27 |
}
|
| 28 |
function _image_pub_base_url() {
|
| 29 |
global $base_url;
|
| 30 |
return $base_url;
|
| 31 |
}
|
| 32 |
function _image_pub_image_baseurl() {
|
| 33 |
return _image_pub_base_url() .'/'. variable_get('file_directory_path', 'files') .'/'. variable_get('image_default_path', 'images');
|
| 34 |
}
|
| 35 |
function _image_pub_get_imagefilename($node, $size = '_original') {
|
| 36 |
$fname = $node->images[$size];
|
| 37 |
if (isset($fname)) {
|
| 38 |
$imgbase = variable_get('image_default_path', 'images') .'/';
|
| 39 |
if (!strncmp($fname, $imgbase, strlen($imgbase))) {
|
| 40 |
$fname = substr($fname, strlen($imgbase));
|
| 41 |
}
|
| 42 |
}
|
| 43 |
return $fname;
|
| 44 |
}
|
| 45 |
function _image_pub_get_imageinfo($node, $size = '_original') {
|
| 46 |
$file = file_create_path($node->images[$size]);
|
| 47 |
$info = image_get_info($file);
|
| 48 |
$info['filesize'] = filesize($file);
|
| 49 |
return $info;
|
| 50 |
}
|
| 51 |
function _image_pub_album_get($albumid) {
|
| 52 |
|
| 53 |
$term = taxonomy_get_term($albumid);
|
| 54 |
if (!$term) {
|
| 55 |
if ($albumid == 0) {
|
| 56 |
$term = taxonomy_get_term('<root>');
|
| 57 |
}
|
| 58 |
else {
|
| 59 |
$term = NULL;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
return $term;
|
| 63 |
}
|
| 64 |
function _image_pub_album_enum($albumid = 0, $subalbums = TRUE) {
|
| 65 |
return taxonomy_get_tree(_image_pub_get_vid(), $albumid, -1,
|
| 66 |
($subalbums ? NULL : 1));
|
| 67 |
}
|
| 68 |
function _image_pub_album_selector($fname, $showroot = FALSE, $selalbum = NULL) {
|
| 69 |
/* This is mostly redundant with taxonomy_form(). */
|
| 70 |
$body = array();
|
| 71 |
$tree = _image_pub_album_enum();
|
| 72 |
$options = array();
|
| 73 |
$albumid = 0;
|
| 74 |
if (isset($selalbum)) {
|
| 75 |
$albumid = $selalbum->tid;
|
| 76 |
}
|
| 77 |
if ($showroot) {
|
| 78 |
$options[0] = '<Root Album>';
|
| 79 |
}
|
| 80 |
if ($tree) {
|
| 81 |
foreach ($tree as $term) {
|
| 82 |
if (!$showroot && ($albumid == 0)) {
|
| 83 |
$albumid = $term->tid;
|
| 84 |
}
|
| 85 |
$dashes = '';
|
| 86 |
for ($i = 0; $i < $term->depth; $i++) {
|
| 87 |
$dashes .= '-';
|
| 88 |
}
|
| 89 |
$options[$term->tid] = $dashes . $term->name;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
$body[] = '<select name="' . $fname . '">';
|
| 93 |
foreach ($options as $aid => $term) {
|
| 94 |
$sel = '';
|
| 95 |
if ($aid == $albumid) {
|
| 96 |
$sel = ' selected="selected"';
|
| 97 |
}
|
| 98 |
$body[] = '<option value="' .$aid . '"' . $sel . '>' . $term . '</option>';
|
| 99 |
}
|
| 100 |
$body[] = '</select>';
|
| 101 |
return implode("\r\n", $body);
|
| 102 |
}
|
| 103 |
|
| 104 |
function _image_pub_album_access($type, $album) {
|
| 105 |
switch ($type) {
|
| 106 |
case 'view':
|
| 107 |
return user_access('access content');
|
| 108 |
case 'update':
|
| 109 |
return user_access('administer images');
|
| 110 |
case 'create':
|
| 111 |
return user_access('create images');
|
| 112 |
}
|
| 113 |
return FALSE;
|
| 114 |
}
|
| 115 |
function _image_pub_album_images($tid) {
|
| 116 |
$nodes = array();
|
| 117 |
$sql = "SELECT n.nid FROM {node} n INNER JOIN {term_node} t ON t.nid = n.nid WHERE t.tid = $tid AND n.type = 'image';";
|
| 118 |
$res = db_query($sql);
|
| 119 |
while ($term = db_fetch_object($res)) {
|
| 120 |
$nodes[] = node_load(array('nid' => $term->nid));
|
| 121 |
}
|
| 122 |
return $nodes;
|
| 123 |
}
|
| 124 |
function _image_pub_authenticate($uname, $pass) {
|
| 125 |
global $user;
|
| 126 |
if ($user->uid) {
|
| 127 |
watchdog('image_pub', 'Session closed for %user', array('%user' => theme('placeholder', $user->name)));
|
| 128 |
unset($user);
|
| 129 |
}
|
| 130 |
$user = user_authenticate(array('name' => $uname, 'pass' => $pass));
|
| 131 |
if (!$user->uid) {
|
| 132 |
return FALSE;
|
| 133 |
}
|
| 134 |
watchdog('image_pub', 'Session opened for %user', array('%user' => theme('placeholder', $user->name)));
|
| 135 |
return TRUE;
|
| 136 |
}
|
| 137 |
function _image_pub_album_add($title, $descr, $palbum) {
|
| 138 |
watchdog('image_pub', 'Album added with title %title', array('%title' => $title));
|
| 139 |
|
| 140 |
if (empty($title)) {
|
| 141 |
$title = 'Untitled album';
|
| 142 |
}
|
| 143 |
$edit = array(
|
| 144 |
'name' => $title,
|
| 145 |
'description' => $descr,
|
| 146 |
'vid' => _image_pub_get_vid(),
|
| 147 |
'parent' => array(isset($palbum) ? $palbum->tid : 0),
|
| 148 |
'weight' => 0,
|
| 149 |
);
|
| 150 |
$term = taxonomy_save_term($edit);
|
| 151 |
if (isset($term)) {
|
| 152 |
// taxonomy_save_term amends $edit directly, and simply returns a status
|
| 153 |
// return (object)$edit;
|
| 154 |
return (object)$edit;
|
| 155 |
}
|
| 156 |
return NULL;
|
| 157 |
}
|
| 158 |
function _image_pub_image_add($album, $caption, $description, $srcfield) {
|
| 159 |
global $user;
|
| 160 |
if (!isset($_FILES[$srcfield])) {
|
| 161 |
return array('success' => FALSE,
|
| 162 |
'reason' => 'Forgetting a file?');
|
| 163 |
}
|
| 164 |
|
| 165 |
if (empty($caption)) {
|
| 166 |
$caption = $_POST['force_filename'];
|
| 167 |
if (empty($caption)) {
|
| 168 |
$caption = basename($_FILES[$srcfield]['name']);
|
| 169 |
if (empty($caption)) {
|
| 170 |
$caption = 'Untitled image';
|
| 171 |
}
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
// Create a drupal node
|
| 176 |
// In order for the image upload to work, the name, tmp_name and error
|
| 177 |
// of the $_FILES[$srcfield] have to be copied to locations
|
| 178 |
// that file_check_upload() recognises.
|
| 179 |
// The size needs to go into $_FILES['files']['size']['image'] for image
|
| 180 |
// module's size checking to occur.
|
| 181 |
$_FILES['files']['name']['image'] = $_FILES[$srcfield]['name'];
|
| 182 |
$_FILES['files']['tmp_name']['image'] = $_FILES[$srcfield]['tmp_name'];
|
| 183 |
$_FILES['files']['error']['image'] = $_FILES[$srcfield]['error'];
|
| 184 |
$_FILES['files']['size']['image'] = filesize($_FILES[$srcfield]['tmp_name']);
|
| 185 |
|
| 186 |
$form_state = array();
|
| 187 |
module_load_include('inc', 'node', 'node.pages');
|
| 188 |
$node = array('type' => 'image');
|
| 189 |
$form_state['values']['title'] = $caption;
|
| 190 |
$form_state['values']['uid'] = $user->uid;
|
| 191 |
$form_state['values']['name'] = $user->name;
|
| 192 |
$form_state['values']['body_field'] = $description;
|
| 193 |
$form_state['values']['taxonomy'][_image_pub_get_vid()] = $album->tid;
|
| 194 |
$form_state['values']['op'] = t('Save');
|
| 195 |
$redirect_url = drupal_execute('image_node_form', $form_state, (object)$node);
|
| 196 |
|
| 197 |
// Check that the uploaded image was accepted.
|
| 198 |
$errors = form_get_errors();
|
| 199 |
if (!empty($errors)) {
|
| 200 |
return array('success' => FALSE,
|
| 201 |
'reason' => strip_tags(implode($errors, ' ')));
|
| 202 |
}
|
| 203 |
|
| 204 |
watchdog('image_pub', '%image was added.', array('%image' => $_FILES[$srcfield]['name']));
|
| 205 |
return array('success' => TRUE, 'redirect_url' => $redirect_url);
|
| 206 |
}
|