imageblock.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * @file imageblock.module
  4. * Primarily Drupal hooks.
  5. */
  6. /**
  7. * Implementation of hook_theme()
  8. */
  9. function imageblock_theme() {
  10. return array(
  11. 'imageblock_content' => array(
  12. 'render element' => 'elements',
  13. 'template' => 'imageblock-content',
  14. ),
  15. );
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function imageblock_menu() {
  21. $items['admin/structure/block/imageblock_add'] = array(
  22. 'title' => 'Add image block',
  23. 'page callback' => 'drupal_get_form',
  24. 'page arguments' => array('imageblock_add_block_form'),
  25. 'access arguments' => array('administer blocks'),
  26. 'file' => 'imageblock.admin.inc',
  27. 'type' => MENU_LOCAL_ACTION,
  28. );
  29. $items['admin/structure/block/imageblock_configure'] = array(
  30. 'title' => 'Image block settings',
  31. 'description' => 'Configure Image block settings.',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('imageblock_admin_settings_form'),
  34. 'access arguments' => array('administer site configuration'),
  35. 'file' => 'imageblock.admin.inc',
  36. );
  37. $items['admin/structure/block/manage/imageblock/%/delete'] = array(
  38. 'title' => 'Delete block',
  39. 'page callback' => 'drupal_get_form',
  40. 'page arguments' => array('imageblock_custom_block_delete', 5),
  41. 'access arguments' => array('administer blocks'),
  42. 'type' => MENU_CALLBACK,
  43. 'file' => 'imageblock.admin.inc',
  44. );
  45. return $items;
  46. }
  47. /**
  48. * Implements hook_file_download().
  49. *
  50. * Ensure that image block images are always downloadable.
  51. * We may want to restrict this in the future.
  52. */
  53. function imageblock_file_download($uri) {
  54. if (strpos(file_uri_target($uri), variable_get('imageblock_image_path', 'imageblock')) === 0) {
  55. $info = image_get_info($uri);
  56. return array('Content-Type' => $info['mime_type']);
  57. }
  58. }
  59. /**
  60. * Implements hook_block_info().
  61. */
  62. function imageblock_block_info() {
  63. $blocks = array();
  64. $result = db_query('SELECT bid, info FROM {imageblock} ORDER BY info');
  65. foreach ($result as $block) {
  66. $blocks[$block->bid]['info'] = $block->info;
  67. // Not worth caching.
  68. $blocks[$block->bid]['cache'] = DRUPAL_NO_CACHE;
  69. }
  70. return $blocks;
  71. }
  72. /**
  73. * Implements hook_block_configure().
  74. */
  75. function imageblock_block_configure($delta = '') {
  76. if ($delta) {
  77. $custom_block = block_imageblock_get($delta);
  78. }
  79. else {
  80. $custom_block = array();
  81. }
  82. $form = block_custom_block_form($custom_block);
  83. imageblock_configure_form($form, $custom_block);
  84. return $form;
  85. }
  86. /**
  87. * Implements hook_block_save().
  88. */
  89. function imageblock_block_save($delta = '', $edit = array()) {
  90. $data = array();
  91. if (!empty($edit['imageblock_imagecache'])) {
  92. $data['imageblock_imagecache'] = $edit['imageblock_imagecache'];
  93. }
  94. if (!empty($edit['imageblock_alt'])) {
  95. $data['imageblock_alt'] = $edit['imageblock_alt'];
  96. }
  97. if (!empty($edit['imageblock_title'])) {
  98. $data['imageblock_title'] = $edit['imageblock_title'];
  99. }
  100. if (!empty($edit['imageblock_link'])) {
  101. $data['imageblock_link'] = $edit['imageblock_link'];
  102. }
  103. if (!empty($edit['imageblock_link_target'])) {
  104. $data['imageblock_link_target'] = $edit['imageblock_link_target'];
  105. }
  106. $old_file = imageblock_get_file($delta);
  107. if (!empty($edit['imageblock_file'])) {
  108. $file = $edit['imageblock_file'];
  109. // If the user uploaded a file, save it to a permanent location.
  110. if (!empty($file->fid)) {
  111. // If there is an old file, delete it.
  112. if (!empty($old_file->fid)) {
  113. file_usage_delete($old_file, 'imageblock', 'imageblock', $delta);
  114. file_delete($old_file);
  115. }
  116. $directory = file_default_scheme() . '://' . variable_get('imageblock_image_path', 'imageblock');
  117. // Prepare the directory.
  118. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  119. $destination = file_stream_wrapper_uri_normalize($directory . "/$file->filename");
  120. // Move the temporary file into the final location.
  121. if ($file = file_move($file, $destination, FILE_EXISTS_RENAME)) {
  122. $file->status = FILE_STATUS_PERMANENT;
  123. file_save($file);
  124. file_usage_add($file, 'imageblock', 'imageblock', $delta);
  125. }
  126. }
  127. }
  128. db_update('imageblock')
  129. ->fields(array(
  130. 'body' => $edit['body']['value'],
  131. 'info' => $edit['info'],
  132. 'format' => $edit['body']['format'],
  133. 'fid' => !empty($file) ? $file->fid : (!empty($old_file) ? $old_file->fid : 0),
  134. 'data' => serialize($data),
  135. ))
  136. ->condition('bid', $delta)
  137. ->execute();
  138. return TRUE;
  139. }
  140. /**
  141. * Implements hook_block_view().
  142. */
  143. function imageblock_block_view($delta = '') {
  144. $block = db_query('SELECT body, format, fid, data FROM {imageblock} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
  145. $data['subject'] = NULL;
  146. $data['content'] = theme('imageblock_content', array('block' => $block));
  147. return $data;
  148. }
  149. /**
  150. * Implements hook_form_alter().
  151. */
  152. function imageblock_form_alter(&$form, &$form_state, $form_id) {
  153. if ($form_id == 'block_admin_configure') {
  154. if ($form['module']['#value'] == 'imageblock') {
  155. $form['#attributes']['enctype'] = 'multipart/form-data';
  156. $form['#validate'][] = 'imageblock_configure_form_validate';
  157. }
  158. }
  159. elseif ($form_id == 'block_admin_display_form') {
  160. foreach ($form['blocks'] as $key => $element) {
  161. if ($element['module']['#value'] == 'imageblock') {
  162. $form['blocks'][$key]['delete'] = array(
  163. '#type' => 'link',
  164. '#title' => t('delete'),
  165. '#href' => 'admin/structure/block/manage/imageblock/' . $element['delta']['#value'] . '/delete',
  166. );
  167. }
  168. }
  169. }
  170. }
  171. function imageblock_configure_form(&$form, $block = NULL) {
  172. $title = t('Image');
  173. $description = '';
  174. if (!empty($block['bid'])) {
  175. $file = imageblock_get_file($block['bid']);
  176. if (!empty($file->fid)) {
  177. $title = t('Change Image');
  178. $description = t('If new file is selected previous file will be deleted. This action cannot be undone.');
  179. $form['imageblock_prev'] = array(
  180. '#type' => 'item',
  181. '#title' => t('Image'),
  182. '#markup' => $file->filename,
  183. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.8 : 0,
  184. );
  185. }
  186. }
  187. // Do not require body as we can have an image as the body.
  188. $form['body_field']['body']['#required'] = FALSE;
  189. $data = !empty($block['data']) ? unserialize($block['data']) : array();
  190. $form['imageblock'] = array(
  191. '#type' => 'file',
  192. '#title' => $title,
  193. '#description' => $description,
  194. '#size' => 40,
  195. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.7 : 0,
  196. );
  197. $form['imageblock_alt'] = array(
  198. '#type' => 'textfield',
  199. '#title' => t('Alternate text'),
  200. '#description' => t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
  201. '#size' => 40,
  202. '#default_value' => isset($data['imageblock_alt']) ? $data['imageblock_alt'] : '',
  203. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.5 : 0,
  204. );
  205. $form['imageblock_title'] = array(
  206. '#type' => 'textfield',
  207. '#title' => t('Title'),
  208. '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
  209. '#size' => 40,
  210. '#default_value' => isset($data['imageblock_title']) ? $data['imageblock_title'] : '',
  211. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.4 : 0,
  212. );
  213. $form['imageblock_link'] = array(
  214. '#type' => 'textfield',
  215. '#title' => t('Link'),
  216. '#description' => t('Leave empty for no link.'),
  217. '#size' => 40,
  218. '#default_value' => isset($data['imageblock_link']) ? $data['imageblock_link'] : '',
  219. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.3 : 0,
  220. );
  221. $form['imageblock_link_target'] = array(
  222. '#type' => 'select',
  223. '#title' => t('Link target'),
  224. '#description' => t('Leave empty for no link.'),
  225. '#options' => array(
  226. '_blank' => '_blank',
  227. '_parent' => '_parent',
  228. '_self' => '_self',
  229. '_top' => '_top',
  230. ),
  231. '#default_value' => isset($data['imageblock_link_target']) ? $data['imageblock_link_target'] : '_self',
  232. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.2 : 0,
  233. );
  234. if (module_exists('image')) {
  235. $options = array();
  236. foreach (image_styles() as $key => $preset) {
  237. $options[$key] = $preset['name'];
  238. }
  239. if (!empty($options)) {
  240. $form['imageblock_imagecache'] = array(
  241. '#type' => 'select',
  242. '#title' => t('Image style'),
  243. '#options' => array('' => t('<none>')) + $options,
  244. '#default_value' => isset($data['imageblock_imagecache']) ? $data['imageblock_imagecache'] : '',
  245. '#weight' => isset($form['body_field']['#weight']) ? $form['body_field']['#weight'] - 0.6 : 0,
  246. );
  247. }
  248. }
  249. }
  250. function imageblock_configure_form_validate($form, &$form_state) {
  251. // TODO: make these settings, make this work
  252. $validators['file_validate_is_image'] = array();
  253. if ($max = variable_get('imageblock_max_file_size', 0)) {
  254. $validators['file_validate_size'] = array(parse_size($max), 0);
  255. }
  256. if ($max = variable_get('imageblock_max_dimensions', 0)) {
  257. $validators['file_validate_image_resolution'] = array($max);
  258. }
  259. // Save new file uploads.
  260. $file = file_save_upload('imageblock', $validators);
  261. if ($file === FALSE) {
  262. form_set_error('imageblock', t("Failed to upload the image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('imageblock_image_path', 'imageblock'))));
  263. }
  264. elseif ($file !== NULL) {
  265. $form_state['values']['imageblock_file'] = $file;
  266. }
  267. }
  268. /**
  269. * Returns information from database about a user-created (custom) image block.
  270. */
  271. function block_imageblock_get($bid) {
  272. return db_query("SELECT * FROM {imageblock} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
  273. }
  274. /**
  275. * Returns the file information for a give block ID.
  276. */
  277. function imageblock_get_file($bid) {
  278. $fid = db_query("SELECT fid FROM {imageblock} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
  279. return file_load($fid);
  280. }
  281. /**
  282. * Process variables for imageblock-content.tpl.php.
  283. *
  284. * The $variables array contains the following arguments:
  285. * - $block
  286. *
  287. * @see imageblock-content.tpl.php
  288. */
  289. function template_preprocess_imageblock_content(&$variables) {
  290. $variables['image'] = '';
  291. $block = $variables['block'];
  292. $data = !empty($block->data) ? unserialize($block->data) : array();
  293. if (!empty($block->fid)) {
  294. $file = file_load($block->fid);
  295. if (!empty($file->fid)) {
  296. $attributes = array('class' => array('imageblock-image'));
  297. $variables['path'] = $file->uri;
  298. $variables['attributes'] = $attributes;
  299. if (!empty($data['imageblock_alt'])) {
  300. $variables['alt'] = $data['imageblock_alt'];
  301. }
  302. if (!empty($data['imageblock_title'])) {
  303. $variables['title'] = $data['imageblock_title'];
  304. }
  305. if (module_exists('image') && !empty($data['imageblock_imagecache']) && $preset = image_style_load($data['imageblock_imagecache'])) {
  306. $variables['image'] = theme('image_style', $variables + array('style_name' => $preset['name']));
  307. }
  308. else {
  309. $variables['image'] = theme('image', $variables);
  310. }
  311. if (!empty($data['imageblock_link'])) {
  312. $attributes = array('class' => array('imageblock-link'));
  313. if (!empty($data['imageblock_link_target'])) {
  314. $attributes['target'] = $data['imageblock_link_target'];
  315. }
  316. $variables['image'] = l($variables['image'], $data['imageblock_link'], array('html' => TRUE, 'attributes' => $attributes));
  317. }
  318. }
  319. }
  320. $variables['content'] = check_markup($block->body, $block->format, '', TRUE);
  321. }