<?

/*
Plugin Name: Amazon Context Links
Plugin URI: http://heyko.esenscity.de/blog/wordpress-context-links
Description: Lässt den Benutzter die Amazon Contextlinks für jede Seite einzeln aktivieren.
Version: 0.1
Author: Heyko Oelrichs <heyko.oelrichs@gmail.com>
Author URI: http://heyko.esenscity.de/blog/
*/

/*  Copyright 2007 Heyko Oelrichs (email : heyko.oelrichs@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/* Seite zu den Optionen hinzufuegen */

    
add_action('admin_menu''mt_add_pages');

    
/* Auskommentiert um es nicht die ganze Nacht an zu haben */
    
add_action('wp_footer''show_amzn_code');

    
/* Menueeintrag */
    
function mt_add_pages() {
        
add_options_page('Context Links''Context Links'8'contextlinks''mt_options_page');
        
        
/* Optionsfelder hinzufuegen */
       
add_option(amzn_cl_tag,                      """Amazon Tracking ID");
       
add_option(amzn_cl_max_links,              "10""Amazon maximale Linkanzahl pro Seite");
       
add_option(amzn_cl_show_buy_btn,                1"Amazon Zeige Kaufbutton");
       
add_option(amzn_cl_product_image,            1"Amazon Zeige Produktbild");
       
add_option(amzn_cl_link_target,                1"Amazon Link in neuem Fenster oeffnen");
       
add_option(amzn_cl_average_customer_rating,      1"Amazon Kundenbewertung");
       
add_option(amzn_cl_logo,                    1"Amazon Logo");
       
add_option(amzn_cl_offered_price,            1"Amazon Preis");
       
add_option(amzn_cl_exact_match,                1"Amazon Zeigen bei genauer Uebereinstimmung");

       
/* Darstellung des Links */
       
add_option(amzn_cl_link_style,                   1,  "Amazon Link Text Decoration");
       
add_option(amzn_cl_link_color,            "0000FF",  "Amazon Link Farbe");
    }


    
/* Optionsseite */
    
function mt_options_page() {

 
        if(
$_POST['Submit']){

           
/*
            * POST Array zerlegen und DB aktualisieren
            *
            * Um nicht jeden Wert im Formular nochmal per update_option
            * aufrufen zu muessen, sollen alle Werte in $_POST die mit
            * 'amzn' beginnen automatisch mit update_option aufgerufen werden.
            *
            * Feedback dazu bitte an mich :)
            *  
            */

           
$post_keys   array_keys($_POST);
           
$post_values array_values($_POST); 

           for(
$a=0;$a<count($_POST);$a++) {

               
/* Wenn es sich um einen String ala amzn_* handelt */
               
if(substr($post_keys[$a],0,4)=="amzn") {
                   
update_option($post_keys[$a],$post_values[$a]);
               }
      
           }

           
$msgSaved _('Options saved.');
        } 
 
        
/* Options saved Box... */
        
if( $msgSaved )
                echo 
'<!-- Last Action --><div id="message" class="updated fade"><p>'.$msgSaved.'</p></div>';
        
?>

        <div class="wrap">
         <h2>Amazon Context Links</h2>
         <form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
         <?php wp_nonce_field('update-options'?>

         <table>
          <!-- Tracking ID -->
          <tr>
           <td>Tracking ID:</td> 
           <td><input type="text"     name="amzn_cl_tag"       value="<? echo get_option('amzn_cl_tag'); ?>" /></td>
          </tr>

          <!-- Max. Links pro Seite -->
          <tr>
           <td>Max. Links:</td>  
           <td><input type="text"     name="amzn_cl_max_links" value="<? echo get_option('amzn_cl_max_links'); ?>" /></td>
          </tr>

          <!-- Zeige Produkt Image Ja/Nein -->
          <tr>
           <td>Produktabbildung 
           <td>
            <select name="amzn_cl_product_image">
             <option value="1" <?if(get_option('amzn_cl_product_image')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_product_image')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>

          <!-- Kundenbewertung zeigen ja/nein -->
          <tr>
           <td>Kundenbewertung:</td> 
           <td>
            <select name="amzn_cl_average_customer_rating">
             <option value="1" <?if(get_option('amzn_cl_average_customer_rating')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_average_customer_rating')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>
          
          <!-- Amazon Logo zeigen ja/nein -->
          <tr>
           <td>Logo</td> 
           <td>
            <select name="amzn_cl_logo">
             <option value="1" <?if(get_option('amzn_cl_logo')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_logo')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>

          <!-- Amazon Kaufbutton zeigen ja/nein -->
          <tr>
           <td>Kaufbutton:</td> 
           <td>
            <select name="amzn_cl_show_buy_btn">
             <option value="1" <?if(get_option('amzn_cl_show_buy_btn')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_show_buy_btn')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>
 
          <!-- Amazon Preis -->
          <tr>
           <td>Amazon Preis</td> 
           <td>
            <select name="amzn_cl_offered_price">
             <option value="1" <?if(get_option('amzn_cl_offered_price')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_offered_price')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>


          <!-- Exakte Uebereinstimmung -->
          <tr>
           <td>Exakte &Uuml;bereinstimmung:</td> 
           <td>
            <select name="amzn_cl_exact_match">
             <option value="1" <?if(get_option('amzn_cl_exact_match')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_exact_match')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>

          <!-- Link in neuem Fenster oeffnen Ja (1) / Nein (0) -->
          <tr>
           <td>Link in neuem Fenster &ouml;ffnen:</td> 
           <td>
            <select name="amzn_cl_link_target">
             <option value="1" <?if(get_option('amzn_cl_link_target')==1) echo "selected";?>><?_e('Yes');?></option>
             <option value="0" <?if(get_option('amzn_cl_link_target')==0) echo "selected";?>><?_e('No');?></option>
            </select>
           </td>
          </tr>
         </table>

         <h3>Darstellung des Links (z.B. Farbe, Unterstreichung)</h3>
         <table>
          <tr>
           <td>Link Farbe</td>
           <td><input type="text" name="amzn_cl_link_color" value="<? echo get_option('amzn_cl_link_color'); ?>" /></td>
          </tr>
          <tr>
           <td>Linkart</td>
           <td>
            <select name="amzn_cl_link_style">
             <option value="1" <?if(get_option('amzn_cl_link_style')==1) echo "selected";?>>Keine Unterstreichung</option>
             <option value="2" <?if(get_option('amzn_cl_link_style')==2) echo "selected";?>>Unterstrichen</option>
             <option value="3" <?if(get_option('amzn_cl_link_style')==3) echo "selected";?>>Doppelt unterstrichen</option>
             <option value="4" <?if(get_option('amzn_cl_link_style')==4) echo "selected";?>>Gestrichelt unterstrichen</option>
            </select>
           </td>
          </tr>
         </table>
      
         <p class="submit">
          <input type="submit" name="Submit" value="<?php _e('Update Options »'?>" />
         </p>
        </div>

        <?
    
}

    function 
show_amzn_code() {

        if(
get_option('amzn_cl_tag'))
        {
            
?>
                <!-- Amazon Context Links Plugin Start -->
                <script type="text/javascript">
                <!--
                    amzn_cl_tag="<? echo get_option('amzn_cl_tag');?>";
                    amzn_cl_max_links=<? echo get_option('amzn_cl_max_links');?>;
                    amzn_cl_product_image=<? echo get_option('amzn_cl_product_image');?>;
                    amzn_cl_average_customer_rating=<? echo get_option('amzn_cl_average_customer_rating');?>;
                    amzn_cl_logo=<? echo get_option('amzn_cl_logo');?>;
                    amzn_cl_show_buy_btn=<? echo get_option('amzn_cl_show_buy_btn');?>;
                    amzn_cl_offered_price=<? echo get_option('amzn_cl_offered_price');?>;
                    amzn_cl_exact_match=<? echo get_option('amzn_cl_exact_match');?>;
                    amzn_cl_link_target=<? echo get_option('amzn_cl_link_target');?>;

                    amzn_cl_link_style=<? echo get_option('amzn_cl_link_style'); ?>;
                    amzn_cl_link_color="<? echo get_option('amzn_cl_link_color'); ?>";

                //--></script>
                <script type="text/javascript" src="http://cls.assoc-amazon.de/de/s/cls.js"></script>
                <!-- Amazon Context Links Plugin End -->
            <?
        
}

        return 
$html;
    }


?>