228 lines
7.0 KiB
PHP
228 lines
7.0 KiB
PHP
<?php
|
|
|
|
if ( ! class_exists( 'GFForms' ) ) {
|
|
die();
|
|
}
|
|
|
|
class Wiaas_Order_Fields {
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'gform_field_standard_settings', array( __CLASS__, 'field_settings' ) );
|
|
|
|
add_filter('gform_fileupload_entry_value_file_path', array( __CLASS__, 'display_order_document_fields' ), 999, 2);
|
|
}
|
|
|
|
|
|
public static function display_order_document_fields($file_path, $field) {
|
|
|
|
if ($field->type === 'wiaas_order_bundle_document') {
|
|
|
|
$field = new Wiaas_Field_Order_Bundle_Document();
|
|
|
|
return $field->get_download_url($file_path);
|
|
}
|
|
|
|
return $file_path;
|
|
}
|
|
|
|
/**
|
|
* Add custom settings for form order fields
|
|
*
|
|
* @param int $position
|
|
*/
|
|
public static function field_settings( $position ) {
|
|
if ( $position === 20 ) {
|
|
// After Field description setting.
|
|
?>
|
|
|
|
<li class="wiaas_doc_type_filter field_setting">
|
|
|
|
<label for="wiaas-doc-type-filter" class="section_label">
|
|
<?php esc_html_e( 'Document Type', 'wiaas' ); ?>
|
|
</label>
|
|
<select id="wiaas-doc-type-filter" onchange="SetFieldProperty('wiaasDocTypeFilter',this.value);">
|
|
<option value="configuration"><?php esc_html_e( 'Configuration', 'wiaas' ); ?></option>
|
|
<option value="order_questionaire"><?php esc_html_e( 'Order Questionaire', 'wiaas' ); ?></option>
|
|
<option value="installation_protocol"><?php esc_html_e( 'Installation protocol', 'wiaas' ); ?></option>
|
|
<option value="install_guide"><?php esc_html_e( 'Installation Guide', 'wiaas' ); ?></option>
|
|
<option value="customer_acceptance"><?php esc_html_e( 'Customer acceptance', 'wiaas' ); ?></option>
|
|
</select>
|
|
|
|
</li>
|
|
|
|
<li class="wiaas_installation_organization_filter field_setting">
|
|
<input type="checkbox" id="wiaas-installation-organization-filter"
|
|
onclick="var value = jQuery(this).is(':checked'); SetFieldProperty('wiaasOnlyInstallationOrg', value);""/>
|
|
<label for="wiaas-installation-organization-filter" class="inline">
|
|
<?php esc_html_e( 'Only installation', 'wiaas' ); ?>
|
|
</label>
|
|
</li>
|
|
|
|
<?php
|
|
}
|
|
}
|
|
|
|
public static function get_value_from_order_field($entry, $field) {
|
|
|
|
switch ($field->type) {
|
|
|
|
case 'wiaas_order_bundle':
|
|
$value = $entry[$field->id];
|
|
list ($order_id, $item_id) = explode('|', $value);
|
|
|
|
if ( ! empty($order_id) && ! empty($item_id)) {
|
|
|
|
return array(
|
|
'id' => $item_id,
|
|
'name' => $field->get_selected_bundle_display_name($value)
|
|
);
|
|
}
|
|
|
|
return null;
|
|
|
|
case '':
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public static function map_order_to_entry($order_id, $form, $bundle_item_id = null) {
|
|
|
|
if (empty($form['fields']) ||
|
|
empty(GFCommon::get_fields_by_type( $form, array('wiaas_order')) ) ) {
|
|
// form does not have order field so cannot be mapped
|
|
return false;
|
|
}
|
|
|
|
$order = wc_get_order($order_id);
|
|
|
|
$entry = array();
|
|
|
|
foreach ($form['fields'] as $field) {
|
|
|
|
switch ($field->type) {
|
|
|
|
case 'wiaas_order':
|
|
|
|
$entry[(string) $field->id] = $order->get_id();
|
|
|
|
break;
|
|
|
|
case 'workflow_user':
|
|
$entry[(string) $field->id] = $order->get_customer_id(); // save customer if needed
|
|
|
|
break;
|
|
|
|
case 'wiaas_order_bundle':
|
|
|
|
$bundle_item = $order->get_item($bundle_item_id);
|
|
|
|
if ( empty($bundle_item) && $field->isRequired) {
|
|
// there is no data for required field so entry cannot be created
|
|
return false;
|
|
}
|
|
|
|
if (! empty($bundle_item)) {
|
|
|
|
$entry[(string) $field->id] = $order->get_id() . '|' . $bundle_item->get_id();
|
|
}
|
|
|
|
break;
|
|
|
|
case 'wiaas_order_bundle_document':
|
|
|
|
$bundle_item = $order->get_item($bundle_item_id);
|
|
|
|
if ( empty($bundle_item) && $field->isRequired) {
|
|
// there is no data for required field so entry cannot be created
|
|
return false;
|
|
}
|
|
|
|
if (! empty($bundle_item)) {
|
|
|
|
$documents = wiaas_get_standard_package_order_item_documents($order, $bundle_item_id);
|
|
|
|
$filtered_documents = array();
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
if ($document['type'] === $field->wiaasDocTypeFilter) {
|
|
|
|
$filtered_documents[] = $document;
|
|
}
|
|
}
|
|
|
|
if ( empty($filtered_documents) && $field->isRequired ) {
|
|
// there is no data for required field so entry cannot be created
|
|
return false;
|
|
}
|
|
|
|
if (! empty ($filtered_documents) && ! $field->multipleFiles) {
|
|
|
|
$document = $filtered_documents[0];
|
|
|
|
$entry[$field->id] = $document['version'];
|
|
|
|
} else if (! empty ($filtered_documents) ) {
|
|
|
|
$versions = array();
|
|
foreach ($filtered_documents as $filtered_document) {
|
|
$versions[] = $filtered_document['version'];
|
|
}
|
|
|
|
$entry[$field->id] = json_encode($versions);
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case 'wiaas_order_installation_select':
|
|
|
|
$bundle_item = $order->get_item($bundle_item_id);
|
|
|
|
if ( empty($bundle_item) && $field->isRequired) {
|
|
// there is no data for required field so entry cannot be created
|
|
return false;
|
|
}
|
|
|
|
$bundled_items = wc_pb_get_bundled_order_items($bundle_item, $order);
|
|
|
|
$installation_items = array();
|
|
|
|
foreach ($bundled_items as $id => $bundled_item) {
|
|
|
|
$product = $bundled_item->get_product();
|
|
|
|
if ($product && Wiaas_Product_Category::is_installation($product)) {
|
|
|
|
$installation_items[] = $bundled_item;
|
|
}
|
|
}
|
|
|
|
if (empty($installation_items) && $field->isRequired) {
|
|
// there is no data for required field so entry cannot be created
|
|
return false;
|
|
}
|
|
|
|
if (count($installation_items) === 1) {
|
|
|
|
$installation_item = $installation_items[0];
|
|
|
|
$entry[(string) $field->id] = 'wiaas_installation_' . $order->get_id() . '|' . $installation_item->get_id();
|
|
} else if (count($installation_items) > 1) {
|
|
|
|
// force admin to select installation
|
|
$entry[(string) $field->id] = '0';
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return $entry;
|
|
}
|
|
|
|
}
|
|
|
|
Wiaas_Order_Fields::init();
|