uc_file.admin.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. /**
  3. * @file
  4. * File administration menu items.
  5. */
  6. /**
  7. * Form step values.
  8. */
  9. define('UC_FILE_FORM_FILES' , NULL);
  10. define('UC_FILE_FORM_ACTION', 1 );
  11. /**
  12. * Form builder for file products admin.
  13. *
  14. * @see uc_file_admin_files_form_show_files()
  15. * @see uc_file_admin_files_form_action()
  16. *
  17. * @ingroup forms
  18. */
  19. function uc_file_admin_files_form($form, &$form_state) {
  20. if (isset($form_state['storage']['step']) && $form_state['storage']['step'] == UC_FILE_FORM_ACTION) {
  21. return array(
  22. '#validate' => array('uc_file_admin_files_form_action_validate'),
  23. '#submit' => array('uc_file_admin_files_form_action_submit'),
  24. ) + $form + uc_file_admin_files_form_action($form, $form_state);
  25. }
  26. else {
  27. // Refresh our file list before display.
  28. uc_file_refresh();
  29. return array(
  30. '#theme' => 'uc_file_admin_files_form_show',
  31. '#validate' => array('uc_file_admin_files_form_show_validate'),
  32. '#submit' => array('uc_file_admin_files_form_show_submit'),
  33. ) + $form + uc_file_admin_files_form_show_files($form, $form_state);
  34. }
  35. }
  36. /**
  37. * Displays all files that may be purchased and downloaded for administration.
  38. *
  39. * @see uc_file_admin_files_form()
  40. * @see uc_file_admin_files_form_show_validate()
  41. * @see uc_file_admin_files_form_show_submit()
  42. * @see theme_uc_file_admin_files_form_show()
  43. *
  44. * @ingroup forms
  45. */
  46. function uc_file_admin_files_form_show_files($form, &$form_state) {
  47. $form['#tree'] = TRUE;
  48. $header = array(
  49. 'filename' => array('data' => t('File'), 'field' => 'f.filename', 'sort' => 'asc'),
  50. 'title' => array('data' => t('Product'), 'field' => 'n.title'),
  51. 'model' => array('data' => t('SKU'), 'field' => 'fp.model'),
  52. );
  53. // Create pager.
  54. $query = db_select('uc_files', 'f')->extend('PagerDefault')->extend('TableSort')
  55. ->orderByHeader($header)
  56. ->limit(UC_FILE_PAGER_SIZE);
  57. $query->leftJoin('uc_file_products', 'fp', 'f.fid = fp.fid');
  58. $query->leftJoin('uc_product_features', 'pf', 'fp.pfid = pf.pfid');
  59. $query->leftJoin('node', 'n', 'pf.nid = n.nid');
  60. $query->addField('n', 'nid');
  61. $query->addField('f', 'filename');
  62. $query->addField('n', 'title');
  63. $query->addField('fp', 'model');
  64. $query->addField('f', 'fid');
  65. $query->addField('pf', 'pfid');
  66. $count_query = db_select('uc_files');
  67. $count_query->addExpression('COUNT(*)');
  68. $query->setCountQuery($count_query);
  69. $result = $query->execute();
  70. $options = array();
  71. foreach ($result as $file) {
  72. $options[$file->fid] = array(
  73. 'filename' => array(
  74. 'data' => check_plain($file->filename),
  75. 'class' => is_dir(uc_file_qualify_file($file->filename)) ? array('uc-file-directory-view') : array(),
  76. ),
  77. 'title' => l($file->title, 'node/' . $file->nid),
  78. 'model' => check_plain($file->model),
  79. );
  80. }
  81. // Create checkboxes for each file.
  82. $form['file_select'] = array(
  83. '#type' => 'tableselect',
  84. '#header' => $header,
  85. '#options' => $options,
  86. '#empty' => t('No file downloads available.'),
  87. );
  88. $form['uc_file_action'] = array(
  89. '#type' => 'fieldset',
  90. '#title' => t('File options'),
  91. '#collapsible' => FALSE,
  92. '#collapsed' => FALSE,
  93. );
  94. // Set our default actions.
  95. $file_actions = array(
  96. 'uc_file_upload' => t('Upload file'),
  97. 'uc_file_delete' => t('Delete file(s)'),
  98. );
  99. // Check if any hook_uc_file_action('info', $args) are implemented.
  100. foreach (module_implements('uc_file_action') as $module) {
  101. $name = $module . '_uc_file_action';
  102. $result = $name('info', NULL);
  103. if (is_array($result)) {
  104. foreach ($result as $key => $action) {
  105. if ($key != 'uc_file_delete' && $key != 'uc_file_upload') {
  106. $file_actions[$key] = $action;
  107. }
  108. }
  109. }
  110. }
  111. $form['uc_file_action']['action'] = array(
  112. '#type' => 'select',
  113. '#title' => t('Action'),
  114. '#options' => $file_actions,
  115. '#prefix' => '<div class="duration">',
  116. '#suffix' => '</div>',
  117. );
  118. $form['uc_file_actions']['actions'] = array('#type' => 'actions');
  119. $form['uc_file_action']['actions']['submit'] = array(
  120. '#type' => 'submit',
  121. '#value' => t('Perform action'),
  122. '#prefix' => '<div class="duration">',
  123. '#suffix' => '</div>',
  124. );
  125. return $form;
  126. }
  127. /**
  128. * Ensures at least one file is selected when deleting.
  129. *
  130. * @see uc_file_admin_files_form_show_files()
  131. * @see uc_file_admin_files_form_show_submit()
  132. */
  133. function uc_file_admin_files_form_show_validate($form, &$form_state) {
  134. switch ($form_state['values']['uc_file_action']['action']) {
  135. case 'uc_file_delete':
  136. $file_ids = array();
  137. if (is_array($form_state['values']['file_select'])) {
  138. foreach ($form_state['values']['file_select'] as $fid => $value) {
  139. if ($value) {
  140. $file_ids[] = $fid;
  141. }
  142. }
  143. }
  144. if (count($file_ids) == 0) {
  145. form_set_error('', t('You must select at least one file to delete.'));
  146. }
  147. break;
  148. }
  149. }
  150. /**
  151. * Moves to the next step of the administration form.
  152. *
  153. * @see uc_file_admin_files_form_show_files()
  154. * @see uc_file_admin_files_form_show_validate()
  155. */
  156. function uc_file_admin_files_form_show_submit($form, &$form_state) {
  157. // Increment the form step.
  158. $form_state['storage']['step'] = UC_FILE_FORM_ACTION;
  159. $form_state['rebuild'] = TRUE;
  160. }
  161. /**
  162. * Returns HTML for uc_file_admin_files_form_show().
  163. *
  164. * @param $variables
  165. * An associative array containing:
  166. * - form: A render element representing the form.
  167. *
  168. * @see uc_file_admin_files_form_show_files()
  169. *
  170. * @ingroup themeable
  171. */
  172. function theme_uc_file_admin_files_form_show($variables) {
  173. $form = $variables['form'];
  174. // Render everything.
  175. $output = '<p>' . t('File downloads can be attached to any Ubercart product as a product feature. For security reasons the <a href="!download_url">file downloads directory</a> is separated from the Drupal <a href="!file_url">file system</a>. Below is the list of files (and their associated Ubercart products, if any) that can be used for file downloads.', array('!download_url' => url('admin/store/settings/products', array('query' => array('destination' => 'admin/store/products/files'))), '!file_url' => url('admin/config/media/file-system'))) . '</p>';
  176. $output .= drupal_render($form['uc_file_action']);
  177. $output .= drupal_render($form['file_select']);
  178. $output .= theme('pager');
  179. $output .= drupal_render_children($form);
  180. return $output;
  181. }
  182. /**
  183. * Performs file action (upload, delete, hooked in actions).
  184. *
  185. * @see uc_file_admin_files_form()
  186. * @see uc_file_admin_files_form_action_validate()
  187. * @see uc_file_admin_files_form_action_submit()
  188. *
  189. * @ingroup forms
  190. */
  191. function uc_file_admin_files_form_action($form, &$form_state) {
  192. $file_ids = array_filter($form_state['values']['file_select']);
  193. $form['file_ids'] = array('#type' => 'value', '#value' => $file_ids);
  194. $form['action'] = array('#type' => 'value', '#value' => $form_state['values']['uc_file_action']['action']);
  195. $file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, FALSE));
  196. switch ($form_state['values']['uc_file_action']['action']) {
  197. case 'uc_file_delete':
  198. $affected_list = _uc_file_build_js_file_display($file_ids);
  199. $has_directory = FALSE;
  200. foreach ($file_ids as $file_id) {
  201. // Gather a list of user-selected filenames.
  202. $file = uc_file_get_by_id($file_id);
  203. $filename = $file->filename;
  204. $file_list[] = (substr($filename, -1) == "/") ? $filename . ' (' . t('directory') . ')' : $filename;
  205. // Determine if there are any directories in this list.
  206. $path = uc_file_qualify_file($filename);
  207. if (is_dir($path)) {
  208. $has_directory = TRUE;
  209. }
  210. }
  211. // Base files/dirs the user selected.
  212. $form['selected_files'] = array(
  213. '#theme' => 'item_list',
  214. '#items' => $file_list,
  215. '#attributes' => array(
  216. 'class' => array('selected-file-name'),
  217. ),
  218. );
  219. $form = confirm_form(
  220. $form, t('Delete file(s)'),
  221. 'admin/store/products/files',
  222. t('Deleting a file will remove all its associated file downloads and product features. Removing a directory will remove any files it contains and their associated file downloads and product features.'),
  223. t('Delete affected files'), t('Cancel')
  224. );
  225. // Don't even show the recursion checkbox unless we have any directories.
  226. if ($has_directory && $affected_list[TRUE] !== FALSE) {
  227. $form['recurse_directories'] = array(
  228. '#type' => 'checkbox',
  229. '#title' => t('Delete selected directories and their sub directories'),
  230. );
  231. // Default to FALSE. Although we have the JS behavior to update with the
  232. // state of the checkbox on load, this should improve the experience of
  233. // users who don't have JS enabled over not defaulting to any info.
  234. $form['affected_files'] = array(
  235. '#theme' => 'item_list',
  236. '#items' => $affected_list[FALSE],
  237. '#title' => t('Affected files'),
  238. '#attributes' => array(
  239. 'class' => array('affected-file-name'),
  240. ),
  241. );
  242. }
  243. break;
  244. case 'uc_file_upload':
  245. // Calculate the maximum size of uploaded files in bytes.
  246. $post_max_size = ini_get('post_max_size');
  247. if (is_numeric($post_max_size)) {
  248. // Handle the case where 'post_max_size' has no suffix.
  249. // An explicit cast is needed because floats are not allowed.
  250. $max_bytes = (int) $post_max_size;
  251. }
  252. else {
  253. // Handle the case where 'post_max_size' has a suffix of
  254. // 'M', 'K', or 'G' (case insensitive).
  255. $max_bytes = (int) substr($post_max_size, 0, -1);
  256. $suffix = strtolower(substr($post_max_size, -1));
  257. switch ($suffix) {
  258. case 'k':
  259. $max_bytes *= 1024;
  260. break;
  261. case 'm':
  262. $max_bytes *= 1048576;
  263. break;
  264. case 'g':
  265. $max_bytes *= 1073741824;
  266. break;
  267. }
  268. }
  269. // Gather list of directories under the selected one(s).
  270. // '/' is always available.
  271. $directories = array('' => '/');
  272. $files = db_query("SELECT * FROM {uc_files}");
  273. foreach ($files as $file) {
  274. if (is_dir(variable_get('uc_file_base_dir', NULL) . "/" . $file->filename)) {
  275. $directories[$file->filename] = $file->filename;
  276. }
  277. }
  278. $form['upload_dir'] = array(
  279. '#type' => 'select',
  280. '#title' => t('Directory'),
  281. '#description' => t('The directory on the server where the file should be put. The default directory is the root of the file downloads directory.'),
  282. '#options' => $directories,
  283. );
  284. $form['upload'] = array(
  285. '#type' => 'file',
  286. '#title' => t('File'),
  287. // Deliberately concatenate the strings to preserve backwards
  288. // compatibility with existing translations.
  289. '#description' => t('The maximum file size that can be uploaded is %size bytes. You will need to use a different method to upload the file to the directory (e.g. (S)FTP, SCP) if your file exceeds this size. Files you upload using one of these alternate methods will be automatically detected.', array('%size' => number_format($max_bytes))) . ' ' . t("Note: A value of '0' means there is no size limit."),
  290. );
  291. $form['#attributes']['class'][] = 'foo';
  292. $form = confirm_form(
  293. $form, t('Upload file'),
  294. 'admin/store/products/files',
  295. '',
  296. t('Upload file'), t('Cancel')
  297. );
  298. // Must add this after confirm_form, as it runs over $form['#attributes'].
  299. // Issue logged at https://www.drupal.org/node/319723.
  300. $form['#attributes']['enctype'] = 'multipart/form-data';
  301. break;
  302. default:
  303. // This action isn't handled by us, so check if any
  304. // hook_uc_file_action('form', $args) are implemented.
  305. foreach (module_implements('uc_file_action') as $module) {
  306. $name = $module . '_uc_file_action';
  307. $result = $name('form', array('action' => $form_state['values']['uc_file_action']['action'], 'file_ids' => $file_ids));
  308. $form = (is_array($result)) ? array_merge($form, $result) : $form;
  309. }
  310. break;
  311. }
  312. return $form;
  313. }
  314. /**
  315. * Validation handler for uc_file_admin_files_form_action().
  316. *
  317. * @see uc_file_admin_files_form_action()
  318. * @see uc_file_admin_files_form_action_submit()
  319. */
  320. function uc_file_admin_files_form_action_validate($form, &$form_state) {
  321. switch ($form_state['values']['action']) {
  322. case 'uc_file_upload':
  323. // Upload the file and get its object.
  324. if ($temp_file = file_save_upload('upload', array('file_validate_extensions' => array()))) {
  325. // Check if any hook_uc_file_action('upload_validate', $args)
  326. // are implemented.
  327. foreach (module_implements('uc_file_action') as $module) {
  328. $name = $module . '_uc_file_action';
  329. $result = $name('upload_validate', array('file_object' => $temp_file, 'form_id' => $form_id, 'form_state' => $form_state));
  330. }
  331. // Save the uploaded file for later processing.
  332. $form_state['storage']['temp_file'] = $temp_file;
  333. }
  334. else {
  335. form_set_error('', t('An error occurred while uploading the file'));
  336. }
  337. break;
  338. default:
  339. // This action isn't handled by us, so check if any
  340. // hook_uc_file_action('validate', $args) are implemented.
  341. foreach (module_implements('uc_file_action') as $module) {
  342. $name = $module . '_uc_file_action';
  343. $result = $name('validate', array('form_id' => $form_id, 'form_state' => $form_state));
  344. }
  345. break;
  346. }
  347. }
  348. /**
  349. * Submit handler for uc_file_admin_files_form_action().
  350. *
  351. * @see uc_file_admin_files_form_action()
  352. */
  353. function uc_file_admin_files_form_action_submit($form, &$form_state) {
  354. switch ($form_state['values']['action']) {
  355. case 'uc_file_delete':
  356. // File deletion status.
  357. $status = TRUE;
  358. // Delete the selected file(s), with recursion if selected.
  359. $status = uc_file_remove_by_id($form_state['values']['file_ids'], !empty($form_state['values']['recurse_directories'])) && $status;
  360. if ($status) {
  361. drupal_set_message(t('The selected file(s) have been deleted.'));
  362. }
  363. else {
  364. drupal_set_message(t('One or more files could not be deleted.'), 'warning');
  365. }
  366. break;
  367. case 'uc_file_upload':
  368. // Build the destination location. We start with the base directory,
  369. // then add any directory which was explicitly selected.
  370. $dir = variable_get('uc_file_base_dir', NULL) . '/';
  371. $dir = (is_null($form_state['values']['upload_dir'])) ? $dir : $dir . $form_state['values']['upload_dir'];
  372. if (is_dir($dir)) {
  373. // Retrieve our uploaded file.
  374. $file_object = $form_state['storage']['temp_file'];
  375. // Copy the file to its final location.
  376. if (copy($file_object->uri, $dir . '/' . $file_object->filename)) {
  377. // Check if any hook_uc_file_action('upload', $args) are implemented.
  378. foreach (module_implements('uc_file_action') as $module) {
  379. $name = $module . '_uc_file_action';
  380. $result = $name('upload', array('file_object' => $file_object, 'form_id' => $form_id, 'form_state' => $form_state));
  381. }
  382. // Update the file list.
  383. uc_file_refresh();
  384. drupal_set_message(t('The file %file has been uploaded to %dir', array('%file' => $file_object->filename, '%dir' => $dir)));
  385. }
  386. else {
  387. drupal_set_message(t('An error occurred while copying the file to %dir', array('%dir' => $dir)), 'error');
  388. }
  389. }
  390. else {
  391. drupal_set_message(t('Can not move file to %dir', array('%dir' => $dir)), 'error');
  392. }
  393. break;
  394. default:
  395. // This action isn't handled by us, so check if any
  396. // hook_uc_file_action('submit', $args) are implemented.
  397. foreach (module_implements('uc_file_action') as $module) {
  398. $name = $module . '_uc_file_action';
  399. $result = $name('submit', array('form_id' => $form_id, 'form_state' => $form_state));
  400. }
  401. break;
  402. }
  403. // Return to the original form state.
  404. $form_state['rebuild'] = FALSE;
  405. drupal_goto('admin/store/products/files');
  406. }
  407. /**
  408. * TODO: Replace with == operator?
  409. */
  410. function _uc_file_display_arrays_equivalent($recur, $no_recur) {
  411. // Different sizes.
  412. if (count($recur) != count($no_recur)) {
  413. return FALSE;
  414. }
  415. // Check the elements.
  416. for ($i = 0; $i < count($recur); $i++) {
  417. if ($recur[$i] != $no_recur[$i]) {
  418. return FALSE;
  419. }
  420. }
  421. return TRUE;
  422. }
  423. /**
  424. * Shows all possible files in selectable list.
  425. */
  426. function _uc_file_build_js_file_display($file_ids) {
  427. // Gather the files if recursion IS selected.
  428. // Get 'em all ready to be punched into the file list.
  429. $recursion_file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, TRUE));
  430. foreach ($recursion_file_ids as $file_id) {
  431. $file = uc_file_get_by_id($file_id);
  432. $recursion[] = '<li>' . $file->filename . '</li>';
  433. }
  434. // Gather the files if recursion ISN'T selected.
  435. // Get 'em all ready to be punched into the file list.
  436. $no_recursion_file_ids = $file_ids;
  437. foreach ($no_recursion_file_ids as $file_id) {
  438. $file = uc_file_get_by_id($file_id);
  439. $no_recursion[] = '<li>' . $file->filename . '</li>';
  440. }
  441. // We'll disable the recursion checkbox if they're equal.
  442. $equivalent = _uc_file_display_arrays_equivalent($recursion_file_ids, $no_recursion_file_ids);
  443. // The list to show if the recursion checkbox is $key.
  444. $result[TRUE] = $equivalent ? FALSE : $recursion;
  445. $result[FALSE] = $no_recursion;
  446. // Set up some JS to dynamically update the list based on the
  447. // recursion checkbox state.
  448. drupal_add_js('uc_file_list[false] = ' . drupal_json_encode('<li>' . implode('</li><li>', $no_recursion) . '</li>') . ';', 'inline');
  449. drupal_add_js('uc_file_list[true] = ' . drupal_json_encode('<li>' . implode('</li><li>', $recursion) . '</li>') . ';', 'inline');
  450. return $result;
  451. }