_binary From 34319967fbc3f81e8f8169144ce00ec04e9e34c6 Mon Sep 17 00:00:00 2001
From: Pushkar Umaranikar <pushkar.umaranikar@sjsu.edu>
Date: Sat, 6 Dec 2014 13:47:02 -0800
Subject: [PATCH] Configuration settings for advertisement server are added to
 Yioop.

---
 controllers/components/system_component.php | 31 +++++++++++-
 controllers/search_controller.php           |  4 ++
 css/search.css                              | 13 +++++
 models/profile_model.php                    |  4 +-
 scripts/server_settings.js                  | 76 +++++++++++++++++++++++++++++
 views/elements/serversettings_element.php   | 53 ++++++++++++++++++++
 views/search_view.php                       | 35 +++++++++++++
 7 files changed, 213 insertions(+), 3 deletions(-)
 create mode 100644 scripts/server_settings.js

diff --git a/controllers/components/system_component.php b/controllers/components/system_component.php
index 07e9028..b8a7048 100755
--- a/controllers/components/system_component.php
+++ b/controllers/components/system_component.php
@@ -536,6 +536,19 @@ class SystemComponent extends Component
      */
     function serverSettings()
     {
+       
+        $_REQUEST['SIDE_ADSCRIPT'] = 
+        str_replace("(","&#40;",$_REQUEST['SIDE_ADSCRIPT']);
+        $_REQUEST['SIDE_ADSCRIPT'] = 
+        str_replace(")","&#41;",$_REQUEST['SIDE_ADSCRIPT']);
+        $_REQUEST['TOP_ADSCRIPT'] = 
+        str_replace("(","&#40;",$_REQUEST['TOP_ADSCRIPT']);
+        $_REQUEST['TOP_ADSCRIPT'] = 
+        str_replace(")","&#41;",$_REQUEST['TOP_ADSCRIPT']);
+		$_REQUEST['GLOBAL_ADSCRIPT'] = 
+        str_replace("(","&#40;",$_REQUEST['GLOBAL_ADSCRIPT']);
+        $_REQUEST['GLOBAL_ADSCRIPT'] = 
+        str_replace(")","&#41;",$_REQUEST['GLOBAL_ADSCRIPT']);
         $parent = $this->parent;
         $profile_model = $parent->model("profile");
         $data = array();
@@ -546,12 +559,16 @@ class SystemComponent extends Component
         }
         $data['SCRIPT'] = "";
         $data["ELEMENT"] = "serversettings";
+		$data["INCLUDE_SCRIPTS"][] = "server_settings";
+		$data["ADLOCATION"] = 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 +592,18 @@ 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 f66b38a..5346593 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;}
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 7b8def7..7a4d61d 100755
--- a/models/profile_model.php
+++ b/models/profile_model.php
@@ -67,8 +67,8 @@ class ProfileModel extends Model
         'SUBSEARCH_LINK', 'TIMEZONE', 'TITLE_WEIGHT', 'TOPBAR_COLOR',
         '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',
+        'AD_LOCATION','TOP_ADSCRIPT','GLOBAL_ADSCRIPT','SIDE_ADSCRIPT');
     /**
      * Associative array (table_name => SQL statement to create that table)
      * List is alphabetical and contains all Yioop tables. List is only
diff --git a/scripts/server_settings.js b/scripts/server_settings.js
new file mode 100644
index 0000000..3adf421
--- /dev/null
+++ b/scripts/server_settings.js
@@ -0,0 +1,76 @@
+/**
+ * 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.
+     */
+    if(adserverconfig[0].checked){
+        elt('top-adscript-config').style.display='block';
+        elt('global-adscript-config').style.display='block';
+        elt('side-adscript-config').style.display='none';
+    }
+    if(adserverconfig[1].checked){
+        elt('side-adscript-config').style.display='block';
+        elt('global-adscript-config').style.display='block';
+        elt('top-adscript-config').style.display='none';
+    }
+    if(adserverconfig[2].checked){
+        elt('top-adscript-config').style.display='block';
+        elt('global-adscript-config').style.display='block';
+        elt('side-adscript-config').style.display='block';
+    }
+    if(adserverconfig[3].checked){
+        elt('top-adscript-config').style.display='none';
+        elt('global-adscript-config').style.display='none';
+        elt('side-adscript-config').style.display='none';
+    }
+}
diff --git a/views/elements/serversettings_element.php b/views/elements/serversettings_element.php
index 9e5fdf7..7147ebc 100644
--- a/views/elements/serversettings_element.php
+++ b/views/elements/serversettings_element.php
@@ -241,6 +241,59 @@ 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..9c839f7 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) ?
@@ -197,6 +198,40 @@ class SearchView extends View implements CrawlConstants
                 $data['TOTAL_ROWS']));
             }
         ?></h2>
+         <?php if((!$is_landing) && ($data['AD_LOCATION'] == 'Top' ||
+        $data['AD_LOCATION'] == 'Both')) { ?>
+        <div class="top-adscript">
+        <?php
+          $str = html_entity_decode($data['TOP_ADSCRIPT'],ENT_QUOTES);       
+            $i = 1;
+            while (($offset = strpos($str, "&#40;")) !== false) {
+            $str = substr_replace($str, "(", $offset, strlen("&#40;"));
+            }
+            $i = 1;
+            while (($offset = strpos($str, "&#41;")) !== false) {
+            $str = substr_replace($str, ")", $offset, strlen("&#41;"));
+            }
+          eval(e($str));
+        ?>
+        </div>
+        <?php } ?>
+        <?php if((!$is_landing) && ($data['AD_LOCATION'] == 'Side' ||
+        $data['AD_LOCATION'] == 'Both')) { ?>
+        <div class="side-adscript">
+        <?php 
+          $str = html_entity_decode($data['SIDE_ADSCRIPT'],ENT_QUOTES);          
+            $i = 1;
+            while (($offset = strpos($str, "&#40;")) !== false) {
+            $str = substr_replace($str, "(", $offset, strlen("&#40;"));
+            }
+            $i = 1;
+            while (($offset = strpos($str, "&#41;")) !== false) {
+            $str = substr_replace($str, ")", $offset, strlen("&#41;"));
+            }
+          eval(e($str));
+        ?>
+        </div>
+        <?php } ?>
         <?php
         $similar_words = $data['THESAURUS_VARIANTS'];
         $use_thesaurus = WORD_SUGGEST && count($similar_words) > 0 && !MOBILE;
-- 
1.9.4.msysgit.1

