Ejemplo n.º 1
0
 /**
  * Display a form allowing to create an entity from frontend with a shortcode
  * @param array $shortcode_args The different parameters for the shortocde: the field list for the form, different parameters for the entity to create
  */
 public static function wpshop_entities_shortcode($shortcode_args)
 {
     global $wpshop_account, $wpdb;
     $output = $form_content = '';
     if (get_current_user_id() > 0) {
         if (!empty($_POST['quick_entity_add_button'])) {
             $attributes = array();
             foreach ($_POST['attribute'] as $attribute_type => $attribute) {
                 foreach ($attribute as $attribute_code => $attribute_value) {
                     $attributes[$attribute_code] = $attribute_value;
                 }
             }
             $result = wpshop_products::addProduct($_POST['wp_fields']['post_title'], '', $attributes, 'complete');
         }
         if (empty($shortcode_args['attribute_set_id']) || empty($shortcode_args['post_type'])) {
             $output = __('This form page is invalid because no set or type or content is defined. Please contact administrator with this error message', 'wpshop');
         } else {
             $entity_identifier = wpshop_entities::get_entity_identifier_from_code($shortcode_args['post_type']);
             $attribute_set_def = wpshop_attributes_set::getElement($shortcode_args['attribute_set_id'], "'valid'");
             if (empty($entity_identifier) || empty($attribute_set_def) || $entity_identifier != $attribute_set_def->entity_id) {
                 $output = __('This form page is invalid because type and set are not linked. Please contact administrator with this error message', 'wpshop');
             } else {
                 /** Display wordpress fields */
                 foreach (explode(', ', $shortcode_args['fields']) as $field_name) {
                     $label = '';
                     switch ($field_name) {
                         case 'post_title':
                             switch ($shortcode_args['post_type']) {
                                 case WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT:
                                     $label = __('Product title', 'wpshop');
                                     break;
                                 default:
                                     $label = __('Name', 'wpshop');
                                     break;
                             }
                             $field_type = 'text';
                             break;
                         case 'post_thumbnail':
                             switch ($shortcode_args['post_type']) {
                                 case WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT:
                                     $label = __('Product picture', 'wpshop');
                                     break;
                                 default:
                                     $label = __('Default picture', 'wpshop');
                                     break;
                             }
                             $field_type = 'file';
                             break;
                     }
                     if (!empty($label)) {
                         $template_part = 'quick_entity_wp_internal_field_' . $field_type;
                         $tpl_component = array();
                         $tpl_component['WP_FIELD_NAME'] = $field_name;
                         $tpl_component['WP_FIELD_VALUE'] = '';
                         $input = wpshop_display::display_template_element($template_part, $tpl_component);
                         unset($tpl_component);
                         $template_part = 'quick_entity_wp_internal_field_output';
                         $tpl_component = array();
                         $tpl_component['ENTITY_TYPE_TO_CREATE'] = $shortcode_args['post_type'];
                         $tpl_component['WP_FIELD_NAME'] = $field_name;
                         $tpl_component['WP_FIELD_LABEL'] = $label;
                         $tpl_component['WP_FIELD_INPUT'] = $input;
                         $form_content .= wpshop_display::display_template_element($template_part, $tpl_component);
                         unset($tpl_component);
                     }
                 }
                 /** Display attributes fields	*/
                 $query = $wpdb->prepare("\nSELECT ATT.code\nFROM " . WPSHOP_DBT_ATTRIBUTE . " AS ATT\n\tINNER JOIN " . WPSHOP_DBT_ATTRIBUTE_DETAILS . " AS ATTR_DET ON ((ATTR_DET.status = 'valid') AND (ATTR_DET.attribute_id = ATT.id) AND (ATTR_DET.entity_type_id = ATT.entity_id))\n\tINNER JOIN " . WPSHOP_DBT_ATTRIBUTE_GROUP . " AS ATT_GROUP ON ((ATT_GROUP.status = 'valid') AND (ATT_GROUP.attribute_set_id = ATTR_DET.attribute_set_id) AND (ATT_GROUP.id = ATTR_DET.attribute_group_id))\nWHERE ATT.is_used_in_quick_add_form = %s\n\tAND ATT.status= %s\n\tAND ATT.entity_id = %d\n\tAND ATTR_DET.attribute_set_id = %d\nGROUP BY ATT.code\nORDER BY ATT_GROUP.position, ATTR_DET.position", 'yes', 'valid', wpshop_entities::get_entity_identifier_from_code($shortcode_args['post_type']), $shortcode_args['attribute_set_id']);
                 $attribute_for_creation = $wpdb->get_results($query);
                 foreach ($attribute_for_creation as $attribute) {
                     $attr_field = wpshop_attributes::display_attribute($attribute->code, 'frontend');
                     $form_content .= $attr_field['field'];
                 }
                 /**	Check if there are extra parameters	*/
                 if (!empty($shortcode_args['extra_element'])) {
                     $extra_element = explode(', ', $shortcode_args['extra_element']);
                     foreach ($extra_element as $element) {
                         $element_def = explode('!#wps#!', $element);
                         $element_type = $element_def[0];
                         $element_id = $element_def[1];
                         if ($element_type == WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS) {
                             $form_content .= '<div class="wpshop_entity_address_container">';
                             // 								$form_content .= $wpshop_account->display_form_fields($element_id, null, 'not');
                             $form_content .= '</div><div class="wpshop_cls"></div>';
                         }
                     }
                 }
             }
         }
         $template_part = 'quick_entity_add_form';
         $tpl_component = array();
         $tpl_component['ENTITY_TYPE'] = $shortcode_args['post_type'];
         $tpl_component['ENTITY_ATTRIBUTE_SET_ID'] = !empty($shortcode_args['attribute_set_id']) ? $shortcode_args['attribute_set_id'] : 0;
         $tpl_component['NEW_ENTITY_FORM_DETAILS'] = $form_content;
         $tpl_component['ENTITY_QUICK_ADDING_FORM_NONCE'] = wp_create_nonce("wpshop_add_new_entity_ajax_nonce");
         $tpl_component['ENTITY_QUICK_ADD_BUTTON_TEXT'] = __($shortcode_args['button_text'], 'wpshop');
         /*	Ajout de la boite permettant d'ajouter des valeurs aux attributs de type liste deroulante a la volee	*/
         $dialog_title = __('New value', 'wpshop');
         $dialog_identifier = 'new_value_for_entity';
         $dialog_input_identifier = 'wpshop_new_attribute_option_value';
         ob_start();
         include WPSHOP_TEMPLATES_DIR . 'admin/add_new_element_dialog.tpl.php';
         $tpl_component['DIALOG_BOX'] = ob_get_contents();
         ob_end_clean();
         $tpl_component['DIALOG_BOX'] .= '<input type="hidden" name="wpshop_attribute_type_select_code" value="" id="wpshop_attribute_type_select_code" />';
         $tpl_component['DIALOG_BOX'] = '';
         $output = wpshop_display::display_template_element($template_part, $tpl_component, array(), 'wpshop');
         echo $output;
     } else {
         echo $wpshop_account->display_login_form();
     }
 }