Skip to content

Commit

Permalink
Merge pull request #25 from Bulletproofmonk/2018-11-15
Browse files Browse the repository at this point in the history
2018-11-15
  • Loading branch information
bulletproofmonk committed Nov 14, 2018
2 parents 1365260 + ba84022 commit e6110d1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
23 changes: 15 additions & 8 deletions articlelist.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<?php
<?php

// connect to SteemSQL database
include 'steemSQLconnect2.php';

// Make your model available
include 'models/articlelistmodel.php';

// Get values from URL
// voter details stripped from URL
if (isset($_GET["voter"])) {
$voter = rtrim($_GET["voter"]);
} else {
$voter=NULL;
}

// check if date has been entered (submitted via form)
if (isset($_GET["date"])) {
$date = $_GET["date"];
} else {
// set date variable for SQL query.
$date = date("Y-m-d", strtotime("-1 months"));
}

// check if to date has been entered (submitted via form)
if (isset($_GET["toDate"])) {
$todate = $_GET["toDate"];
Expand All @@ -26,37 +31,39 @@
if (isset($_GET["mode"])) {
$mode = $_GET["mode"];
}

// retrieve choice for whether to include articles only or to include comments as well.
if (isset($_GET["Articlesonly"])) {
$articlesonly = $_GET["Articlesonly"];
} else {
$articlesonly = 1;
}

// retrieve the tag input box value.
if (isset($_GET["tag"])) {
$tag =$_GET["tag"];
$tag = trim($tag);
$tag = explode(" ", $tag) ;
} else {
$tag=NULL;
}
if (isset($_GET["tag2"])) {
$tag2=$_GET["tag2"];
} else {
$tag2=NULL;
}

// retrieve the title input box value.
if (isset($_GET["title"])) {
$title=$_GET["title"];
} else {
$title=NULL;
}

// create an instance
$articlelistmodel = new articlelistmodel($conn);
if (isset($voter)&&$mode=="upvote") {
// get list of results
$results = $articlelistmodel -> gethistory($date,$todate,$voter,$articlesonly,$tag,$tag2,$title);
$results = $articlelistmodel -> gethistory($date,$todate,$voter,$articlesonly,$tag,$title);
} elseif ((isset($voter)&&$mode=="written") || (isset($tag)&&$mode=="written")) {
$results = $articlelistmodel -> getwritten($date,$todate,$voter,$articlesonly,$tag,$tag2,$title);
$results = $articlelistmodel -> getwritten($date,$todate,$voter,$articlesonly,$tag,$title);
}

// Show the view
include 'views/articlelistview.php';
$articlelistmodel -> close_connection();
Expand Down
46 changes: 29 additions & 17 deletions models/articlelistmodel.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
<?php
class articlelistmodel {

protected $db;

public function __construct(PDO $db) {
$this->db = $db;
}
public function gethistory($date,$todate,$voter,$articlesonly,$tag,$tag2,$title) {

public function gethistory($date,$todate,$voter,$articlesonly,$tag,$title) {
// retrieve list of articles voted on by user
$sql = "SELECT top 500 timestamp,weight,TxVotes.author,TxVotes.permlink
FROM TxVotes LEFT OUTER JOIN Comments
ON (TxVotes.author=Comments.author AND TxVotes.permlink=Comments.permlink)
WHERE (voter=:voter)
AND (timestamp>=Convert(datetime, :date))
AND (timestamp<=Convert(datetime,:todate))";
// if comments are excluded, add this to SQL
// if comments are excluded, add this to SQL
if ($articlesonly==2) {
$sql.= "AND (Comments.depth=0)";
}
// if there is a tag specified, add this to SQL
if ($tag!=NULL) {
$sql.="AND (CONTAINS(Comments.json_metadata, '".$tag."'))";
}
if ($tag2!=NULL) {
$sql.="AND (CONTAINS(Comments.json_metadata, '".$tag2."'))";
}
// search for tags SQL added
$sql.=$this->searchtag($tag);
if ($title!=NULL) {
$sql.="AND (CONTAINS(Comments.title, '".$title."'))";
}
Expand All @@ -34,7 +32,8 @@ public function gethistory($date,$todate,$voter,$articlesonly,$tag,$tag2,$title)
$result->execute();
return $result;
}
public function getwritten($date,$todate,$author,$articlesonly,$tag,$tag2,$title) {

public function getwritten($date,$todate,$author,$articlesonly,$tag,$title) {
// retrieve list of articles written by user
$sql = "SELECT top 500 author,permlink,created AS timestamp
FROM Comments
Expand All @@ -49,13 +48,8 @@ public function getwritten($date,$todate,$author,$articlesonly,$tag,$tag2,$title
if ($articlesonly==2) {
$sql.= "AND (depth=0)";
}
// if there is a tag specified, add this to SQL
if ($tag!=NULL) {
$sql.="AND (CONTAINS(json_metadata, '".$tag."'))";
}
if ($tag2!=NULL) {
$sql.="AND (CONTAINS(json_metadata, '".$tag2."'))";
}
// search for tags SQL added
$sql.=$this->searchtag($tag);
// if there is a title specified, add this to SQL
if ($title!=NULL) {
$sql.="AND (CONTAINS(title, '".$title."'))";
Expand All @@ -70,10 +64,28 @@ public function getwritten($date,$todate,$author,$articlesonly,$tag,$tag2,$title
$result->execute();
return $result;
}

public function close_connection() {
if(isset($this->db)) {
$this->db->close();
unset($this->db);
}
}

// function to search for tags in json_metadata
private function searchtag($tag) {
if ($tag!=NULL) {
$sql.="AND (CONTAINS(Comments.json_metadata, '";
// the near function is used to ensure that the tag searched is within 5 words from the tag word in the json_metadata string, so it doesn't pick up the same word in other parts of the json_metadata
$sql.="NEAR((tags,$tag[0]),4,TRUE)";
$arrlength = count($tag);
if ($arrlength>1) {
for ($x=1;$x<$arrlength;$x++) {
$sql.="AND NEAR((tags,$tag[$x]),4,TRUE)";
}
}
$sql.="'))";
}
return $sql;
}
}
31 changes: 15 additions & 16 deletions views/articlelistview.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,21 @@
<input class="form-control" name="toDate" id="toDate" type="date" value="<? if (isset($todate)) {echo $todate;} else {echo date(" Y-m-d ",strtotime("+1 day "));} ?>" id="toDate" min="2016-03-30" max="<?echo date(" Y-m-d ",strtotime("+1 day ")); ?>">&nbsp;&nbsp;
</div>
<div class="form-group" style="margin-top:10px;">
<label for="tag">Tag 1:&nbsp;&nbsp;</label>
<input class="form-control" name="tag" id="tag" type="text" size="5" value="<?if (isset($tag)) {echo $tag;}?>">&nbsp;&nbsp;
</div>
<div class="form-group" style="margin-top:10px;">
<label for="tag2">Tag 2:&nbsp;&nbsp;</label>
<input class="form-control" name="tag2" id="tag2" type="text" size="5" value="<?if (isset($tag2)) {echo $tag2;}?>">&nbsp;&nbsp;
</div>
<label for="tag">Must Contain Tag(s) Separated By Space:&nbsp;&nbsp;</label>
<input class="form-control" placeholder="e.g. utopian-io development" name="tag" id="tag" type="text" size="25" value="<? if (isset($tag)) {$arrlength = count($tag);for($x = 0; $x < $arrlength; $x++) {echo $tag[$x];echo " ";}}?>" aria-describedby="tagHelp"">&nbsp;&nbsp;<br>
</div>
<div class="form-group" style="margin-top:10px;">
<label for="title">Title:&nbsp;&nbsp;</label>
<input class="form-control" name="title" id="title" type="text" size="10" value="<?if (isset($title)) {echo $title;}?>">&nbsp;&nbsp;
<label for="title">Title Must Contain Word(s):&nbsp;&nbsp;</label>
<input class="form-control" name="title" id="title" type="text" size="20" value="<?if (isset($title)) {echo $title;}?>">&nbsp;&nbsp;
</div>
<div class="form-group" style="margin-top:10px;">
<input class="form-control" name="Articlesonly" type="checkbox" value="2" id="Articlesonly"<? if ($articlesonly==2) {echo " checked";} ?>>&nbsp;
<label for="Articlesonly">Exclude comments&nbsp;&nbsp;</label>
<label for="Articlesonly">Exclude Comments&nbsp;&nbsp;</label>
</div>
</form>
<?
if ($mode=="upvote") {
echo '<button id="upvotebtn" class="btn btn-lg btn-primary" style="margin-top:10px;">List Articles Upvoted</button>';
echo '<button id="upvotebtn" class="btn btn-lg btn-primary" style="margin-top:10px;">List Articles Voted</button>';
} else {
echo '<button id="writtenbtn" class="btn btn-lg btn-primary" style="margin-top:10px;">List Articles Written</button>';
}
Expand Down Expand Up @@ -115,10 +111,10 @@
</body>
<!-- Javascript -->
<script>
// Retrieve input to be submitted via Javascript
function retrieveInput() {
title = document.getElementById("title").value;
tag = document.getElementById("tag").value;
tag2= document.getElementById("tag2").value;
goToUser = document.getElementById("voter").value;
date = document.getElementById("date").value;
toDate = document.getElementById("toDate").value;
Expand All @@ -130,15 +126,18 @@ function retrieveInput() {
{
mycheck=1;
}
}
}

// Submit input from user to articlelist controller
$(function(){
$("#upvotebtn").click(function() {
retrieveInput();
window.location.href = 'articlelist.php?mode=upvote&Articlesonly='+mycheck+'&voter='+goToUser+'&date='+date+'&toDate='+toDate+'&tag='+tag+'&tag2='+tag2+'&title='+title;
});
window.location.href = 'articlelist.php?mode=upvote&Articlesonly='+mycheck+'&voter='+goToUser+'&date='+date+'&toDate='+toDate+'&tag='+tag+'&title='+title;
});

$("#writtenbtn").click(function() {
retrieveInput();
window.location.href = 'articlelist.php?mode=written&Articlesonly='+mycheck+'&voter='+goToUser+'&date='+date+'&toDate='+toDate+'&tag='+tag+'&tag2='+tag2+'&title='+title;
window.location.href = 'articlelist.php?mode=written&Articlesonly='+mycheck+'&voter='+goToUser+'&date='+date+'&toDate='+toDate+'&tag='+tag+'&title='+title;
});
});
</script>
Expand Down

0 comments on commit e6110d1

Please sign in to comment.