_binary From e66003ce2d9239cbbc77d6f8dff63fbd080b2b7e Mon Sep 17 00:00:00 2001
From: Yashi Kamboj <yashi.kamboj@gmail.com>
Date: Wed, 9 Nov 2016 20:15:58 -0800
Subject: [PATCH] Fixes Issue 184

---
 src/controllers/components/SocialComponent.php | 31 +++++++++++
 src/css/search.css                             | 26 ++++++++++
 src/models/GroupModel.php                      | 61 ++++++++++++++++++++++
 src/views/elements/WikiElement.php             | 71 +++++++++++++++++++++++++-
 4 files changed, 187 insertions(+), 2 deletions(-)

diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index f8b0ed0..1f21956 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -2120,6 +2120,37 @@ class SocialComponent extends Component implements CrawlConstants
                     break;
             }
         }
+        //checking argument 2
+        if (isset($_REQUEST["reltype"])) {
+            $rel_type = $_REQUEST["reltype"];
+            $data["REL-TYPE"] = $rel_type;
+            $data["GROUP_ID"] = $group_id;
+            //clean up
+            if (!empty($page_id)) {
+                $data["MODE"] = "rel-type";
+                $page_info = $group_model->getPageInfoByPageId(
+                    $page_id);
+                if (!isset($page_name)) {
+                        $page_name = empty($page_info['PAGE_NAME'])
+                            ? "rel-types" : $page_info['PAGE_NAME'];
+                }
+                $limit = isset($limit) ? $limit : 0;
+                $num = (isset($_SESSION["MAX_PAGES_TO_SHOW"])) ?
+                    $_SESSION["MAX_PAGES_TO_SHOW"] :
+                    C\DEFAULT_ADMIN_PAGING_NUM;
+                $data["PAGE_ID"] = $page_id;
+                $data["PAGE_NAME"] = $page_name;
+                $data["DISCUSS_THREAD"] = empty($page_info["DISCUSS_THREAD"]
+                    ) ? -1 : $page_info['DISCUSS_THREAD'];
+                $data["GROUP_ID"] = $page_info["GROUP_ID"];
+                $data["LIMIT"] = $limit;
+                $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);
+            }
+        }
         if (!$page_name) {
             $page_name = tl('social_component_main');
         }
diff --git a/src/css/search.css b/src/css/search.css
index 76dbddf..81681b0 100755
--- a/src/css/search.css
+++ b/src/css/search.css
@@ -2746,6 +2746,32 @@ table.wikitable > caption
 {
     font-weight:bold;
 }
+.rel-type-current
+{
+    border:2px solid black;
+    border-radius:8px;
+    text-align:center;
+    background:white;
+    padding:4px;
+}
+.rel-type-left
+{
+    width:10px;
+    border-left:thick double #000000;
+}
+.rel-type-right
+{
+    width:10px;
+    border-right:thick double #000000;
+}
+.rel-type-td
+{
+    width:10px;
+}
+.rel-type-hr
+{
+    border:1px solid black;
+}
 .resource-actions
 {
     min-width:2in;
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 91e0b65..1f12406 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1194,6 +1194,67 @@ class GroupModel extends Model implements MediaConstants
         return [$total,$pages];
     }
     /**
+     * Gets all the pages that are linked to a particular wiki page
+     * by providing a particular relationship type.
+     *
+     * @param int $page_id identifier for the current page
+     * @param string $relationship the type of relationship
+           linking the wiki pages
+     * @param string $limit first row we want from the result set
+     * @param string $num number of rows we want starting from the
+     *     first row in the result set
+     * @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)
+    {
+        $db = $this->db;
+        //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)";
+        $result = $db->execute($sql, [$page_id, $relationship]);
+        if ($result) {
+            $row = $db->fetchArray($result);
+            $total_to_pages = $row['NUM'];
+        }
+        //get the count of pages that link from the given page
+        $sql = "SELECT COUNT(*) AS NUM FROM GROUP_PAGE_LINK L, PAGE_RELATIONSHIP P
+            WHERE L.FROM_ID = ? AND P.NAME = ? AND (L.LINK_TYPE_ID = P.ID)";
+        $result = $db->execute($sql, [$page_id, $relationship]);
+        if ($result) {
+            $row = $db->fetchArray($result);
+            $total_from_pages = $row['NUM'];
+        }
+        //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
+            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)"
+            .$db->limitOffset($limit, $num);
+        $result = $db->execute($sql, [$page_id, $relationship]);
+        if ($result) {
+            while ($tmp = $db->fetchArray($result)) {
+                $pages_that_link_to[] = $tmp;
+            }
+        }
+        // get the array of all pages that link from
+        $pages_that_link_from = [];
+        $sql = "SELECT G.TITLE AS PAGES_LINKING_FROM, G.ID AS PAGE_ID
+            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)"
+            .$db->limitOffset($limit, $num);
+        $result = $db->execute($sql, [$page_id, $relationship]);
+        if ($result) {
+            while ( $tmp = $db->fetchArray($result)) {
+                $pages_that_link_from[] = $tmp;
+            }
+        }
+        return [$total_to_pages, $pages_that_link_to, $total_from_pages,
+            $pages_that_link_from];
+    }
+    /**
      * Gets all the relationship types between this particular wiki page
      * and all other pages that it is linked to.
      *
diff --git a/src/views/elements/WikiElement.php b/src/views/elements/WikiElement.php
index cda329b..08187a8 100644
--- a/src/views/elements/WikiElement.php
+++ b/src/views/elements/WikiElement.php
@@ -282,6 +282,9 @@ class WikiElement extends Element implements CrawlConstants
             case "relationships":
                 $this->renderRelationshipPage($data);
                 break;
+            case "rel-type":
+                $this->renderRelationshipType($data);
+                break;
             case "read":
                 // no break
             case "show":
@@ -1274,15 +1277,79 @@ class WikiElement extends Element implements CrawlConstants
         if ($logged_in) {
             $csrf_token = C\CSRF_TOKEN."=".$data[C\CSRF_TOKEN];
         }
+        $page_name = $data['PAGE_NAME'];
+        $page_id = $data['PAGE_ID'];
+        $url_base = htmlentities(B\wikiUrl($page_name, true,
+            $data['CONTROLLER'], $data['GROUP_ID']));
+        $url_base .= $csrf_token."&arg=".$data["MODE"]."&page_id=".$page_id;
         if(empty($data["RELATIONSHIPS"])){
             ?><div class="red"><?= tl('wiki_element_no_inrelationships')
             ?>'</div><?php
             return;
         }
+        e('<table class="name-table">');
         foreach ($data["RELATIONSHIPS"] as $key => $value) {
-            ?><div><?=$data["RELATIONSHIPS"][$key]["RELATIONSHIP_TYPE"]
-            ?></div><?php
+            $url = $url_base;
+            $var = $data["RELATIONSHIPS"][$key]["RELATIONSHIP_TYPE"];
+            $url .= "&reltype=".$var;
+            e("<tr><td><a href='$url'>$var</a></td><tr>");
+        }
+        e('</table>');
+    }
+    /**
+     * Used to draw the page which displays all wiki pages
+     *    that link a wiki page with a particular relationship type
+     *
+     * @param array $data fields contain info about all such wiki pages
+     */
+    public function renderRelationshipType($data)
+    {
+        $logged_in = !empty($data["ADMIN"]);
+        if ($logged_in) {
+            $csrf_token = C\CSRF_TOKEN."=".$data[C\CSRF_TOKEN];
+        }
+        $page_name = $data['PAGE_NAME'];
+        $page_id = $data['PAGE_ID'];
+        $url_main = htmlentities(B\wikiUrl($page_name, true,
+                $data['CONTROLLER'], $data['GROUP_ID'])).$csrf_token;
+        //display pages that link to the particular page
+        ?><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"];?>
+            <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 class="rel-type-td"><hr class="rel-type-hr"></td><?php
+        }
+        //displaying the middle table - Current Page Name ?>
+        <td><div class="rel-type-current"><a href=<?=$url_main?>>
+            <?=$page_name?></a></div></td><?php
+            if($data["TOTAL_FROM_PAGES"]>0) {?>
+                <td class="rel-type-right"><hr class="rel-type-hr"></td>
+            <?php }
+            else {?>
+                <td class="rel-type-td"><hr class="rel-type-hr"></td>
+            <?php }
+        //display pages that link from the particular page ?>
+        <td><?php
+        foreach ($data["PAGES_THAT_LINK_FROM"] as $key => $value) {
+            $var = $data["PAGES_THAT_LINK_FROM"][$key]["PAGES_LINKING_FROM"];
+            $url = htmlentities(B\wikiUrl($var, true,
+                $data['CONTROLLER'], $data['GROUP_ID'])).$csrf_token.
+                "&arg=relationships"."&page_id=".
+                $data["PAGES_THAT_LINK_FROM"][$key]["PAGE_ID"].
+                "&reltype=".$data["REL-TYPE"];
+            ?><div class="align-left"><a href=<?=$url?>><?=$var?></a></div><?php
         }
+        ?></td></tr></table><?php
     }
     /**
      * Used to draw the revision history page for a wiki document
-- 
1.9.5.msysgit.0

