From 6c656a0b26d66f5c0ad85c8af9b23a082daf984b Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Thu, 15 Nov 2018 11:29:15 +0100 Subject: [PATCH] Handle order documents --- .../wiaas/assets/js/wiaas-admin-package.js | 46 +++++++++++++++ .../documents/views/html-document-form.php | 18 +++++- .../views/html-product-documents.php | 59 +++---------------- .../includes/api/class-wiaas-document-api.php | 55 ++++++++++++----- .../wiaas/includes/class-wiaas-order.php | 9 +-- .../class-wiaas-order-fields.php | 26 +++++--- .../class-wiaas-document-download.php | 47 ++++++++++++++- .../document/wiaas-document-functions.php | 29 ++++++++- .../orders/components/OrderDocuments.jsx | 19 ++++-- .../orders/components/OrderDocumentsGroup.jsx | 16 +---- 10 files changed, 216 insertions(+), 108 deletions(-) diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-admin-package.js b/backend/app/plugins/wiaas/assets/js/wiaas-admin-package.js index 20afebd..0b0ca25 100644 --- a/backend/app/plugins/wiaas/assets/js/wiaas-admin-package.js +++ b/backend/app/plugins/wiaas/assets/js/wiaas-admin-package.js @@ -8,4 +8,50 @@ jQuery(document).ready(function($) { $('#general_product_data').find('.pricing').addClass('hide_if_wiaastemplate hide_if_bundle'); $('#general_product_data').find('.pricing').removeClass('show_if_bundle show_if_wiaastemplate'); + + $( '.wiaas-search-documents' ).each(function() { + var element = $( this ); + var searchTarget = $('#' + element.data('target')); + + element.autocomplete({ + source: function(request, response) { + $.get( window.ajaxurl, { + action: 'wiaas_json_search_documents', + query: request.term, + _ajax_nonce: element.data('search-nonce') + } ).done( function( documents ) { + response( documents || []); + } + ); + }, + select: function(event, ui) { + if (!searchTarget || $('#wiaas_attached_document_' + ui.item.id).length) { + return; + } + + $.get(window.ajaxurl, { + action: 'wiaas_link_document', + _ajax_nonce: element.data('link-nonce'), + id: ui.item.id + }).done( function (document) { + searchTarget.find('tbody').append(document); + }); + + } + }) + .autocomplete( 'instance' )._renderItem = function( ul, item ) { + return $( '
  • ' ) + .text( item.name ) + .appendTo( ul ); + }; + }); + + $('#wiaas_attached_documents').delegate('.wiaas-remove-attached-document', 'click', function (e) { + e.preventDefault(); + + var id = $(this).data('id'); + + $('#wiaas_attached_document_' + id).remove(); + }); + }); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/admin/documents/views/html-document-form.php b/backend/app/plugins/wiaas/includes/admin/documents/views/html-document-form.php index 0cd880a..797c30e 100644 --- a/backend/app/plugins/wiaas/includes/admin/documents/views/html-document-form.php +++ b/backend/app/plugins/wiaas/includes/admin/documents/views/html-document-form.php @@ -152,18 +152,29 @@ $insert_new_document = $post->post_type !== 'wiaas_doc'; response = response.response; + var infoContainer = jQuery('#wiaas_upload_info'); + if (response.substring(0, 6) === 'ERROR:') { - jQuery('#wiaas_upload_errors').html('' + - '' + + infoContainer.html('' + + '' + '   ' + response.substring(6, response.length) + ''); + return; } + infoContainer.html('' + + '' + + '   ' + 'Document uploaded and linked' + + ''); + $('#wiaas_attached_documents').find('tbody').append(response); + + $('.wiaas_documents_tab a').click(); + @@ -252,7 +263,8 @@ $insert_new_document = $post->post_type !== 'wiaas_doc'; -
    +
    +
    diff --git a/backend/app/plugins/wiaas/includes/admin/documents/views/html-product-documents.php b/backend/app/plugins/wiaas/includes/admin/documents/views/html-product-documents.php index 4bdbe45..a70001c 100644 --- a/backend/app/plugins/wiaas/includes/admin/documents/views/html-product-documents.php +++ b/backend/app/plugins/wiaas/includes/admin/documents/views/html-product-documents.php @@ -8,62 +8,17 @@ if ( ! defined( 'ABSPATH' ) ) { } ?> - -

    - +

    diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-document-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-document-api.php index 7480c52..127a98f 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-document-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-document-api.php @@ -5,7 +5,6 @@ if ( ! defined( 'ABSPATH' ) ) { } /** - * TODO: This is temporary implemetation and will probably be changed during work on pending wiaas cart task * Class Wiaas_Document_API */ class Wiaas_Document_API { @@ -30,30 +29,26 @@ class Wiaas_Document_API { ) ) ); - register_rest_route( self::$namespace, 'documents/order/(?P\d+)/(?P[\w-]+)', array( + register_rest_route( self::$namespace, 'order/(?P\d+)/item/(?P\d+)/document/(?P[\w-]+)', array( 'args' => array( 'id' => array( 'description' => __( 'Order ID.', 'wiaas' ), 'type' => 'integer', 'sanitize_callback' => 'absint', - ), - 'type' => array( - 'description' => __( 'Order document type.', 'wiaas' ), - 'type' => 'string', - 'sanitize_callback' => 'sanitize_key', - ), + ) ), array( 'methods' => 'GET', 'permission_callback' => 'is_user_logged_in', - 'callback' => array(__CLASS__, 'download_order_document'), + 'callback' => array(__CLASS__, 'download_order_item_document'), 'args' => array( 'item_id' => array( 'description' => __( 'Package Order Item ID.', 'wiaas' ), 'type' => 'integer', 'sanitize_callback' => 'absint', + 'required' => true ), - 'document_key' => array( + 'key' => array( 'description' => __( 'Unique key identifier for order document.', 'wiaas' ), 'type' => 'string', 'sanitize_callback' => 'sanitize_key', @@ -62,6 +57,29 @@ class Wiaas_Document_API { ) ) ) ); + + register_rest_route(self::$namespace, 'order/(?P\d+)/document/(?P[\w-]+)', array( + 'args' => array( + 'id' => array( + 'description' => __( 'Order ID.', 'wiaas' ), + 'type' => 'integer', + 'sanitize_callback' => 'absint', + ) + ), + array( + 'methods' => 'GET', + 'permission_callback' => 'is_user_logged_in', + 'callback' => array(__CLASS__, 'download_order_other_document'), + 'args' => array( + 'key' => array( + 'description' => __( 'Unique key identifier for order document.', 'wiaas' ), + 'type' => 'string', + 'sanitize_callback' => 'sanitize_key', + 'required' => true + ), + ) + ) + )); } /** @@ -76,14 +94,23 @@ class Wiaas_Document_API { } /** - * Download order document + * Download order item document + * * @param WP_REST_Request $request */ - public static function download_order_document($request) { + public static function download_order_item_document($request) { Wiaas_Document_Download::download_order_item_document( $request['id'], $request['item_id'], - $request['type'], - $request['document_key']); + $request['key']); + } + + /** + * Download order document + * + * @param WP_REST_Request $request + */ + public static function download_order_other_document($request) { + Wiaas_Document_Download::download_order_other_document($request['id'], $request['key']); } } \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index 8c3cd55..16d1e98 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -681,19 +681,12 @@ class Wiaas_Order { if (wiaas_is_order_item__standard_package($order_item)) { $documents = wiaas_get_standard_package_order_item_documents($order, $product_line['id']); - if (! empty($order_item['wiaas_delivery_documents'])) { - - $documents = array_merge($documents, $order_item['wiaas_delivery_documents']); - } - $data['line_items'][$index] ['documents'] = $documents; } } - $order_delivery_documents = $order->get_meta('wiaas_delivery_documents', true); - $order_delivery_documents = empty($order_delivery_documents) ? array() : $order_delivery_documents; - $data['documents'] = $order_delivery_documents; + $data['documents'] = wiaas_get_order_other_documents($order->get_id()); return $data; } diff --git a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php index 072f7d2..32679c8 100644 --- a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php +++ b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php @@ -96,7 +96,7 @@ class Wiaas_Order_Fields { continue; } - $bundle_item->update_meta_data('wiaas_installation_date', $new_value); + $bundle_item->update_meta_data('_wiaas_installation_date', $new_value); $bundle_item->save_meta_data(); break; @@ -121,7 +121,7 @@ class Wiaas_Order_Fields { continue; } - $bundle_documents = $bundle_item['wiaas_delivery_documents']; + $bundle_documents = $bundle_item->get_meta('_wiaas_documents', true); if (empty($bundle_documents)) { $bundle_documents = array(); } @@ -140,15 +140,20 @@ class Wiaas_Order_Fields { foreach ($added_documents as $added_document) { $info = pathinfo( $added_document ); + $dir = wp_upload_dir(); + $relative_file_path = str_replace($dir['baseurl'] . '/', '', $added_document); + $bundle_documents[] = array( - 'name' => $info['basename'], + 'key' => wp_generate_uuid4(), + 'name' => $info['filename'], 'extension' => $info['extension'], + 'version' => $relative_file_path, 'url' => $field->get_download_url( $added_document, true ), 'type' => $field->wiaasDocTypeFilter ); } - $bundle_item->update_meta_data('wiaas_delivery_documents', $bundle_documents); + $bundle_item->update_meta_data('_wiaas_documents', $bundle_documents); $bundle_item->save_meta_data(); break; @@ -168,7 +173,7 @@ class Wiaas_Order_Fields { $added_documents = $new_documents; } - $order_documents = $order->get_meta('wiaas_delivery_documents', true); + $order_documents = $order->get_meta('_wiaas_other_documents', true); if (empty($order_documents)) { $order_documents = array(); } @@ -176,15 +181,20 @@ class Wiaas_Order_Fields { foreach ($added_documents as $added_document) { $info = pathinfo( $added_document ); + $dir = wp_upload_dir(); + $relative_file_path = str_replace($dir['baseurl'] . '/', '', $added_document); + $order_documents[] = array( - 'name' => $info['basename'], + 'key' => wp_generate_uuid4(), + 'name' => $info['filename'], 'extension' => $info['extension'], + 'version' => $relative_file_path, 'url' => $field->get_download_url( $added_document, true ), 'type' => $field->wiaasDocTypeFilter ); } - $order->update_meta_data('wiaas_delivery_documents', $order_documents); + $order->update_meta_data('_wiaas_other_documents', $order_documents); $order->save_meta_data(); break; @@ -216,7 +226,7 @@ class Wiaas_Order_Fields { continue; } - $bundle_item->update_meta_data('wiaas_installation', $selected_installation->get_id()); + $bundle_item->update_meta_data('_wiaas_installation', $selected_installation->get_id()); $bundle_item->save_meta_data(); } } diff --git a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php index 916d113..ffc8f01 100644 --- a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php +++ b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php @@ -83,6 +83,49 @@ class Wiaas_Document_Download { ); } + /** + * Download order other document (not binded to any bundle) + * @param int $order_id + * @param string $document_key + */ + public static function download_order_other_document($order_id, $document_key) { + $order = wc_get_order($order_id); + if (!$order) { + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); + } + + $order_other_documents = wiaas_get_order_other_documents($order_id); + + $order_document = null; + + foreach ($order_other_documents as $order_other_document) { + + if ($order_other_document['key'] === $document_key) { + + $order_document = $order_other_document; + break; + } + } + + if (!isset($order_document)) { + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); + } + + $file_path = wiaas_get_document_version_path($order_document['version']); + + error_log($file_path); + + if (!file_exists($file_path)) { + wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) ); + } + + WC_Download_Handler::download_file_force( + $file_path, + pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION ) + ); + + } + /** * Download document related to order item * @@ -91,7 +134,7 @@ class Wiaas_Document_Download { * @param $type * @param $document_key */ - public static function download_order_item_document($order_id, $item_id, $type, $document_key) { + public static function download_order_item_document($order_id, $item_id, $document_key) { $order = wc_get_order($order_id); if (!$order) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); @@ -106,7 +149,7 @@ class Wiaas_Document_Download { $order_document = null; foreach ($item_documents as $item_document) { - if ($item_document['key'] === $document_key && $item_document['type'] === $type) { + if ($item_document['key'] === $document_key) { $order_document = $item_document; break; } diff --git a/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php b/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php index 524b2ef..e8e66b2 100644 --- a/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php +++ b/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php @@ -26,6 +26,17 @@ function wiaas_documents_upload_dir() { * @return string */ function wiaas_get_document_version_path($version) { + + // documents uploaded with gravity forms have relevant path to form entry upload folder + if (strpos($version, 'gravity_forms') !== false) { + + $wp_uploads = wp_upload_dir(); + $wp_uploads_dir = $wp_uploads['basedir']; + + return $wp_uploads_dir . '/' . $version; + + } + return wiaas_documents_upload_dir() . '/' . $version; } @@ -182,7 +193,7 @@ function wiaas_get_standard_package_order_item_documents($order, $package_item_i return array_map(function($doc) { // append document extension and name information - $doc['extension'] = wiaas_get_doc_version_extension($doc['version']); + $doc['extension'] = isset($doc['extension']) ? $doc['extension'] : wiaas_get_doc_version_extension($doc['version']); $doc['name'] = isset($doc['name']) ? $doc['name'] : wiaas_get_doc_version_filename($doc['version']); return $doc; @@ -222,4 +233,20 @@ function wiaas_get_order_item_documents($order_item, $doc_type = null) { } return $filtered_documents; +} + +/** + * Retrieve order other documents (not binded to any order item) + * + * @param int $order_id + * + * @return array|mixed + */ +function wiaas_get_order_other_documents($order_id) { + + $order = wc_get_order($order_id); + + $documents = $order->get_meta('_wiaas_other_documents', true); + + return empty($documents) ? array() : $documents; } \ No newline at end of file diff --git a/frontend/src/containers/orders/components/OrderDocuments.jsx b/frontend/src/containers/orders/components/OrderDocuments.jsx index b7139ec..13478f1 100644 --- a/frontend/src/containers/orders/components/OrderDocuments.jsx +++ b/frontend/src/containers/orders/components/OrderDocuments.jsx @@ -3,8 +3,18 @@ import {connect} from 'react-redux'; import OrderDocumentsGroup from './OrderDocumentsGroup.jsx'; import {orderTexts} from '../../../constants/ordersConstants'; import WiaasBox from "../../../mainComponents/box/WiaasBox"; +import {API_SERVER} from "../../../config"; +import FileDownloader from "../../../helpers/FileDownloader"; + +const fileHandler = new FileDownloader(); class OrderDocuments extends Component { + + downloadDocument(orderId, document){ + const fileUrl = `${API_SERVER}/wp-json/wiaas/order/${orderId}/document/${document.key}`; + fileHandler.download(fileUrl, `${document.name}.${document.extension}`); + } + render() { const {orderInfo} = this.props; @@ -23,14 +33,13 @@ class OrderDocuments extends Component { { orderInfo.documents.map((document, index) => ( - - - + + )) } diff --git a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx index 1876b37..d423ed6 100644 --- a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx +++ b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx @@ -13,7 +13,7 @@ class OrderDocumentsGroup extends Component { } downloadDocument(orderId, itemId, document){ - const fileUrl = `${API_SERVER}/wp-json/wiaas/documents/order/${orderId}/${document.type}?item_id=${itemId}&document_key=${document.key}`; + const fileUrl = `${API_SERVER}/wp-json/wiaas/order/${orderId}/item/${itemId}/document/${document.key}`; fileHandler.download(fileUrl, `${document.name}.${document.extension}`); } @@ -28,20 +28,6 @@ class OrderDocumentsGroup extends Component { { documentsGroup.documents.map((document, index) => { - if (document.url) { - - return ( - -
    - -
    - {document.name} -
    -
    -
    - ); - } - return (
    {this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big">