<?php

//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//\\\       \\\\\\\\|
//\\\ @@    @@\\\\\\| Hierophant Product Plugin
//\\ @@@@  @@@@\\\\\|
//\\\@@@@| @@@@\\\\\|
//\\\ @@ |\\@@\\\\\\| http://www.smilingsouls.net/hierophant
//\\\\  ||   \\\\\\\| (c) Copyright 2008 Richard York, All rights Reserved
//\\\\  \\_   \\\\\\|
//\\\\\        \\\\\| Use and redistribution are subject to the terms of the license.
//\\\\\  ----  \@@@@| http://www.smilingsouls.net/hierophant/license
//@@@@@\       \@@@@|
//@@@@@@\     \@@@@@|
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

class hProduct extends hPlugin {

    private 
$hProduct;
    private 
$hSpotlight;
    private 
$hSpotlightSearch;
    private 
$hApplicationMenu;
    private 
$hDialogue;
    private 
$hForm;

    public function 
hConstructor()
    {
        
$this->hFileHeaders();

        
// Include framework API library plugins
        // hSpotlight - A Search API
        
$this->hSpotlight       $this->hLibrary('hFramework/hSpotlight');
        
$this->hSpotlightSearch $this->hLibrary('hFramework/hSpotlight/hSpotlightSearch');
        
        
// hDialogue - A JavaScript <div> Popup Window API
        
$this->hDialogue        $this->hLibrary('hFramework/hDialogue');
        
        
// hForm - An HTML Form API
        
$this->hForm            $this->hLibrary('hForm');
        
        
// hProduct - A Product Management API
        
$this->hProduct         $this->hLibrary('hProduct');

        
// hApplicationStatus - An animated status notification API
        // e.g., provides a GUI element that let's you know a document's been saved.
        
$this->hPlugin('hFramework/hApplication/hApplicationStatus');

        
// Set the database columns that are allowed in the search component
        
$this->hProduct->setSpotlightSearchColumns();

        
// hApplicationMenu - An Application API that provides drop down menus similar to 
        // Mac OS X.
        
$this->hApplicationMenu $this->hLibrary('hFramework/hApplication/hApplicationMenu');

        
// Don't provide the "Constrain Location" option in the search GUI.
        
$this->hSpotlightConstrainLocation false;

        
        
$this->hFileDocument =
            
// Get the drop down menus for this application
            
$this->hApplicationMenu->getMenu(
                array(
                    
'Products' => array(
                        
''
                    
),
                    
'File' => array(
                        
'Import',
                        
'Export'
                    
)
                )
            ).
            
// Get the search GUI
            
$this->hSpotlight->getSearch('Products'false).
            
// This is where search results will go
            
"<div id='hProductResults'>".
            
"</div>".
            
// Get the product management form
            
$this->getForm().
            
"<div id='hProductButtons'>\n".
            
"  <input type='submit' id='hProductNew' value='New' />\n".
            
"  <input type='submit' id='hProductSave' value='Save' />\n".
            
"</div>\n".
            
$this->getPricingDialogue().
            
$this->getImportDialogue();
    }

    
/**
     * Gets the main form.
     *
     * @return string
     */
    
public function getForm()
    {
        
        
// A form cannot be started until at least one <div> element is added.
        // The first argument is the id name, the second is the tab name
        
$this->hForm->addDiv('hProductSummaryDiv''Summary');
        
$this->hForm->addFieldset('Summary''100%''30%,70%');

        
// Add a hidden input, the first argument is both the id and name, the second is the value.
        
$this->hForm->addHiddenInput('hProductID'0);
        
        
// Add a text input.  The first argument is both the id and name, the second is the label, and the third is the
        // size of the input.
        
$this->hForm->addTextInput('hProductPartNumber''Part Number:'12);

        
$this->hForm->addTextInput('hProductName''Product Name:'25);
        
        
// Add a textarea input, the first argument is both the id and name, the second is the label,
        // the third is the size.  The comma delimits two values, the first is the "cols" attribute, 
        // the 2nd is the "rows" attribute.
        
$this->hForm->addTextareaInput('hProductDescription''Product Description:<br /><i>Plain Text</i> -L''50,5');
        
        
// Add a radio input.  The first argument is the name, ids are automatically created from the name and
        // value of each radio input.  The 2nd argument is the master label that describes the entire collection 
        // of inputs.  Then an array is provided for the options, each key will become the input's value, and each 
        // array value will become the input's label.
        
$this->hForm->addRadioInput(
            
'hProductMatch',
            
'To match this part number:',
            array(
                
=> ' all products must be present.',
                
=> ' any product can be present.'
            
)
        );

        
// The form API lays out the form using an HTML table.  The following adds 
        // a label and associates the label with a blank table cell, similarly to how 
        // previous controls laid out label with input.  This allows you to have 
        // a label associated with arbitrary HTML content, which is useful for AJAX-driven,
        // custom form controls.
        //
        // The label has an option appended "-L", which is designed similarly to command line 
        // arguments.  The "-L" option changes the class name of the enclosing table cell for 
        // the input label so that label can be laid out differently from others.  In this case,
        // the label is "long", so the "-L" option causes the label text to be aligned to the 
        // top of the table cell, rather than the middle, which is the default.
        //
        // The third argument is the HTML content for the table cell the label is associated with.
        
$this->hForm->addData(
            
'hProductFilesCell',
            
'Products: -L',
            
"<ul id='hProductFiles'>".
            
"</ul>"
        
);

        
// A blank table cell is added so that the buttons are correctly positioned.
        
$this->hForm->addTableCell('');

        
// The buttons.  Buttons are more challenging to package into a more simplified API.
        // A few APIs exist for buttons, but only allow one button to a cell.  The following
        // works around that by simply putting the button HTML in a cell without using a
        //  pre-defined API.
        
$this->hForm->addTableCell(
            
"<input type='submit' id='hProductEditFile' value='Edit' /> ".
            
"<input type='submit' id='hProductPricing' value='Pricing' disabled='disabled' /> ".
            
"<input type='submit' id='hProductRemove' value='Remove' /> ".
            
"<input type='submit' id='hProductAdd' value='Add' />"
        
);

//        $hForm->addDiv('hProduct', 'Account');
//        $hForm->addFieldset('Login', '100%', '30%,70%', 'hUserRegistrationSetID');

        // The Dialogue API lays out the form contents with slick, Mac OS X-inspired 
        // styles.  It provides an API for tabbed dialogues, and virtual in-browser
        // popup-window-like layers, that can be used to get around pop-up blocking software.
        
$this->hDialogue->newDialogue('hProductSummary');
        
        
// Sets the form control the dialogue API should use.
        
$this->hDialogue->setForm($this->hForm);

        
// Turn off the dialogue shadow (you would use the shadow if this were a popup layer)
        
$this->hDialogueShadow false;
        
        
// Turns off the titlebar component (you might use that if this were a popup layer)
        
$this->hDialogueTitlebar false;

        
// Gets the form and dialogue markup.
        
return $this->hDialogue->getDialogue();
    }

    
/**
     * Provides an interface for setting product pricing.
     * 
     * @return string
     */
    
public function getPricingDialogue()
    {
        
// Query the database for pricing categories, or tiers.
        
$query db::query(
            
"SELECT `hProductPriceCategoryID`,
                    `hProductPriceCategoryName`
               FROM `hProductPriceCategories`
           ORDER BY `hProductPriceCategoryName` ASC"
        
);

        
$options '';

        
// Define the pricing categories.  Some examples might be, Domestic Pricing,
        // International Pricing, and so on.  Pricing categories can be defined to 
        // be very region specific, down to the zip code level in the U.S., and 
        // per-country, internationally...  although territories could theoretically 
        // be more specific internationlly as well.
        
while ($data db::fetchArray($query))
        {
            
$options .= "<option value='{$data['hProductPriceCategoryID']}'>{$data['hProductPriceCategoryName']}</option>";
        }

        
// Define a new dialogue
        
$this->hDialogue->newDialogue('hProductPrice');
        
        
// Add some dialogue buttons.
        
$this->hDialogue->addButtons('Close''Save');

        
// Get the dialogue HTML.
        
return $this->hDialogue->getDialogue(
            
"<div class='hProductPriceHeaders'>".
                
"<div class='hProductPriceCategoryHeader'>Category</div>".
                
"<div class='hProductPriceHeader'>Price</div>".
            
"</div>".
            
"<div id='hProductPriceWrapper'>".
                
"<div class='hProductPrice hProductPriceTemplate'>".
                    
"<div class='hProductPriceCategoryCell'>".
                        
"<select class='hProductPriceCategory'>".
                            
$options.
                        
"</select>".
                    
"</div>".
                    
"<div class='hProductPriceCell'>".
                        
"\$ <input type='text' class='hProductPrice' value='' size='10' />".
                    
"</div>".
                
"</div>".
            
"</div>".
            
"<div id='hProductPriceToggle'>".
                
"<div>".
                    
"<input type='radio' name='hProductPriceBy' value='0' id='hProductPriceBy-0' />".
                    
"<label for='hProductPriceBy-0'>Price by part number</label>".
                
"</div>".
                
"<div>".
                    
"<input type='radio' name='hProductPriceBy' value='1' id='hProductPriceBy-1' checked='checked' />".
                    
"<label for='hProductPriceBy-1'>Price by products in part number</label>".
                
"</div>".
            
"</div>".
            
"<div id='hProductPriceButtons'>".
                 
"<img src='/images/themes/aqua/icons/misc/add.png' id='hProductPriceAdd' />".
                 
"<img src='/images/themes/aqua/icons/misc/remove.png' id='hProductPriceRemove' />".
            
"</div>",
            
'Set Pricing'
        
);
    }

    
/**
     * This dialogue provides a GUI for importing more complicated products that 
     * can be configured, and have a more exponential combination of options.
     * 
     * The interface lets you associate product objects with columns in a CSV file, 
     * and also define what the other CSV columns are.
     * 
     * The framework then takes the CSV file and imports it into the product database.
     *
     * @return string
     */
    
public function getImportDialogue()
    {
        
$this->hDialogue->newDialogue('hProductImport');
        
$this->hDialogue->addButtons('Close''Import');

        
$this->hDialogueTargetFrame true;
        
$this->hDialogueTarget      'hProductImportUpload';
        
$this->hDialogueEnctype     true;
        
$this->hDialogueAction      '/plugins/hProduct/import';

        
$html =
            
"<div id='hProductImportFile'>".
                
"<label for='hProductImportCSV' class='hProductImportLabel'>Select a CSV Document:</label>".
                
"<input type='file' name='hProductImportCSV' id='hProductImportCSV' size='20' />".
            
"</div>".
            
"<div id='hProductImportHeaders'>".
                
"<div class='hProductImportHeader' id='hProductImportDataHeader'>Data</div>".
                
"<div class='hProductImportHeader' id='hProductImportCSVHeader'>CSV Column</div>".
            
"</div>".
            
"<div id='hProductImportFields'>".
                
"<div class='hProductImportField'>".
                    
"<div class='hProductImportRow hProductImportTemplate'>".
                        
"<div class='hProductImportCell hProductImportDataCell'>".
                            
"<select name='hProductImportType[0]' class='hProductImportType'>".
                                
"<option value=''>Empty</option>".
                                
"<option value='hProductPartNumber'>Part Number</option>".
                                
"<option value='hProductName'>Name</option>".
                                
"<option value='hProductDescription'>Description</option>".
                                
"<option value='hProductFile'>Product File (Option)</option>".
                                
"<option value='hProductPrice'>Price</option>".
                            
"</select> ".
                        
"</div>".
                        
"<div class='hProductImportCell hProductImportColumnCell'><div>1</div></div>".
                        
"<div class='hProductImportAttachProductCell'>".
                            
"<input type='submit' name='hProductImportAttachProduct' class='hProductImportAttachProduct' value='Attach Product' />".
                            
"<div class='hProductImportAttachedProduct'>".
                            
"</div>".
                        
"</div>".
                    
"</div>".
                
"</div>".
            
"</div>".
            
"<div id='hProductImportFieldControls'>".
                 
"<img src='/images/themes/aqua/icons/misc/add.png' id='hProductImportAdd' />".
                 
"<img src='/images/themes/aqua/icons/misc/remove.png' id='hProductImportRemove' />".
            
"</div>".
            
"<div id='hProductImportBaseProductWrapper'>".
                
"<label for='hProductImportBaseProduct' class='hProductImportLabel'>Base Product<br /><i>Assigned to row with no options</i>:</label> ".
                
"<div id='hProductImportAttachedBaseProductWrapper'>".
                    
"<input type='submit' name='hProductImportAttachBaseProduct[0]' class='hProductImportAttachBaseProduct' value='Attach Product' />".
                    
"<div class='hProductImportAttachedBaseProduct'>".
                    
"</div>".
                
"</div>".
            
"</div>".
            
"<div id='hProductImportPriceCategoryWrapper'>".
                
"<label for='hProductImportPriceCategory' class='hProductImportLabel'>Price Category:</label> ".
                
"<select name='hProductImportPriceCategory'>";

        
$query db::query(
            
"SELECT `hProductPriceCategoryID`,
                    `hProductPriceCategoryName`
               FROM `hProductPriceCategories`
           ORDER BY `hProductPriceCategoryName` ASC"
        
);

        while (
$data db::fetchArray($query))
        {
            
$html .= "<option value='{$data['hProductPriceCategoryID']}'>{$data['hProductPriceCategoryName']}</option>\n";
        }

        
$html .=
                
"</select>".
            
"</div>".
            
"<div id='hProductImportExistsWrapper'>".
                
"<label class='hProductImportLabel'>When a part number exists:</label> ".
                
"<div id='hProductImportExistsInner'>".
                    
"<div>".
                        
"<input type='radio' value='0' name='hProductImportExists' id='hProductImportExists-0' /> ".
                        
"<label for='hProductImportExists-0'>Do not modify.</label>\n".
                    
"</div>".
                    
"<div>".
                        
"<input type='radio' value='1' name='hProductImportExists' id='hProductImportExists-1' /> ".
                        
"<label for='hProductImportExists-1'>Sync to csv data.</label>".
                    
"</div>".
                
"</div>".
            
"</div>";

        return 
$this->hDialogue->getDialogue($html'Import Products From Excel CSV (Comma Separated Values)');
    }
}

?>