From 0af693c4ce266db5ab6d44a55c1f038a80f4892a Mon Sep 17 00:00:00 2001
From: Pushkar Umaranikar <pushkar.umaranikar@sjsu.edu>
Date: Fri, 12 Dec 2014 00:53:34 -0800
Subject: [PATCH] 0000160: Configuration settings for advertisement server are
 added to Yioop.

---
 controllers/components/system_component.php | 29 +++++++++++-
 controllers/search_controller.php           |  6 ++-
 css/search.css                              | 13 ++++++
 models/profile_model.php                    | 10 ++--
 scripts/server_settings.js                  | 69 +++++++++++++++++++++++++++
 views/elements/serversettings_element.php   | 72 +++++++++++++++++++++++++++++
 views/search_view.php                       | 27 +++++++++++
 7 files changed, 219 insertions(+), 7 deletions(-)
 create mode 100644 scripts/server_settings.js

diff --git a/controllers/components/system_component.php b/controllers/components/system_component.php
index ca25db0..43cb301 100755
--- a/controllers/components/system_component.php
+++ b/controllers/components/system_component.php
@@ -536,6 +536,14 @@ class SystemComponent extends Component
      */
     function serverSettings()
     {
+        $script_array = array('SIDE_ADSCRIPT',
+        'TOP_ADSCRIPT','GLOBAL_ADSCRIPT');
+        foreach($script_array as $value) {
+            if(isset($_REQUEST[$value])) {
+                $_REQUEST[$value] = str_replace("(","&#40;",$_REQUEST[$value]);
+                $_REQUEST[$value] = str_replace(")","&#41;",$_REQUEST[$value]);
+            }
+        }
         $parent = $this->parent;
         $profile_model = $parent->model("profile");
         $data = array();
@@ -546,12 +554,16 @@ class SystemComponent extends Component
         }
         $data['SCRIPT'] = "";
         $data["ELEMENT"] = "serversettings";
+        $data["INCLUDE_SCRIPTS"][] = "server_settings";
+        $data["AD_LOCATION"] = array("top" => e(tl('ad_element_top')),
+        "side" => e(tl('ad_element_side')),"both" => e(tl('ad_element_both')),
+        "none" => e(tl('ad_element_none')));
         switch($arg)
         {
             case "update":
                 $parent->updateProfileFields($data, $profile,
                     array('USE_FILECACHE', 'USE_MEMCACHE', 'USE_MAIL_PHP',
-                        'USE_PROXY'));
+                        'USE_PROXY','AD_LOCATION'));
                 $old_profile =
                     $profile_model->getProfile(WORK_DIRECTORY);
                 $db_problem = false;
@@ -575,6 +587,21 @@ class SystemComponent extends Component
                         $db_problem = true;
                     }
                 }
+                if(isset($profile['AD_LOCATION']) &&
+                    $profile['AD_LOCATION'] !=
+                    $old_profile['AD_LOCATION']) {
+                        $data['AD_LOCATION'] = $old_profile['AD_LOCATION'];
+                }
+                if(isset($profile['TOP_ADSCRIPT']) &&
+                    $profile['TOP_ADSCRIPT'] !=
+                    $old_profile['TOP_ADSCRIPT']) {
+                        $data['TOP_ADSCRIPT'] = $old_profile['TOP_ADSCRIPT'];
+                }
+                if(isset($profile['SIDE_ADSCRIPT']) &&
+                    $profile['SIDE_ADSCRIPT'] !=
+                    $old_profile['SIDE_ADSCRIPT']) {
+                        $data['SIDE_ADSCRIPT'] = $old_profile['SIDE_ADSCRIPT'];
+                }
                 if($db_problem) {
                     $data['MESSAGE'] =
                         tl('system_component_configure_no_change_db');
diff --git a/controllers/search_controller.php b/controllers/search_controller.php
index a2af99e..532da02 100755
--- a/controllers/search_controller.php
+++ b/controllers/search_controller.php
@@ -83,6 +83,10 @@ class SearchController extends Controller implements CrawlConstants
     {
         $data = array();
         $start_time = microtime();
+        $data['TOP_ADSCRIPT'] = TOP_ADSCRIPT;
+        $data['SIDE_ADSCRIPT'] = SIDE_ADSCRIPT;
+        $data['GLOBAL_ADSCRIPT'] = GLOBAL_ADSCRIPT;
+        $data['AD_LOCATION'] = AD_LOCATION;
         list($subsearches, $no_query) = $this->initializeSubsearches();
         $format_info = $this->initializeResponseFormat();
         if(!$format_info) { return;}
@@ -1955,4 +1959,4 @@ class SearchController extends Controller implements CrawlConstants
         $node->appendChild($script);
     }
 }
-?>
+?>
\ No newline at end of file
diff --git a/css/search.css b/css/search.css
index 5f63c4e..fb6aa82 100755
--- a/css/search.css
+++ b/css/search.css
@@ -2604,3 +2604,16 @@ button.default
 {
     margin-bottom : -5px;
 }
+.top-adscript
+{
+    left: 0.5in;
+    position: relative;
+    top: -0.9in;
+
+}
+.side-adscript
+{
+    float: right;
+    margin-top: -50px;
+    margin-right: 180px;
+}
\ No newline at end of file
diff --git a/models/profile_model.php b/models/profile_model.php
index 728e3dd..8a1f3f7 100755
--- a/models/profile_model.php
+++ b/models/profile_model.php
@@ -51,23 +51,23 @@ class ProfileModel extends Model
      * profile.php file
      * @var array
      */
-    var $profile_fields = array('API_ACCESS', 'AUTH_KEY',
+    var $profile_fields = array('AD_LOCATION','API_ACCESS', 'AUTH_KEY',
         'AUTHENTICATION_MODE', 'AUXILIARY_CSS', 'BACKGROUND_COLOR',
         'BACKGROUND_IMAGE', 'CACHE_LINK', 'CAPTCHA_MODE', 'CSRF_TOKEN',
         'DEBUG_LEVEL', 'DESCRIPTION_WEIGHT', 'DB_HOST', 'DBMS', 'DB_NAME',
         'DB_PASSWORD', 'DB_USER', 'DEFAULT_LOCALE', 'FAVICON',
-        'FIAT_SHAMIR_MODULUS', 'FOREGROUND_COLOR',
+        'FIAT_SHAMIR_MODULUS', 'FOREGROUND_COLOR', 'GLOBAL_ADSCRIPT',
         'GROUP_ITEM', 'IN_LINK','IP_LINK', 'LANDING_PAGE', 'LINK_WEIGHT',
         'LOGO', 'M_LOGO', 'MAIL_PASSWORD',  'MAIL_SECURITY',
         'MAIL_SENDER', 'MAIL_SERVER', 'MAIL_SERVERPORT', 'MAIL_USERNAME',
         'MEMCACHE_SERVERS', 'MIN_RESULTS_TO_GROUP', 'NAME_SERVER', 'NEWS_MODE',
         'PROXY_SERVERS', 'REGISTRATION_TYPE', 'ROBOT_INSTANCE',
         'RSS_ACCESS', 'SEARCHBAR_PATH', 'SERVER_ALPHA', 'SESSION_NAME',
-        'SIDEBAR_COLOR', 'SIGNIN_LINK', 'SIMILAR_LINK',
+        'SIDE_ADSCRIPT', 'SIDEBAR_COLOR', 'SIGNIN_LINK', 'SIMILAR_LINK',
         'SUBSEARCH_LINK', 'TIMEZONE', 'TITLE_WEIGHT', 'TOPBAR_COLOR',
-        'TOR_PROXY',
+        'TOP_ADSCRIPT','TOR_PROXY',
         'USE_FILECACHE', 'USE_MAIL_PHP', 'USE_MEMCACHE', 'USE_PROXY',
-        'USER_AGENT_SHORT', 'WEB_URI', 'WEB_ACCESS', 'WORD_SUGGEST'
+        'USER_AGENT_SHORT', 'WEB_URI', 'WEB_ACCESS', 'WORD_SUGGEST',
         );
     /**
      * Profile fields which are stored in wiki or in a flat file
diff --git a/scripts/server_settings.js b/scripts/server_settings.js
new file mode 100644
index 0000000..5dd536f
--- /dev/null
+++ b/scripts/server_settings.js
@@ -0,0 +1,69 @@
+/**
+ * SeekQuarry/Yioop --
+ * Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ * Copyright (C) 2009 - 2014  Chris Pollett chris@pollett.org
+ *
+ * LICENSE:
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * END LICENSE
+ *
+ * @author Pushkar Umaranikar (edited Chris Pollett)
+ * @package seek_quarry
+ * @subpackage javascript
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2014
+ * @filesource
+ */
+/**
+ * This script shows or blocks the textarea for including ad script on respective 
+ * location depending upon the ad location that user has selected.
+ */
+window.onload = function()
+{
+    showHideScriptdiv();
+}
+/**
+ * Method to show/block div including text area depending upon location selected for 
+ * the advertisement to display on search results page.
+ */
+function showHideScriptdiv()
+{
+     /*
+      * Get the radio button list represnting location for the 
+      * advertisement.
+     */
+    var adserverconfig=document.getElementsByName('AD_LOCATION');
+    /*
+     * Show/ block div with text area depending upon the radio
+     * button value.
+     */
+      var adAlign = [
+      ['block','block','none'],//top[top,global,side]
+      ['none','block','block'],//side
+      ['block','block','block'],//both
+      ['none','none','none'],//none
+     ]
+      for(var i = 0;i < adserverconfig.length;i++){
+        if(adserverconfig[i].checked){
+            elt('top-adscript-config').style.display = adAlign[i][0];
+            elt('global-adscript-config').style.display = adAlign[i][1];
+            elt('side-adscript-config').style.display = adAlign[i][2];
+            break;
+        }
+      }
+}
diff --git a/views/elements/serversettings_element.php b/views/elements/serversettings_element.php
index 9e5fdf7..b9de8a2 100644
--- a/views/elements/serversettings_element.php
+++ b/views/elements/serversettings_element.php
@@ -241,6 +241,78 @@ class ServersettingsElement extends Element
                     ?></textarea>
                 </div>
             </fieldset>
+            <fieldset><legend>
+            <?php e(tl('serversettings_element_adserver_configuration'))
+            ?></legend>
+            <div><b><label>
+            <?php e(tl('serversettings_element_align'))
+            ?></label></b><br>
+            <input type='radio' name='AD_LOCATION'
+                value="<?php e(tl('ad_element_top'));?>"
+                onchange="showHideScriptdiv(this);"
+                <?php
+                e(($data['AD_LOCATION'])==tl('ad_element_top')?'checked':'')
+                ?>/>
+                <?php
+                e(tl('ad_element_top'))
+                ?>
+            <input type='radio' name='AD_LOCATION'
+                value="<?php e(tl('ad_element_side'));?>"
+                onclick="showHideScriptdiv(this);"
+                <?php
+                e(($data['AD_LOCATION'])==tl('ad_element_side')?'checked':'')
+                ?>/>
+                <?php
+                e(tl('ad_element_side'))
+                ?>
+            <input type='radio' name='AD_LOCATION'
+                value="<?php e(tl('ad_element_both'));?>"
+                onclick="showHideScriptdiv(this);"
+                <?php
+                e(($data['AD_LOCATION'])==tl('ad_element_both')?'checked':'')
+                ?>/>
+                <?php
+                e(tl('ad_element_both'))
+                ?>
+            <input type='radio' name='AD_LOCATION'
+                value="<?php e(tl('ad_element_none'));?>"
+                onclick="showHideScriptdiv(this);"
+                <?php
+                e(($data['AD_LOCATION'])==tl('ad_element_none')?'checked':'')
+                ?>/>
+                <?php
+                e(tl('ad_element_none'))
+                ?>
+            </div>
+            <div id="global-adscript-config">
+            <label for="global-adscript"><b>
+            <?php 
+            e(tl('serversettings_element_global_adscript'));
+            ?></b></label>
+            <textarea class="short-text-area" id="global-adscript"
+                name="GLOBAL_ADSCRIPT"><?php
+                e(html_entity_decode($data['GLOBAL_ADSCRIPT'],ENT_QUOTES))
+                ?>
+            </textarea></div>
+            <div id="top-adscript-config"><label for="top-adscript"><b>
+            <?php
+            e(tl('serversettings_element_top_adscript'));
+            ?></b></label>
+            <textarea class="short-text-area" id="top-adscript"
+                name="TOP_ADSCRIPT"><?php
+                e(html_entity_decode($data['TOP_ADSCRIPT'],ENT_QUOTES))
+                ?>
+            </textarea></div>
+            <div id="side-adscript-config"><label for="side-adscript"><b>
+            <?php
+            e(tl('serversettings_element_side_adscript'));
+            ?></b></label>
+            <textarea class="short-text-area" id="side-adscript"
+                name="SIDE_ADSCRIPT"><?php
+                e(html_entity_decode($data['SIDE_ADSCRIPT'],ENT_QUOTES))
+                ?>
+            </textarea></div>
+            </fieldset>
             </div>
             <div class="top-margin center">
             <button class="button-box" type="submit"><?php
diff --git a/views/search_view.php b/views/search_view.php
index d85f606..3e2c811 100755
--- a/views/search_view.php
+++ b/views/search_view.php
@@ -177,6 +177,7 @@ class SearchView extends View implements CrawlConstants
      */
     function renderSearchResults($data)
     {
+        $is_landing = (!isset($data['PAGES']) && !isset($data['MORE']));
         $logged_in = isset($data["ADMIN"]) && $data["ADMIN"];
         $token_string = ($logged_in) ? CSRF_TOKEN."=".$data[CSRF_TOKEN]."&":"";
         $token_string_amp = ($logged_in) ?
@@ -198,6 +199,32 @@ class SearchView extends View implements CrawlConstants
             }
         ?></h2>
         <?php
+        if((!$is_landing) && ($data['AD_LOCATION'] == tl('ad_element_top') ||
+            $data['AD_LOCATION'] == tl('ad_element_both'))) { ?>
+            <div class="top-adscript">
+            <?php
+            $str = html_entity_decode($data['TOP_ADSCRIPT'],ENT_QUOTES);
+            $str = preg_replace("[&#40;]","(",$str);
+            $str = preg_replace("[&#41;]",")",$str);
+            eval(e($str));
+            ?>
+            </div>
+        <?php
+        } ?>
+        <?php
+        if(!$is_landing && ($data['AD_LOCATION'] == tl('ad_element_side') ||
+            $data['AD_LOCATION'] == tl('ad_element_both'))) { ?>
+            <div class="side-adscript">
+            <?php 
+            $str = html_entity_decode($data['SIDE_ADSCRIPT'], ENT_QUOTES);
+            $str = preg_replace("[&#40;]","(",$str);
+            $str = preg_replace("[&#41;]",")",$str);
+            eval(e($str));
+            ?>
+            </div>
+        <?php
+        } ?>
+        <?php
         $similar_words = $data['THESAURUS_VARIANTS'];
         $use_thesaurus = WORD_SUGGEST && count($similar_words) > 0 && !MOBILE;
         if($use_thesaurus) { ?>
-- 
1.9.4.msysgit.1

