uc_file.admin.inc 17 KB

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