static function getAllTemplateNames()
 {
     global $wpdb;
     $table = $wpdb->prefix . self::TABLENAME;
     $results = $wpdb->get_results("\n\t\t\tSELECT profile_id, tpl_id \n\t\t\tFROM {$table}\n\t\t");
     $templates = array();
     foreach ($results as $result) {
         $template = WPLA_AmazonFeedTemplate::getFeedTemplate($result->tpl_id);
         $templates[$result->profile_id] = $template ? $template->title : false;
     }
     return $templates;
 }
 public function displayCategoriesPage()
 {
     $templates = WPLA_AmazonFeedTemplate::getAll();
     $active_templates = array();
     foreach ($templates as $template) {
         $tpl_name = $template->name == 'Offer' ? 'ListingLoader' : $template->name;
         $active_templates[] = $template->site_id . $tpl_name;
     }
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'file_index' => WPLA_FeedTemplateIndex::get_file_index(), 'active_templates' => $active_templates, 'installed_templates' => $templates, 'settings_url' => 'admin.php?page=' . self::ParentMenuId . '-settings', 'form_action' => 'admin.php?page=' . self::ParentMenuId . '-settings' . '&tab=categories');
     $this->display('settings_tpl_btg', $aData);
 }
 static function buildNewProductsFeedData($items, $account_id, $profile, $append_feed = false)
 {
     if (!$profile || !$profile->id) {
         WPLA()->logger->info('no profile found, falling back to ListingLoader (Offer)');
         return self::buildListingLoaderFeedData($items, $account_id, $append_feed);
     }
     $template = new WPLA_AmazonFeedTemplate($profile->tpl_id);
     if (!$template || !$template->id) {
         WPLA()->logger->info('no template, falling back to ListingLoader (Offer) - tpl_id: ' . $profile->tpl_id);
         return self::buildListingLoaderFeedData($items, $account_id, $append_feed);
     }
     $columns = $template->getFieldNames();
     $profile_fields = maybe_unserialize($profile->fields);
     // echo "<pre>";print_r($items);echo"</pre>";#die();
     // echo "<pre>";print_r($template);echo"</pre>";#die();
     // echo "<pre>";print_r($profile);echo"</pre>";#die();
     // echo "<pre>";print_r($columns);echo"</pre>";#die();
     // echo "<pre>";print_r($profile_fields);echo"</pre>";#die();
     if (!$columns) {
         WPLA()->logger->error('no columns found in template - tpl_id: ' . $profile->tpl_id);
         WPLA()->logger->info('profile: ' . print_r($profile, 1));
         WPLA()->logger->info('template: ' . print_r($template, 1));
         WPLA()->logger->info('columns: ' . print_r($columns, 1));
         WPLA()->logger->info('items: ' . print_r($items, 1));
         return '';
     }
     // add variation columns
     // (not really needed - if the template doesn't already have them, variations are probably not allowed or the template is outdated)
     if ($template->name != 'Offer') {
         // $profile_details = maybe_unserialize( $profile->details );
         // $variations_mode = isset( $profile_details['variations_mode'] ) ? $profile_details['variations_mode'] : 'default';
         // if ( $variations_mode != 'flat' ) {
         // 	$columns[] = 'parent-sku';
         // 	$columns[] = 'parentage';
         // 	$columns[] = 'relationship-type';
         // 	$columns[] = 'variation-theme';
         // }
     }
     // header
     $csv_header = 'TemplateType=' . $template->name . "\t" . 'Version=' . $template->version . str_repeat("\t", sizeof($columns) - 2) . "\n";
     $csv_header .= join("\t", $columns) . "\n";
     if ($template->name != 'Offer') {
         $csv_header .= join("\t", $columns) . "\n";
     }
     $csv_body = '';
     // loop products
     foreach ($items as $item) {
         // get WooCommerce product data
         $product_id = $item['post_id'];
         $product = get_product($product_id);
         if (!$product) {
             continue;
         }
         if (!$item['sku']) {
             continue;
         }
         WPLA()->logger->debug('processing item ' . $item['sku'] . ' - ID ' . $product_id);
         // reset row cache
         WPLA()->memcache->clearColumnCache();
         // process product
         foreach ($columns as $col) {
             $value = self::parseProductColumn($col, $item, $product, $profile);
             $value = apply_filters('wpla_filter_listing_feed_column', $value, $col, $item, $product, $profile, $template->name);
             $value = str_replace(array("\t", "\n", "\r"), ' ', $value);
             // make sure there are no tabs or line breaks in any field
             $csv_body .= $value . "\t";
             WPLA()->memcache->setColumnValue($product->sku, $col, $value);
         }
         $csv_body .= "\n";
     }
     // check if any rows were created
     if (!$csv_body) {
         return self::return_csv_object();
     }
     // only return body when appending feed
     if ($append_feed) {
         return self::return_csv_object($csv_body, '', $template->name);
     }
     // return csv object
     return self::return_csv_object($csv_body, $csv_header, $template->name);
 }
 public function displayEditPage()
 {
     // init model
     // get item
     if ($this->requestAction() == 'add_new_profile') {
         $profile = new WPLA_AmazonProfile();
     } else {
         $profile = new WPLA_AmazonProfile($_REQUEST['profile']);
     }
     // $listingsModel = new ListingsModel();
     // $prepared_listings  = $listingsModel->getAllPreparedWithProfile( $item['profile_id'] );
     // $verified_listings  = $listingsModel->getAllVerifiedWithProfile( $item['profile_id'] );
     // $published_listings = $listingsModel->getAllPublishedWithProfile( $item['profile_id'] );
     // $ended_listings     = $listingsModel->getAllEndedWithProfile( $item['profile_id'] );
     $lm = new WPLA_ListingsModel();
     $listings = $profile->profile_id ? $lm->findAllListingsByColumn($profile->profile_id, 'profile_id') : array();
     $accounts = WPLA_AmazonAccount::getAll();
     $templates = WPLA_AmazonFeedTemplate::getAll();
     // separate ListingLoader templates
     $category_templates = array();
     $liloader_templates = array();
     foreach ($templates as $tpl) {
         if ($tpl->title == 'Offer') {
             $tpl->title = "Listing Loader";
             $liloader_templates[] = $tpl;
         } else {
             $category_templates[] = $tpl;
         }
     }
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'profile' => $profile, 'accounts' => $accounts, 'category_templates' => $category_templates, 'liloader_templates' => $liloader_templates, 'profile_listings' => $listings, 'profile_details' => maybe_unserialize($profile->details), 'form_action' => 'admin.php?page=' . self::ParentMenuId . '-profiles');
     // $this->display( 'profiles_edit_page', array_merge( $aData, $profile ) );
     $this->display('profiles_edit_page', $aData);
 }
 public function ajax_wpla_load_template_data_for_product()
 {
     // TODO: check nonce
     if (isset($_REQUEST['tpl_id'])) {
         $template = new WPLA_AmazonFeedTemplate($_REQUEST['tpl_id']);
         $post_id = $_REQUEST['post_id'];
         $field_data = get_post_meta($post_id, '_wpla_custom_feed_columns', true);
         if ($template) {
             // build settings form
             $data = array();
             $data['fields'] = $template->getFieldData();
             $data['values'] = $template->getFieldValues();
             $data['profile_field_data'] = is_array($field_data) ? $field_data : array();
             $data['product_attributes'] = WPLA_ProductWrapper::getAttributeTaxonomies();
             @WPLA_Page::display('profile/edit_field_data', $data);
             exit;
         } else {
             echo "invalid template id";
         }
     }
 }
    function display_feed_template_selector()
    {
        global $post;
        // get templates
        $templates = WPLA_AmazonFeedTemplate::getAll();
        $custom_feed_tpl_id = get_post_meta($post->ID, '_wpla_custom_feed_tpl_id', true);
        // separate ListingLoader templates
        $category_templates = array();
        $liloader_templates = array();
        foreach ($templates as $tpl) {
            if ($tpl->title == 'Offer') {
                $tpl->title = "Listing Loader";
                $liloader_templates[] = $tpl;
            } else {
                $category_templates[] = $tpl;
            }
        }
        // compatibility with profile code
        $wpl_category_templates = $category_templates;
        $wpl_liloader_templates = $liloader_templates;
        ?>
							<label for="wpl-text-tpl_id" class="text_label">
								<?php 
        echo __('Feed Template', 'wpla');
        ?>
                                <?php 
        wpla_tooltip('Each main category on Amazon uses a different feed template with special fields for that particular category.<br>You need to select the right template for your category and make sure all the required fields are filled in - or are populated from product details or attributes.');
        ?>
							</label>
							<select id="wpl-text-tpl_id" name="wpla_tpl_id" class="required-entry select">
							<option value="">-- <?php 
        echo __('Select feed template', 'wpla');
        ?>
 --</option> 
							<optgroup label="Generic Feeds">
								<?php 
        foreach ($wpl_liloader_templates as $tpl) {
            ?>
									<option value="<?php 
            echo $tpl->id;
            ?>
" 
										<?php 
            if ($custom_feed_tpl_id == $tpl->id) {
                ?>
											selected="selected"
										<?php 
            }
            ?>
										<?php 
            $site = new WPLA_AmazonMarket($tpl->site_id);
            ?>
										><?php 
            echo $tpl->title;
            ?>
 (<?php 
            echo $site ? $site->code : '?';
            ?>
)</option>
								<?php 
        }
        ?>
							</optgroup>
							<optgroup label="Category Specific Feeds">
								<?php 
        foreach ($wpl_category_templates as $tpl) {
            ?>
									<option value="<?php 
            echo $tpl->id;
            ?>
" 
										<?php 
            if ($custom_feed_tpl_id == $tpl->id) {
                ?>
											selected="selected"
										<?php 
            }
            ?>
										<?php 
            $site = new WPLA_AmazonMarket($tpl->site_id);
            ?>
										><?php 
            echo $tpl->title;
            ?>
 (<?php 
            echo $site ? $site->code : '?';
            ?>
)</option>
								<?php 
        }
        ?>
							</optgroup>
							</select>
							<br class="clear" />
							<p class="desc" style="">
								<?php 
        $link = sprintf('<a href="%s">%s</a>', 'admin.php?page=wpla-settings&tab=categories', __('Amazon &raquo; Settings &raquo; Categories', 'wpla'));
        ?>
								<?php 
        echo sprintf(__('You can add additional feed templates at %s.', 'wpla'), $link);
        ?>
							</p>


					<div id="FeedDataBox">
						<hr>
						<!-- <h3><span><?php 
        echo __('Feed Attributes', 'wpla');
        ?>
</span></h3> -->
						<div class="x-inside" id="wpla_feed_data_wrapper">
						</div>
					</div>

					<!-- hidden ajax categories tree -->
					<div id="amazon_categories_tree_wrapper">
						<div id="amazon_categories_tree_container">TEST</div>
					</div>

		<?php 
    }
 function column_template($item)
 {
     $fields = maybe_unserialize($item['fields']);
     $template = WPLA_AmazonFeedTemplate::getFeedTemplate($item['tpl_id']);
     $template_title = $template ? $template->title : '<i>' . __('no feed template selected', 'wpla') . '</i>';
     if ($template_title == 'Offer') {
         $template_title = 'Listing Loader';
     }
     return sprintf('%1$s <br><span style="color:silver">%2$s</span>', $template_title, wpla_spacify(@$fields['feed_product_type']));
 }