Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions code/CustomSearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,29 @@ public function getSearchField()
{
return $this->_searchField;
}
/**
* Get the search query for display in a "You searched for ..." sentence.
*
* @param array $data
* @return string
*/
/**
* Get the search query for display in a "You searched for ..." sentence.
*
* @param array $data
* @return string
*/
public function getSearchQuery($data = null)
{
if(!isset($data))
if (!isset($data)) {
$data = $_REQUEST;
}

if (!is_array($this->_searchField))
if (!is_array($this->_searchField)) {
return $data[$this->_searchField];
}

// find first best result
foreach ($this->_searchField as $field)
if (isset($data[$field]))
foreach ($this->_searchField as $field) {
if (isset($data[$field])) {
return $data[$field];
}
}
}

/**
Expand All @@ -66,20 +70,21 @@ public function getSearchQuery($data = null)
public function getResults($pageLength = null, $data = null)
{
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials
if (!isset($data) || !is_array($data))
if (!isset($data) || !is_array($data)) {
$data = $_REQUEST;
}

// set language (if present)
if (singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale']))
{
if (singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
$origLocale = Translatable::get_current_locale();
Translatable::set_current_locale($data['locale']);
}

// Check all given search fields
$keywords = $this->getSearchQuery($data);
if(empty($keywords))
if (empty($keywords)) {
return new DataObjectSet();
}

$andProcessor = create_function('$matches', '
return " +" . $matches[2] . " +" . $matches[4] . " ";
Expand All @@ -95,36 +100,33 @@ public function getResults($pageLength = null, $data = null)

$keywords = $this->addStarsToKeywords($keywords);

if (!$pageLength)
if (!$pageLength) {
$pageLength = $this->pageLength;
}
$start = isset($_GET['start'])
? (int) $_GET['start']
: 0;

if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false)
{
if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) {
$results = DB::getConn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", $this->_extraFilter, true);
}
else
{
} else {
$results = DB::getConn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, null, $this->_extraFilter);
}

// filter by permission
if ($results)
foreach ($results as $result)
{
if (!$result->canView())
if ($results) {
foreach ($results as $result) {
if (!$result->canView()) {
$results->remove($result);
}
}
}

// reset locale
if (singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale']))
{
if (singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
Translatable::set_current_locale($origLocale);
}

return $results;
}

}
1 change: 0 additions & 1 deletion code/Models/KnowledgeBaseArticleRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ class KnowledgeBaseArticleRating extends DataObject
'Article' => 'KnowledgeBaseArticle',
'Author' => 'Member' // author of the rating, not the article
);

}
11 changes: 6 additions & 5 deletions code/Pages/KnowledgeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ class KnowledgeBase extends KnowledgeBasePage
* records when the database is built, but make sure you call
* parent::requireDefaultRecords().
*/
function requireDefaultRecords()
public function requireDefaultRecords()
{
parent::requireDefaultRecords();

// Ignore inherited pages
if ($this->class != get_class() || !self::$create_knowledgebase_pages)
if ($this->class != get_class() || !self::$create_knowledgebase_pages) {
return;
}

if (DB::query("SELECT COUNT(*) FROM `SiteTree` WHERE `SiteTree`.`ClassName` = '" . get_class() . "'")->value() > 0)
if (DB::query("SELECT COUNT(*) FROM `SiteTree` WHERE `SiteTree`.`ClassName` = '" . get_class() . "'")->value() > 0) {
return;
}

$kbSection = new KnowledgeBase();
$kbSection->Title = _t('KnowledgeBase.DEFAULT_TITLE', 'Knowledge Base');
Expand Down Expand Up @@ -69,9 +71,8 @@ function requireDefaultRecords()
$kbArticle->flushCache();
DB::alteration_message('Knowledge Base Article created', 'created');
}

}

class KnowledgeBase_Controller extends KnowledgeBasePage_Controller
{
}
}
36 changes: 19 additions & 17 deletions code/Pages/KnowledgeBaseArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function getCategoryText()
{
$category = $this->Parent();
$items = array();
while($category && $category->exists())
{
while ($category && $category->exists()) {
$items[] = $category->MenuTitle;
$category = $category->Parent();
}
Expand Down Expand Up @@ -61,20 +60,19 @@ public function getRating()
return intval(DB::query('SELECT ROUND(AVG(`Rating`)) as Rating FROM `KnowledgeBaseArticleRating` WHERE `ArticleID` = ' . $this->ID)->value());
}

function getCMSFields()
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new CheckboxField('Featured','Feature this article?'), 'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('Featured', 'Feature this article?'), 'Content');
return $fields;
}

}

class KnowledgeBaseArticle_Controller extends KnowledgeBasePage_Controller
{
private static $allowed_actions = array('rate');

function init()
public function init()
{
parent::init();

Expand All @@ -89,8 +87,9 @@ function init()
*/
protected function ratingIdentifier()
{
if ($cookie = Cookie::get('RatingIdentifier'))
if ($cookie = Cookie::get('RatingIdentifier')) {
return $cookie;
}
Cookie::set('RatingIdentifier', $cookie = uniqid());
return $cookie;
}
Expand All @@ -101,21 +100,21 @@ protected function ratingIdentifier()
protected function userAlreadyRatedArticle()
{
// check by cookie
if ($cookie = $this->ratingIdentifier())
{
if ($cookie = $this->ratingIdentifier()) {
$result = DB::query(sprintf("SELECT COUNT(*) FROM KnowledgeBaseArticleRating WHERE ArticleID = %d AND Cookie = '%s'", $this->data()->ID, Convert::raw2sql($cookie)
))->value();
if ($result > 0)
if ($result > 0) {
return true;
}
}

// check by user
if ($memberID = Member::currentUserID())
{
if ($memberID = Member::currentUserID()) {
$result = DB::query(sprintf('SELECT COUNT(*) FROM KnowledgeBaseArticleRating WHERE ArticleID = %d AND AuthorID = %d', $this->data()->ID, $memberID
))->value();
if ($result > 0)
if ($result > 0) {
return true;
}
}

return false;
Expand All @@ -128,28 +127,31 @@ protected function userAlreadyRatedArticle()
*/
public function rate()
{
if(!$this->data()->RatingEnabled)
if (!$this->data()->RatingEnabled) {
return json_message(array(
'Message' => _t('ERROR_RATING_DISABLED', "Rating is disabled on this article"),
'Result' => 'Error'
));
}

// Ensure that this user isn't voting multiple times
if ($this->userAlreadyRatedArticle())
if ($this->userAlreadyRatedArticle()) {
return json_encode(array(
'Message' => _t('ERROR_ALREADY_VOTED', "You've already voted on this article"),
'Result' => 'Error'
));
}

// Make sure the rating is valid
$rating = isset($_REQUEST['rate'])
? intval($_REQUEST['rate'])
: null;
if (is_null($rating) || $rating < 0 || $rating > 20)
if (is_null($rating) || $rating < 0 || $rating > 20) {
return json_encode(array(
'Message' => _t('ERROR_PLEASE_SELECT', "Please select a rating"),
'Result' => 'Error'
));
}

// Save the record
$record = new KnowledgeBaseArticleRating();
Expand All @@ -165,4 +167,4 @@ public function rate()
'Result' => 'Success'
));
}
}
}
2 changes: 1 addition & 1 deletion code/Pages/KnowledgeBaseCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KnowledgeBaseCategory extends KnowledgeBasePage
'KnowledgeBaseCategory'
);

function getCMSFields()
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new TextField('Description', 'Category Description', null, 255), 'Content');
Expand Down
Loading