print_pdf.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the print_pdf module.
  5. *
  6. * @ingroup print
  7. */
  8. /**
  9. * Implements hook_enable().
  10. */
  11. function print_pdf_enable() {
  12. // Module weight.
  13. db_update('system')
  14. ->fields(array(
  15. 'weight' => 2,
  16. ))
  17. ->condition('type', 'module')
  18. ->condition('name', 'print_pdf')
  19. ->execute();
  20. }
  21. /**
  22. * Implements hook_uninstall().
  23. */
  24. function print_pdf_uninstall() {
  25. variable_del('print_pdf_autoconfig');
  26. variable_del('print_pdf_cache_enabled');
  27. variable_del('print_pdf_cache_lifetime');
  28. variable_del('print_pdf_content_disposition');
  29. variable_del('print_pdf_display_sys_urllist');
  30. variable_del('print_pdf_filename');
  31. variable_del('print_pdf_images_via_file');
  32. variable_del('print_pdf_link_text');
  33. variable_del('print_pdf_link_text_enabled');
  34. variable_del('print_pdf_page_orientation');
  35. variable_del('print_pdf_paper_size');
  36. variable_del('print_pdf_pdf_tool');
  37. variable_del('print_pdf_book_link');
  38. variable_del('print_pdf_link_class');
  39. variable_del('print_pdf_link_pos');
  40. variable_del('print_pdf_link_teaser');
  41. variable_del('print_pdf_link_use_alias');
  42. variable_del('print_pdf_show_link');
  43. variable_del('print_pdf_sys_link_pages');
  44. variable_del('print_pdf_sys_link_visibility');
  45. $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'print\_pdf\_display\_%'");
  46. foreach ($settings as $variable) {
  47. variable_del($variable->name);
  48. }
  49. }
  50. /**
  51. * Implements hook_schema().
  52. */
  53. function print_pdf_schema() {
  54. $schema['print_pdf_node_conf'] = array(
  55. 'description' => 'PDF version node-specific configuration settings',
  56. 'fields' => array(
  57. 'nid' => array(
  58. 'type' => 'int',
  59. 'unsigned' => TRUE,
  60. 'not null' => TRUE,
  61. 'description' => 'The {node}.nid of the node.',
  62. ),
  63. 'link' => array(
  64. 'type' => 'int',
  65. 'unsigned' => TRUE,
  66. 'not null' => TRUE,
  67. 'default' => 1,
  68. 'size' => 'tiny',
  69. 'description' => 'Show link',
  70. ),
  71. 'comments' => array(
  72. 'type' => 'int',
  73. 'unsigned' => TRUE,
  74. 'not null' => TRUE,
  75. 'default' => 1,
  76. 'size' => 'tiny',
  77. 'description' => 'Show link in individual comments',
  78. ),
  79. 'url_list' => array(
  80. 'type' => 'int',
  81. 'unsigned' => TRUE,
  82. 'not null' => TRUE,
  83. 'default' => 1,
  84. 'size' => 'tiny',
  85. 'description' => 'Show Printer-friendly URLs list',
  86. ),
  87. 'size' => array(
  88. 'type' => 'varchar',
  89. 'length' => 9,
  90. 'description' => 'Paper size',
  91. ),
  92. 'orientation' => array(
  93. 'type' => 'varchar',
  94. 'length' => 9,
  95. 'description' => 'Page orientation',
  96. ),
  97. ),
  98. 'primary key' => array('nid'),
  99. );
  100. $schema['print_pdf_page_counter'] = array(
  101. 'description' => 'PDF version access counter',
  102. 'fields' => array(
  103. 'path' => array(
  104. 'type' => 'varchar',
  105. 'length' => 255,
  106. 'not null' => TRUE,
  107. 'description' => 'Page path',
  108. ),
  109. 'totalcount' => array(
  110. 'type' => 'int',
  111. 'unsigned' => TRUE,
  112. 'not null' => TRUE,
  113. 'default' => 0,
  114. 'size' => 'big',
  115. 'description' => 'Number of page accesses',
  116. ),
  117. 'timestamp' => array(
  118. 'type' => 'int',
  119. 'unsigned' => TRUE,
  120. 'not null' => TRUE,
  121. 'default' => 0,
  122. 'description' => 'Last access',
  123. ),
  124. ),
  125. 'primary key' => array('path'),
  126. );
  127. return $schema;
  128. }
  129. /**
  130. * Implements hook_requirements().
  131. */
  132. function print_pdf_requirements($phase) {
  133. $requirements = array();
  134. $t = get_t();
  135. switch ($phase) {
  136. // At runtime, make sure that a PDF generation tool is selected.
  137. case 'runtime':
  138. $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  139. if (empty($print_pdf_pdf_tool)) {
  140. $requirements['print_pdf_tool'] = array(
  141. 'title' => $t('Printer, email and PDF versions - PDF generation library'),
  142. 'value' => $t('No PDF tool selected'),
  143. 'description' => $t('Please configure it in the !url.', array('!url' => l($t('PDF settings page'), 'admin/config/user-interface/print/pdf'))),
  144. 'severity' => REQUIREMENT_ERROR,
  145. );
  146. }
  147. else {
  148. // Tool is defined, get some data from it's handler module.
  149. $tool = explode('|', $print_pdf_pdf_tool);
  150. $function = $tool[0] . '_pdf_tool_info';
  151. $info = function_exists($function) ? $function() : array();
  152. // Is the file there?
  153. if (!is_file($tool[1]) || !is_readable($tool[1])) {
  154. $requirements['print_pdf_tool'] = array(
  155. 'title' => $t('Printer, email and PDF versions - PDF generation library'),
  156. 'value' => $t('File not found'),
  157. 'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $tool[1])),
  158. 'severity' => REQUIREMENT_ERROR,
  159. );
  160. }
  161. else {
  162. // Get the version number.
  163. $function = $tool[0] . '_pdf_tool_version';
  164. if (function_exists($function)) {
  165. $version = $function($tool[1]);
  166. if (isset($info['min_version']) && version_compare($version, $info['min_version'], '<')) {
  167. $requirements['print_pdf_tool_version'] = array(
  168. 'title' => $t('Printer, email and PDF versions - PDF generation library'),
  169. 'value' => $t('Unsupported %lib version', array('%lib' => $info['name'])),
  170. 'description' => $t('The currently selected version of %lib (@version) is not supported. Please update to a !url.', array(
  171. '%lib' => $info['name'],
  172. '@version' => $version,
  173. '!url' => l($t('newer version'), $info['url']),
  174. )),
  175. 'severity' => REQUIREMENT_ERROR,
  176. );
  177. }
  178. else {
  179. $requirements['print_pdf_tool_version'] = array(
  180. 'title' => $t('Printer, email and PDF versions - PDF generation library'),
  181. 'value' => $info['name'] . ' ' . $version,
  182. );
  183. }
  184. }
  185. }
  186. // If auto-config is on, check for write access to the appropriate dirs.
  187. if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
  188. $directories = array();
  189. if (isset($info['public_dirs'])) {
  190. foreach ($info['public_dirs'] as $dir) {
  191. $directories[] = 'public://print_pdf/' . $tool[0] . '/' . $dir;
  192. }
  193. }
  194. if (isset($info['tool_dirs'])) {
  195. foreach ($info['tool_dirs'] as $dir) {
  196. $directories[] = dirname($tool[1]) . '/' . $dir;
  197. }
  198. }
  199. foreach ($directories as $dir) {
  200. if (!is_dir($dir) || !is_writable($dir)) {
  201. $requirements['print_pdf_tool_' . $dir] = array(
  202. 'title' => $t('%lib directory', array('%lib' => $info['name'])),
  203. 'value' => $t('Non-writable permissions'),
  204. 'description' => $t('You must change the %libdir permissions to be writable, as %lib requires write-access to that directory.', array('%lib' => $info['name'], '%libdir' => $dir)),
  205. 'severity' => REQUIREMENT_ERROR,
  206. );
  207. }
  208. }
  209. }
  210. }
  211. break;
  212. }
  213. return $requirements;
  214. }
  215. /**
  216. * Remove hardcoded numeric deltas from all blocks.
  217. */
  218. function print_pdf_update_7000(&$sandbox) {
  219. $renamed_deltas = array(
  220. 'print_pdf' => array(
  221. '0' => 'print_pdf-top',
  222. ),
  223. );
  224. update_fix_d7_block_deltas($sandbox, $renamed_deltas, array());
  225. if (variable_get('print_pdf_filename', '') == '[site-name] - [title] - [mod-yyyy]-[mod-mm]-[mod-dd]') {
  226. variable_set('print_pdf_filename', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
  227. }
  228. }
  229. /**
  230. * Delete old variables.
  231. */
  232. function print_pdf_update_7200(&$sandbox) {
  233. variable_del('print_pdf_settings');
  234. variable_del('print_pdf_node_link_pages');
  235. variable_del('print_pdf_node_link_visibility');
  236. }
  237. /**
  238. * Update pdf_tool variable to new module|path format.
  239. */
  240. function print_pdf_update_7201(&$sandbox) {
  241. $tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
  242. if (count($tool) == 1) {
  243. // Not an array yet, update variable to new format.
  244. if (preg_match('!dompdf_config.inc.php$!', $tool[0])) {
  245. $tool[0] = 'print_pdf_dompdf|' . $tool[0];
  246. }
  247. elseif (preg_match('!tcpdf.php$!', $tool[0])) {
  248. $tool[0] = 'print_pdf_tcpdf|' . $tool[0];
  249. }
  250. elseif (preg_match('!wkhtmltopdf!', $tool[0])) {
  251. $tool[0] = 'print_pdf_wkhtmltopdf|' . $tool[0];
  252. }
  253. else {
  254. $tool[0] = PRINT_PDF_PDF_TOOL_DEFAULT;
  255. }
  256. variable_set('print_pdf_pdf_tool', $tool[0]);
  257. }
  258. }
  259. /**
  260. * Enable block and help area links.
  261. */
  262. function print_pdf_update_7202(&$sandbox) {
  263. $link_pos = variable_get('print_pdf_link_pos', drupal_json_decode('{ "link": "link", "block": "block", "help": "help" }'));
  264. $link_pos['block'] = 'block';
  265. $link_pos['help'] = 'help';
  266. variable_set('print_pdf_link_pos', $link_pos);
  267. }
  268. /**
  269. * Add Size and Orientation fields for per content type Size and Orientation.
  270. */
  271. function print_pdf_update_7203(&$sandbox) {
  272. $spec = array(
  273. 'type' => 'varchar',
  274. 'length' => 9,
  275. 'description' => 'Paper size',
  276. );
  277. db_add_field('print_pdf_node_conf', 'size', $spec);
  278. $spec = array(
  279. 'type' => 'varchar',
  280. 'length' => 9,
  281. 'description' => 'Page orientation',
  282. );
  283. db_add_field('print_pdf_node_conf', 'orientation', $spec);
  284. }
  285. /**
  286. * Enable the PDF generation sub-module being used.
  287. */
  288. function print_pdf_update_7204(&$sandbox) {
  289. // Since update_7201 already stored the correct module in the array, use that.
  290. $tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
  291. if (count($tool) == 2) {
  292. module_enable(array($tool[0]), FALSE);
  293. }
  294. }
  295. /**
  296. * Increase size of the path field in the print_pdf_page_counter table.
  297. */
  298. function print_pdf_update_7205(&$sandbox) {
  299. db_drop_primary_key('print_pdf_page_counter');
  300. db_change_field('print_pdf_page_counter', 'path', 'path',
  301. array(
  302. 'type' => 'varchar',
  303. 'length' => 255,
  304. 'not null' => TRUE,
  305. 'description' => 'Page path',
  306. ),
  307. array('primary key' => array('path')));
  308. }