Skip to content

Commit

Permalink
Tags now optional. Improved Request object, improved GeoIP class
Browse files Browse the repository at this point in the history
  • Loading branch information
snytkine committed Apr 14, 2011
1 parent 574dfc7 commit b9d57fc
Show file tree
Hide file tree
Showing 24 changed files with 452 additions and 206 deletions.
8 changes: 8 additions & 0 deletions !config.ini.dist
Expand Up @@ -21,6 +21,11 @@ collection = "C_Cache"
; debug log and raize error reporting level
DEBUG = true
;
; Minimum number of tags question must have
; set to 0 to make tags optional
;
MIN_QUESTION_TAGS = 1
;
; Maximum number of tags Question can have
; This will be enforced when question is submitted
; and when question is retagged
Expand All @@ -29,6 +34,9 @@ MAX_QUESTION_TAGS = 5
; Minimun length of question in chars
MIN_QUESTION_CHARS = 10
;
; Minimum length of questio
MIN_TITLE_CHARS = 10
;
; Minimum number of words required in question
MIN_QUESTION_WORDS = 3
;
Expand Down
18 changes: 17 additions & 1 deletion lib/Lampcms/Answer.php
Expand Up @@ -333,7 +333,23 @@ public function getUrl(){
* @see Lampcms\Interfaces.CommentedResource::addComment()
*/
public function addComment(CommentParser $oComment){
$aKeys = array('_id', 'i_uid', 'i_prnt', 'username', 'avtr', 'b_owner', 'b', 't', 'ts');
$aKeys = array(
'_id',
'i_uid',
'i_prnt',
'username',
'avtr',
'b_owner',
'b',
't',
'ts',
'cc',
'cn',
'reg',
'city',
'zip',
'lat',
'lon');

$aComments = $this->getComments();
d('aComments: '.print_r($aComments, 1));
Expand Down
1 change: 1 addition & 0 deletions lib/Lampcms/AnswerParser.php
Expand Up @@ -215,6 +215,7 @@ protected function makeAnswer(){
'username' => $username,
'ulink' => '<a href="'.$this->oSubmittedAnswer->getUserObject()->getProfileUrl().'">'.$username.'</a>',
'avtr' => $this->oSubmittedAnswer->getUserObject()->getAvatarSrc(),
'i_words' => $oBody->asPlainText()->getWordsCount(),
'i_up' => 0,
'i_down' => 0,
'i_votes' => 0,
Expand Down
19 changes: 16 additions & 3 deletions lib/Lampcms/Controllers/Edit.php
Expand Up @@ -54,6 +54,16 @@

use Lampcms\WebPage;

/**
* Controller for creating a page
* with the "Edit" form
* to edit question or answer
* It creates the form object
* and adds form to the page template
*
* @author Dmitri Snytkine
*
*/
class Edit extends WebPage
{

Expand Down Expand Up @@ -104,9 +114,12 @@ protected function makeForm(){
} else {
$this->oForm->title = $this->oResource['title'];
$this->oForm->id_title = $this->oResource['id_title'];
$this->oForm->addValidator('title', function($val){
if(strlen($val) < 10){
return 'Title must contain at least 10 letters';
$minTitle = $this->oRegistry->Ini->MIN_TITLE_CHARS;
$this->oForm->addValidator('title', function($val) use ($minTitle){

if(mb_strlen($val) < $minTitle){
$err = 'Title must contain at least %s letters';
return sprintf($err, $minTitle);
}

return true;
Expand Down
38 changes: 24 additions & 14 deletions lib/Lampcms/Controllers/Editor.php
Expand Up @@ -82,6 +82,14 @@ class Editor extends Edit

protected $aRequired = array('rid', 'rtype');

/**
* Object Utf8String represents body
* of question
*
* @var object of type Utf8string
*/
protected $oBody;


protected function main(){
$this->getResource()
Expand Down Expand Up @@ -124,20 +132,23 @@ protected function process()
d('formVals: '.print_r($formVals, 1));

$this->oResource['b'] = $this->makeBody($formVals['qbody']);

$this->oResource['i_words'] = $this->oBody->asPlainText()->getWordsCount();

/**
* @important Don't attempt to edit the value of title
* for the answer since it technically does not have the title
* and we don't want to change existing one
* If we don't skip this step for Answer then title
* of answer will be removed
*/
if($this->oResource instanceof \Lampcms\Question){
$oTitle = $this->makeTitle($formVals['title']);
$this->oResource['title'] = $oTitle->valueOf();
$oTitle = $this->makeTitle($formVals['title']);
$title = $oTitle->valueOf();
$this->oResource['title'] = $title;
$this->oResource['url'] = $oTitle->toASCII()->makeLinkTitle()->valueOf();
$this->oResource['a_title'] = \Lampcms\TitleTokenizer::factory($oTitle)->getArrayCopy();
}

$this->oResource->setEdited($this->oRegistry->Viewer, strip_tags($formVals['reason']));
$this->oResource->setEdited($this->oRegistry->Viewer, \strip_tags($formVals['reason']));
$this->oResource->save();

$this->oRegistry->Dispatcher->post($this->oResource, 'onEdit');
Expand All @@ -157,22 +168,22 @@ protected function process()
* tags have been added
*
* @param string $body
*
*
* @return string html of new body
*
*
*/
protected function makeBody($body){
$oBody = Utf8String::factory($body)
$this->oBody = Utf8String::factory($body)
->tidy()
->safeHtml()
->asHtml();

$oBody = HTMLStringParser::factory($oBody)->parseCodeTags()->linkify()->reload()->setNofollow();
$oBody = HTMLStringParser::factory($this->oBody)->parseCodeTags()->linkify()->reload()->setNofollow();

if($this->oResource instanceof \Lampcms\Question){
$oBody->unhilight()->hilightWords($this->oResource['a_tags']);
}

$htmlBody = $oBody->valueOf();

d('after HTMLStringParser: '.$htmlBody);
Expand All @@ -183,9 +194,9 @@ protected function makeBody($body){

/**
* Make new value of title
*
*
* @param string $title
*
*
* @return object of type Utf8String
*/
protected function makeTitle($title){
Expand All @@ -207,8 +218,7 @@ protected function updateQuestion(){
d('need to update QUESTION');

try{
$this->oRegistry->Mongo->QUESTIONS
->update(array('_id' => $this->oResource['i_qid']),
$this->oRegistry->Mongo->QUESTIONS->update(array('_id' => $this->oResource['i_qid']),
array(
'$set' => array(
'i_lm_ts' => time(),
Expand Down
37 changes: 25 additions & 12 deletions lib/Lampcms/Controllers/Retag.php
Expand Up @@ -140,15 +140,26 @@ protected function main(){
*/
protected function validateSubmitted(){

if(empty($this->aSubmitted)){
$min = $this->oRegistry->Ini->MIN_QUESTION_TAGS;
$max = $this->oRegistry->Ini->MAX_QUESTION_TAGS;

if(($min > 0) && empty($this->aSubmitted)){
/*
* @todo translate string
*/
throw new \Lampcms\Exception('No valid tags have been submitted. Please use words that best categorize this question');
}

$max = $this->oRegistry->Ini->MAX_QUESTION_TAGS;
if(count($this->aSubmitted) > $max){
$count = count($this->aSubmitted);

if($count < $min){
/**
* @todo Translate string
*/
throw new \Lampcms\Exception('Question must have at least '.$min.' tag(s)');
}

if($count > $max){
/**
* @todo translate string
*/
Expand Down Expand Up @@ -259,15 +270,17 @@ protected function removeOldTags(){
* @return object $this
*/
protected function addNewTags(){
\Lampcms\Qtagscounter::factory($this->oRegistry)->parse($this->oQuestion);
\Lampcms\UserTags::factory($this->oRegistry)->addTags($this->oQuestion['i_uid'], $this->oQuestion);
\Lampcms\Relatedtags::factory($this->oRegistry)->addTags($this->oQuestion);

if(0 === $this->oQuestion['i_sel_ans']){
d('going to add to Unanswered tags');
\Lampcms\UnansweredTags::factory($this->oRegistry)->set($this->oQuestion);
if(count($this->aSubmitted) > 0){
\Lampcms\Qtagscounter::factory($this->oRegistry)->parse($this->oQuestion);
\Lampcms\UserTags::factory($this->oRegistry)->addTags($this->oQuestion['i_uid'], $this->oQuestion);
\Lampcms\Relatedtags::factory($this->oRegistry)->addTags($this->oQuestion);

if(0 === $this->oQuestion['i_sel_ans']){
d('going to add to Unanswered tags');
\Lampcms\UnansweredTags::factory($this->oRegistry)->set($this->oQuestion);
}
}

return $this;
}

Expand Down Expand Up @@ -305,7 +318,7 @@ protected function returnResult(){
$message = 'Question retagged successfully';

if(Request::isAjax()){
$ret = array('reload' => 100); //'alert' => $message,
$ret = array('reload' => 100); //'alert' => $message,

Responder::sendJSON($ret);
}
Expand Down
79 changes: 61 additions & 18 deletions lib/Lampcms/Forms/Askform.php
Expand Up @@ -75,24 +75,41 @@ class Askform extends Form
protected $template = 'tplFormask';


/**
* This is just an example how a custom validation
* callback could be added to form
* The callback must accept value of element
* and must return error string or true
* if there are no errors
*
* @see Form::init()
*/

protected function init(){
$this->addValidator('title', function($val){
/**
* This is just an example how a custom validation
* callback could be added to form
* The callback must accept value of element
* and must return error string or true
* if there are no errors
*
* @see Form::init()
*/
/*$this->addValidator('title', function($val){
if(\mb_strlen($val) < 1){
return 'Title must contain at least 1 character long';
}
return true;
});*/

if(strlen($val) < 10){
return 'Title must contain at least 10 letters';
}
$minTags = $this->oRegistry->Ini->MIN_QUESTION_TAGS;
$maxTags = $this->oRegistry->Ini->MAX_QUESTION_TAGS;

$d = 'Please enter between %s and %s tags, separated by spaces';
$this->setVar('tags_d', sprintf($d, $minTags, $maxTags));
if($minTags > 0){
$tagsRequired = '(* %s)';
$this->setVar('tags_required', sprintf($tagsRequired, 'required'));
}

return true;
});
$minTitle = $this->oRegistry->Ini->MIN_TITLE_CHARS;
if($minTitle > 0){
$t = 'Please enter a descriptive title at least %s characters long';
$this->setVar('title_d', sprintf($t, $minTitle));
}
}


Expand All @@ -103,11 +120,28 @@ protected function init(){
*/
protected function doValidate(){

$this->validateBody()->validateTags();
$this->validateBody()->validateTitle()->validateTags();

}


/**
* Validate title length
*
* @return object $this
*/
protected function validateTitle(){
$t = $this->oRegistry->Request['title'];
$min = $this->oRegistry->Ini->MIN_TITLE_CHARS;
d('min title: '.$min);
if(\mb_strlen($t) < $min){
$this->setError('title', 'Title must contain at least '.$min.' letters');
}

return $this;
}


/**
* Validate min number of words in question
* and min number of chars in question
Expand Down Expand Up @@ -152,23 +186,32 @@ protected function validateBody(){
* @return object $this
*/
protected function validateTags(){
$min = $this->oRegistry->Ini->MIN_QUESTION_TAGS;
$max = $this->oRegistry->Ini->MAX_QUESTION_TAGS;
$tags = $this->oRegistry->Request->get('tags', 's', '');
$tags = trim($tags);
if(empty($tags)){
if(($min > 0) && empty($tags)){
$this->setError('tags', 'You must include at least one tag');
}

\mb_regex_encoding('UTF-8');
$aTags = \mb_split('([\s,;]+)', $tags);
$count = count($tags);

if(count($aTags) > $max){
if($count > $aTags){
/**
* @todo Translate string
*/
$this->setError('tags', 'Question cannot have more than '.$max.' tags. Please remove some tags');
}

if($count < $min){
/**
* @todo Translate string
*/
$this->setError('tags', 'Question must have at least '.$min.' tag(s)');
}

return $this;
}

Expand Down

0 comments on commit b9d57fc

Please sign in to comment.