_binary From f5f9b345a285b751139d96bfe5cd4c01ba7c2c44 Mon Sep 17 00:00:00 2001
From: Yashi Kamboj <yashi.kamboj@gmail.com>
Date: Fri, 9 Dec 2016 23:55:33 -0800
Subject: [PATCH] Fixes Issue 187, adds advanced search features

---
 src/controllers/components/SocialComponent.php |  35 ++++-
 src/locale/en_US/configure.ini                 |   6 +
 src/models/GroupModel.php                      |  50 ++++++-
 src/scripts/basic.js                           |  15 ++
 src/views/elements/WikiElement.php             | 182 ++++++++++++++++++-------
 5 files changed, 232 insertions(+), 56 deletions(-)

diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index 1f21956..9f4a509 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -2061,6 +2061,37 @@ class SocialComponent extends Component implements CrawlConstants
                         $data["MODE"] = "read";
                         $page_name = $data["FILTER"];
                     }
+                    list($data["RELATIONSHIP_ROWS"], $data["ALL_RELATIONSHIPS"])
+                        = $group_model->getAllRelationships($limit, $num);
+                    $data["FINAL_PAGES_THAT_LINK_TO"] =[];
+                    $data["FINAL_PAGES_THAT_LINK_FROM"] =[];
+                    if (isset($_REQUEST["select_reltype"]) && isset($_REQUEST
+                        ["advancedsearchtextbox"])) {
+                        $data["search"] = "yes";
+                        $page_name = $_REQUEST["advancedsearchtextbox"];
+                        $search_page_info = $group_model->getPageInfoByName(
+                            $group_id, $page_name, $data['CURRENT_LOCALE_TAG'],
+                            "read");
+                        if($search_page_info) {
+                            $page_id = $search_page_info['ID'];
+                            foreach($_REQUEST["select_reltype"] as
+                                $relationship_type) {
+                                list($data["TOTAL_TO_PAGES"],
+                                    $data["PAGES_THAT_LINK_TO"],
+                                    $data["TOTAL_FROM_PAGES"],
+                                    $data["PAGES_THAT_LINK_FROM"])=
+                                    $group_model->pagesLinkedWithRelationship(
+                                    $page_id, $group_id, $page_name,
+                                    $relationship_type, $limit, $num);
+                                $data["FINAL_PAGES_THAT_LINK_TO"] = array_merge(
+                                    $data["FINAL_PAGES_THAT_LINK_TO"],
+                                    $data["PAGES_THAT_LINK_TO"]);
+                                $data["FINAL_PAGES_THAT_LINK_FROM"] = array_merge(
+                                    $data["FINAL_PAGES_THAT_LINK_FROM"],
+                                    $data["PAGES_THAT_LINK_FROM"]);
+                            }
+                        }
+                    }
                     break;
                 case 'relationships':
                     $data["MODE"] = "relationships";
@@ -2147,8 +2178,8 @@ class SocialComponent extends Component implements CrawlConstants
                 $data["RESULTS_PER_PAGE"] = $num;
                 list($data["TOTAL_TO_PAGES"], $data["PAGES_THAT_LINK_TO"],
                     $data["TOTAL_FROM_PAGES"], $data["PAGES_THAT_LINK_FROM"]) =
-                    $group_model->pagesLinkedWithRelationship($page_id, $rel_type,
-                    $limit, $num);
+                    $group_model->pagesLinkedWithRelationship($page_id, 
+                        $data["GROUP_ID"], $data["PAGE_NAME"], $rel_type, $limit, $num);
             }
         }
         if (!$page_name) {
diff --git a/src/locale/en_US/configure.ini b/src/locale/en_US/configure.ini
index 6e49d3d..5875701 100644
--- a/src/locale/en_US/configure.ini
+++ b/src/locale/en_US/configure.ini
@@ -4162,6 +4162,9 @@ wiki_view_filter_or_create = "Search group page titles"
 ; WikiElement.php line: 1177
 wiki_element_go = "Go"
 ;
+; WikiElement.php line: 1200
+wiki_view_page_name = "Enter group page title"
+;
 ; WikiElement.php line: 1182
 wiki_view_create_page = "Create Page: %s"
 ;
@@ -4183,6 +4186,9 @@ wiki_view_difference = "Difference:"
 ; WikiElement.php line: 1280
 wiki_view_go = "Go"
 ;
+; WikiElement.php line: 1282
+wiki_element_advanced = "Advanced"
+;
 ; WikiElement.php line: 1299
 wiki_view_diff_first = "First"
 ;
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 1f12406..dcc54fd 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1157,6 +1157,37 @@ class GroupModel extends Model implements MediaConstants
         return false;
     }
     /**
+     * Gets all relationship names that exist in the database
+     * @return array of elements which represent all relationships
+     *     linking all wiki page
+     */
+    public function getAllRelationships($limit, $num) {
+        $db = $this->db;
+        $sql = "SELECT COUNT(*) AS NUM_RELATIONSHIPS FROM
+            PAGE_RELATIONSHIP";
+        $result = $db->execute($sql);
+        if ($result) {
+            $row = $db->fetchArray($result);
+            $total = $row['NUM_RELATIONSHIPS'];
+        }
+        $i=0;
+        $rel_names=[];
+        $sql = "SELECT DISTINCT R.NAME AS RELATIONSHIP_NAME
+            FROM PAGE_RELATIONSHIP R";
+        $result = $db->execute($sql);
+        if ($result) {
+            while ($rel_names[$i] = $db->fetchArray($result)) {
+                if($rel_names[$i]['RELATIONSHIP_NAME'] == 'generic_link') {
+                    unset($rel_names[$i]['RELATIONSHIP_NAME']);
+                    $i--;
+                    $total--;
+                }
+                $i++;
+            }
+        }
+        return [$total, $rel_names];
+    }
+    /**
      * Gets all the pages that are linked to a particular wiki page
      * by providing that page id from the table Group_Page_Link.
      *
@@ -1206,9 +1237,20 @@ class GroupModel extends Model implements MediaConstants
      * @return two arrays of elements which represent all pages linked
      *     to and from the given wiki page with a particular relationship
      */
-    public function pagesLinkedWithRelationship($page_id, $relationship, $limit, $num)
+    public function pagesLinkedWithRelationship($page_id, $group_id,
+        $page_name, $relationship, $limit, $num)
     {
         $db = $this->db;
+        //get data from group pre link and insert to group link
+        $sql = "INSERT INTO GROUP_PAGE_LINK (LINK_TYPE_ID, FROM_ID, TO_ID) ".
+            "SELECT LINK_TYPE_ID, FROM_ID, ?  FROM GROUP_PAGE_PRE_LINK ".
+            "WHERE TO_GROUP_ID = ? AND TO_PAGE_NAME = ?";
+        $db->execute($sql, [$page_id, $group_id, $page_name]);
+        $sql = "DELETE FROM GROUP_PAGE_PRE_LINK WHERE TO_GROUP_ID = ? AND " .
+            "TO_PAGE_NAME = ?";
+        $db->execute($sql, [$group_id, $page_name]);
+        $sql = "DELETE FROM GROUP_PAGE_PRE_LINK WHERE FROM_ID = ?";
+        $db->execute($sql, [$page_id]);
         //get the count of pages that link to the given page
         $sql = "SELECT COUNT(*) AS NUM FROM GROUP_PAGE_LINK L, PAGE_RELATIONSHIP P
             WHERE L.TO_ID = ? AND P.NAME = ? AND (L.LINK_TYPE_ID = P.ID)";
@@ -1227,7 +1269,7 @@ class GroupModel extends Model implements MediaConstants
         }
         //get the array of all pages linking to this page
         $pages_that_link_to = [];
-        $sql = "SELECT G.TITLE AS PAGES_LINKING_TO, G.ID AS PAGE_ID
+        $sql = "SELECT G.TITLE AS PAGES_LINKING_TO, G.ID AS PAGE_ID, P.NAME as REL
             FROM GROUP_PAGE_LINK L, GROUP_PAGE G, PAGE_RELATIONSHIP P
             where L.TO_ID = ? AND P.NAME = ? AND (L.FROM_ID = G.ID)
             AND (L.LINK_TYPE_ID = P.ID)"
@@ -1238,9 +1280,9 @@ class GroupModel extends Model implements MediaConstants
                 $pages_that_link_to[] = $tmp;
             }
         }
-        // get the array of all pages that link from
+        // get the array of all pages that link from this page
         $pages_that_link_from = [];
-        $sql = "SELECT G.TITLE AS PAGES_LINKING_FROM, G.ID AS PAGE_ID
+        $sql = "SELECT G.TITLE AS PAGES_LINKING_FROM, G.ID AS PAGE_ID, P.NAME as REL
             FROM GROUP_PAGE_LINK L, GROUP_PAGE G, PAGE_RELATIONSHIP P
             where L.FROM_ID = ? AND P.NAME = ? AND (L.TO_ID = G.ID)
             AND (L.LINK_TYPE_ID = P.ID)"
diff --git a/src/scripts/basic.js b/src/scripts/basic.js
index 009c0ce..cd49255 100755
--- a/src/scripts/basic.js
+++ b/src/scripts/basic.js
@@ -491,6 +491,21 @@ function setDisplay(id, value, display_type)
     obj.style.display = value;
 }
 /*
+ * Changes an element between display:none and display inline
+ * @param String id  the id of the DOM element one wants
+ */
+function changeDisplay(id)
+{
+    obj = elt(id);
+    $display = obj.style.display;
+    if($display === "none"){
+        obj.style.display = "inline";
+    }
+    else {
+        obj.style.display = "none";
+    }
+}
+/*
  * Toggles an element between display:none and display block
  * @param String id  the id of the DOM element one wants
  */
diff --git a/src/views/elements/WikiElement.php b/src/views/elements/WikiElement.php
index 08187a8..58f5d72 100644
--- a/src/views/elements/WikiElement.php
+++ b/src/views/elements/WikiElement.php
@@ -1176,17 +1176,19 @@ class WikiElement extends Element implements CrawlConstants
      */
     public function renderPages($data, $can_edit, $logged_in)
     {
-        $token_string = ($logged_in) ? C\CSRF_TOKEN."=". $data[C\CSRF_TOKEN] :
-            "";
+        $token_string = ($logged_in) ? C\CSRF_TOKEN."=".
+            $data[C\CSRF_TOKEN] : "";
+        $data["ADVANCED_FILTER"] = "";
+        $select_reltype = [];
         $group_id = $data["GROUP"]["GROUP_ID"];
         $controller = $data['CONTROLLER'];
-        $create_query = htmlentities(B\wikiUrl($data["FILTER"], true,
-            $controller, $group_id)) . $token_string . "&amp;arg=edit";
-        $paging_query = htmlentities(B\wikiUrl("pages", true, $controller,
-            $group_id)) . $token_string;
+        $create_query = htmlentities(B\wikiUrl($data["FILTER"],
+            true, $controller, $group_id)) . $token_string .
+            "&amp;arg=edit";
+        $paging_query = htmlentities(B\wikiUrl("pages", true,
+            $controller, $group_id)) . $token_string;
         ?><h2><?=tl("wiki_view_wiki_page_list", $data["GROUP"]["GROUP_NAME"])
-        ?></h2><?php
-        ?>
+        ?></h2>
         <form id="editpageForm" method="get">
         <input type="hidden" name="c" value="<?=$data['CONTROLLER'] ?>" />
         <input type="hidden" name="<?= C\CSRF_TOKEN ?>" value="<?=
@@ -1199,45 +1201,122 @@ class WikiElement extends Element implements CrawlConstants
             maxlength="<?= C\SHORT_TITLE_LEN ?>"
             placeholder="<?= tl("wiki_view_filter_or_create")
             ?>" value="<?= $data['FILTER'] ?>" />
-        <button class="button-box" type="submit"><?=tl('wiki_element_go')
-            ?></button>
-        </form>
+        <button id="page-go" class="button-box block" type="submit">
+            <?=tl('wiki_element_go') ?></button>
+        <a href="#" id="advanced-link" onclick="toggleDisplay(
+            'advancedPageForm');changeDisplay('page-go')">
+            <?=tl('wiki_element_advanced')?></a></form>
+        <form id="searchreltypeForm" method="get">
+        <input type="hidden" name="c" value="<?=$data['CONTROLLER'] ?>" />
+        <input type="hidden" name="<?= C\CSRF_TOKEN ?>" value="<?=
+            $data[C\CSRF_TOKEN] ?>" />
+        <input type="hidden" name="a" value="wiki" />
+        <input type="hidden" name="arg" value="pages" />
+        <input type="hidden" name="group_id" value="<?=
+            $data['GROUP']['GROUP_ID'] ?>" />
+        <div class="none" id="advancedPageForm">&nbsp;
+        <div class='group-result'>
+        <table><tr><td><select size="3" name="select_reltype[]"
+            id="select_reltype[]" multiple>
         <?php
-        if ($data["FILTER"] != "") {
-            ?><a href='<?= $create_query ?>'><?=tl("wiki_view_create_page",
-                $data['FILTER']) ?></a><?php
+        if ($data["RELATIONSHIP_ROWS"] != []){
+            foreach ($data["ALL_RELATIONSHIPS"] as $key => $value) {
+                ?><option><?=$data["ALL_RELATIONSHIPS"][$key]
+                ['RELATIONSHIP_NAME']?></option><?php
+            }
+        }?></select></td>
+        <td><input type="text" name="advancedsearchtextbox"
+            id="advancedsearchtextbox" class="extra-wide-field"
+            maxlength="<?= C\SHORT_TITLE_LEN ?>"
+            placeholder="<?=tl("wiki_view_page_name")?>"
+            value="<?=$data['ADVANCED_FILTER']?>" /></td>
+        <td><button class="button-box" type="submit" ><?=tl(
+            'wiki_element_go') ?></button></td></tr></table>
+            </div></div></form><?php
+        if ($data["FILTER"] != "") {?>
+            <a href='<?= $create_query ?>'><?=tl(
+                "wiki_view_create_page", $data['FILTER']) ?></a>
+        <?php }
+        if (strpos($_SERVER['REQUEST_URI'],"advancedsearchtextbox")
+            !== false) {
+            parse_str($_SERVER["QUERY_STRING"], $query_array);?>
+            <script type="text/javascript">
+                var rel_array = <?php echo json_encode(
+                    $query_array['select_reltype']); ?>;
+                window.onload = function() {
+                    document.getElementById("advanced-link").click();
+                    document.getElementById("advancedsearchtextbox").
+                        value="<?=$query_array['advancedsearchtextbox']?>";
+                    var select = document.getElementById("select_reltype[]");
+                    for (var i = 0, l = select.options.length, o; i < l; i++) {
+                        o = select.options[i];
+                        if ( rel_array.indexOf( o.text ) != -1 ) {
+                            o.selected = true;
+                        }
+                    }
+                }
+            </script><?php
         }
-        ?>
-        <div>&nbsp;</div>
-        <?php
-        if ($data['PAGES'] != []) {
-            foreach ($data['PAGES'] as $page) {
-                if ($page['TYPE'] == 'page_alias' && isset($page['ALIAS'])) {
-                    $page["DESCRIPTION"] = tl('wiki_element_redirect_to').
-                        " <a href='".htmlentities(B\wikiUrl($page['ALIAS'],
-                        true, $controller, $group_id)) . $token_string .
-                        "'>{$page['ALIAS']}</a>";
-                } else {
-                    $page["DESCRIPTION"] = strip_tags($page["DESCRIPTION"]);
+        if($data["search"]) {
+            ?><div>&nbsp;</div><?php
+            if($data["FINAL_PAGES_THAT_LINK_TO"] != []) {
+                foreach ($data["FINAL_PAGES_THAT_LINK_TO"] as $page_that_link_to) {
+                    $var = $page_that_link_to["PAGES_LINKING_TO"];
+                    $advanced_url = htmlentities(B\wikiUrl($var, true, $controller,
+                        $group_id)) . $token_string .
+                        "&arg=relationships" . "&page_id=" .
+                        $page_that_link_to["PAGE_ID"] . "&reltype=" .
+                        $page_that_link_to["REL"];
+                    ?><div class='group-result'><a href='<?= $advanced_url ?>'>
+                        <?=$page_that_link_to["PAGES_LINKING_TO"] ?>
+                        <br/></a></div><div>&nbsp;</div><?php
+                }
+            }
+            if($data["FINAL_PAGES_THAT_LINK_FROM"] != []){
+                foreach($data["FINAL_PAGES_THAT_LINK_FROM"] as $page_that_link_from){
+                    $var = $page_that_link_from["PAGES_LINKING_FROM"];
+                    $advanced_url = htmlentities(B\wikiUrl($var, true, $controller,
+                        $group_id)) . $token_string . "&arg=relationships" .
+                        "&page_id=" . $page_that_link_from["PAGE_ID"] .
+                        "&reltype=" . $page_that_link_from["REL"];
+                        ?><div class='group-result'><a href='<?= $advanced_url ?>'>
+                            <?=$page_that_link_from["PAGES_LINKING_FROM"] ?>
+                            <br/></a></div><div>&nbsp;</div><?php
                 }
-                ?>
-                <div class='group-result'>
-                <a href="<?= htmlentities(B\wikiUrl($page['TITLE'],
-                    true, $controller, $group_id)) . $token_string
-                    ?>&amp;noredirect=true" ><?=$page["SHOW_TITLE"] ?></a>
-                    </br />
-                <?= $page["DESCRIPTION"] ?>
-                </div>
-                <div>&nbsp;</div>
-                <?php
             }
-            $this->view->helper("pagination")->render(
-                $paging_query,
-                $data['LIMIT'], $data['RESULTS_PER_PAGE'], $data['TOTAL_ROWS']);
         }
-        if ($data['PAGES'] == []) {
-            ?><div><?=tl('wiki_view_no_pages', "<b>".L\getLocaleTag()."</b>")?>
-            </div><?php
+        else {
+            ?><div>&nbsp;</div><?php
+            if ($data['PAGES'] != []) {
+                foreach ($data['PAGES'] as $page) {
+                    if ($page['TYPE'] == 'page_alias' && isset($page['ALIAS'])) {
+                        $page["DESCRIPTION"] = tl('wiki_element_redirect_to').
+                            " <a href='".htmlentities(B\wikiUrl($page['ALIAS'],
+                            true, $controller, $group_id)) . $token_string .
+                            "'>{$page['ALIAS']}</a>";
+                    }
+                    else {
+                        $page["DESCRIPTION"] = strip_tags($page["DESCRIPTION"]);
+                    }
+                    ?>
+                    <div class='group-result'>
+                    <a href="<?= htmlentities(B\wikiUrl($page['TITLE'],
+                        true, $controller, $group_id)) . $token_string
+                        ?>&amp;noredirect=true" ><?=$page["SHOW_TITLE"] ?></a>
+                        </br />
+                    <?= $page["DESCRIPTION"] ?>
+                    </div>
+                    <div>&nbsp;</div>
+                    <?php
+                }
+                $this->view->helper("pagination")->render(
+                    $paging_query,
+                    $data['LIMIT'], $data['RESULTS_PER_PAGE'], $data['TOTAL_ROWS']);
+                }
+            if ($data['PAGES'] == []) {
+                ?><div><?=tl('wiki_view_no_pages', "<b>".L\getLocaleTag()."</b>")?>
+                </div><?php
+            }
         }
     }
     /**
@@ -1263,6 +1342,7 @@ class WikiElement extends Element implements CrawlConstants
                 $data['CONTROLLER'], $data['GROUP_ID']));
             $url .= $csrf_token;
             e("<td><a href='$url'>$var</a></td>");
+            e("<br/>");
         }
     }
     /**
@@ -1316,17 +1396,19 @@ class WikiElement extends Element implements CrawlConstants
         ?><table><tr><td><?php
         foreach ($data["PAGES_THAT_LINK_TO"] as $key => $value) {
             $var = $data["PAGES_THAT_LINK_TO"][$key]["PAGES_LINKING_TO"];
-            $url = htmlentities(B\wikiUrl($var, true, $data['CONTROLLER'],
-                $data['GROUP_ID'])).$csrf_token."&arg=relationships".
-                "&page_id=".$data["PAGES_THAT_LINK_TO"][$key]["PAGE_ID"].
-                "&reltype=".$data["REL-TYPE"];?>
+            $url = htmlentities(B\wikiUrl($var, true, $data['CONTROLLER'] ,
+                $data['GROUP_ID'])).$csrf_token."&arg=relationships" .
+                "&page_id=" . $data["PAGES_THAT_LINK_TO"][$key]["PAGE_ID"] .
+                "&reltype=" . $data["REL-TYPE"];?>
             <div class="align-right"><a href=<?=$url?>><?=$var?>
                 </a></div><?php
-        }?></td><?php 
-        if($data["TOTAL_TO_PAGES"]>0) {?>
-            <td class="rel-type-left"><hr class="rel-type-hr"></td><?php
         }
-        else {?>
+        ?></td><?php
+        if ($data["TOTAL_TO_PAGES"] > 0) {
+            ?>
+            <td class="rel-type-left"><hr class="rel-type-hr"></td><?php
+        } else {
+            ?>
             <td class="rel-type-td"><hr class="rel-type-hr"></td><?php
         }
         //displaying the middle table - Current Page Name ?>
-- 
1.9.5.msysgit.0

