Skip to content

Commit

Permalink
Work on ask module
Browse files Browse the repository at this point in the history
  • Loading branch information
voltan committed Jul 31, 2017
1 parent a09d06b commit af237f3
Show file tree
Hide file tree
Showing 29 changed files with 877 additions and 178 deletions.
16 changes: 16 additions & 0 deletions asset/css/front.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.question-list {
margin-top: 15px;
}

.question-single {
padding-bottom: 5px;
margin-bottom: 5px;
border-bottom: 1px solid #ddd;
}

.question-single .question-single-title h3 {
padding: 5px 0;
margin: 0;
}

.question-single .question-single-time {}
2 changes: 1 addition & 1 deletion config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'meta' => array(
'title' => _a('Ask'),
'description' => _a('Ask and Answer'),
'version' => '0.2.0',
'version' => '0.2.2',
'license' => 'New BSD',
'logo' => 'image/logo.png',
'readme' => 'docs/readme.txt',
Expand Down
6 changes: 6 additions & 0 deletions config/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
'permission' => 'public',
'block' => 1,
),
array(
'title' => _a('Project page'),
'controller' => 'project',
'permission' => 'public',
'block' => 1,
),
array(
'title' => _a('Answer'),
'controller' => 'answer',
Expand Down
4 changes: 3 additions & 1 deletion sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ CREATE TABLE `{project}` (
`seo_title` VARCHAR(255) NOT NULL DEFAULT '',
`seo_keywords` VARCHAR(255) NOT NULL DEFAULT '',
`seo_description` VARCHAR(255) NOT NULL DEFAULT '',
`manager` INT(10) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `title` (`title`),
KEY `status` (`status`),
KEY `time_create` (`time_create`)
KEY `time_create` (`time_create`),
KEY `manager` (`manager`)
);
69 changes: 69 additions & 0 deletions src/Api/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Pi Engine (http://pialog.org)
*
* @link http://code.pialog.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://pialog.org
* @license http://pialog.org/license.txt New BSD License
*/

/**
* @author Hossein Azizabadi <[email protected]>
*/
namespace Module\Ask\Api;

use Pi;
use Pi\Application\Api\AbstractApi;

/*
* Pi::api('project', 'ask')->getProject($parameter, $type);
* Pi::api('project', 'ask')->getProjectList();
* Pi::api('project', 'ask')->canonizeProject($project);
*/

class Project extends AbstractApi
{
public function getProject($parameter, $type = 'id')
{
// Get project
$project = Pi::model('project', $this->getModule())->find($parameter, $type);
$project = $this->canonizeProject($project);
return $project;
}

public function getProjectList()
{
$list = array();
$where = array('status' => 1);
$order = array('time_create DESC', 'id DESC');
$select = Pi::model('project', $this->getModule())->select()->where($where)->order($order);
$rowset = Pi::model('project', $this->getModule())->selectWith($select);
foreach ($rowset as $row) {
$list[$row->id] = $this->canonizeProject($row);
}
return $list;
}

public function canonizeProject($project)
{
// Check
if (empty($project)) {
return '';
}
// boject to array
$project = $project->toArray();
// Set times
$project['time_create_view'] = _date($project['time_create']);
$project['time_update_view'] = _date($project['time_update']);
// Set text_description
$project['text_description'] = Pi::service('markup')->render($project['text_description'], 'html', 'html');
// Set question url
$project['projectUrl'] = Pi::url(Pi::service('url')->assemble('ask', array(
'module' => $this->getModule(),
'controller' => 'project',
'slug' => $project['slug'],
)));
// return question
return $project;
}
}
13 changes: 11 additions & 2 deletions src/Api/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function canonizeQuestion($question)
if (empty($question)) {
return '';
}
// Get config
$config = Pi::service('registry')->config->read($this->getModule());
// boject to array
$question = $question->toArray();
// Set times
Expand All @@ -74,6 +72,17 @@ public function canonizeQuestion($question)
}
$question['tag'] = $tagList;
}
// Set user
$question['user'] = Pi::user()->get($question['uid'], array(
'id', 'identity', 'name', 'email'
));
/* $question['user']['avatar'] = Pi::service('user')->avatar($question['user']['id'], 'large', array(
'alt' => $question['user']['name'],
'class' => 'img-circle',
));
$question['user']['profileUrl'] = Pi::url(Pi::service('user')->getUrl('profile', array(
'id' => $question['user']['id'],
))); */
// Set info for Q and A
switch ($question['type']) {
case 'Q':
Expand Down
74 changes: 74 additions & 0 deletions src/Controller/Admin/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,91 @@
namespace Module\Ask\Controller\Admin;

use Pi;
use Pi\Filter;
use Pi\Mvc\Controller\ActionController;
use Module\Ask\Form\ProjectForm;
use Module\Ask\Form\ProjectFilter;

class ProjectController extends ActionController
{
public function indexAction()
{
$projects = array();
// Set info
$order = array('time_create DESC', 'id DESC');
// Set select
$select = $this->getModel('project')->select()->order($order);
$rowset = $this->getModel('project')->selectWith($select);
// Make list
foreach ($rowset as $row) {
$projects[$row->id] = Pi::api('project', 'ask')->canonizeProject($row);
}
// Set view
$this->view()->setTemplate('project-index');
$this->view()->assign('projects', $projects);
}

public function updateAction()
{
// Get id
$id = $this->params('id');
// Set form
$form = new ProjectForm('project');
$form->setAttribute('enctype', 'multipart/form-data');
if ($this->request->isPost()) {
$data = $this->request->getPost();
// Set slug
$slug = ($data['slug']) ? $data['slug'] : $data['title'];
$filter = new Filter\Slug;
$data['slug'] = $filter($slug);
// Form filter
$form->setInputFilter(new ProjectFilter);
$form->setData($data);
if ($form->isValid()) {
$values = $form->getData();
// Set seo_title
$title = ($values['seo_title']) ? $values['seo_title'] : $values['title'];
$filter = new Filter\HeadTitle;
$values['seo_title'] = $filter($title);
// Set seo_keywords
$keywords = ($values['seo_keywords']) ? $values['seo_keywords'] : $values['title'];
$filter = new Filter\HeadKeywords;
$filter->setOptions(array(
'force_replace_space' => (bool)$this->config('force_replace_space'),
));
$values['seo_keywords'] = $filter($keywords);
// Set seo_description
$description = ($values['seo_description']) ? $values['seo_description'] : $values['title'];
$filter = new Filter\HeadDescription;
$values['seo_description'] = $filter($description);
// Set if new
if (empty($values['id'])) {
// Set time
$values['time_create'] = time();
}
// Set time_update
$values['time_update'] = time();
// Save values
if (!empty($values['id'])) {
$row = $this->getModel('project')->find($values['id']);
} else {
$row = $this->getModel('project')->createRow();
}
$row->assign($values);
$row->save();
// jump
$message = __('Project data saved successfully.');
$this->jump(array('action' => 'index'), $message);
}
} else {
if ($id) {
$project = $this->getModel('project')->find($id)->toArray();
$form->setData($project);
}
}
// Set view
$this->view()->setTemplate('project-update');
$this->view()->assign('form', $form);
$this->view()->assign('title', __('Add project'));
}
}
3 changes: 1 addition & 2 deletions src/Controller/Admin/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function indexAction()
// Make list
foreach ($rowset as $row) {
$question[$row->id] = Pi::api('question', 'ask')->canonizeQuestion($row);

}
// Set paginator
$count = array('count' => new \Zend\Db\Sql\Predicate\Expression('count(*)'));
Expand Down Expand Up @@ -123,7 +122,7 @@ public function updateAction()
}
// Set slug
$slug = ($values['slug']) ? $values['slug'] : $values['title'];
$slug = $slug . ' ' . $question['time_create'];
$slug = sprintf('%s-%s', $slug, _date($question['time_create'], array('pattern' => 'yyyy MM dd H I')));
$filter = new Filter\Slug;
$values['slug'] = $filter($slug);
// Set seo_title
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/Front/AnswerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function indexAction()
$values['time_update'] = time();
// Set slug
$filter = new Filter\Slug;
$values['slug'] = $filter($values['title'] . ' ' . _date($values['time_create']));
$slug = sprintf('%s-%s', $values['title'], _date($values['time_create']));
$values['slug'] = $filter($slug);

// Set seo_title
$filter = new Filter\HeadTitle;
$values['seo_title'] = $filter($values['title']);
Expand Down
44 changes: 27 additions & 17 deletions src/Controller/Front/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,33 @@ public function indexAction()
$module = $this->params('module');
// Get config
$config = Pi::service('registry')->config->read($module);
// Set question info
$where = array('status' => 1, 'type' => 'Q');
// Set paginator info
$template = array(
'controller' => 'index',
'action' => 'index',
);
// Get question List
$questions = $this->askList($where);
// Get paginator
$paginator = $this->askPaginator($template, $where);
// Set view
$this->view()->setTemplate('question_list');
$this->view()->assign('questions', $questions);
$this->view()->assign('paginator', $paginator);
$this->view()->assign('config', $config);
$this->view()->assign('title', __('List of all questions'));
// Check
if ($config['project_active']) {
$projects = Pi::api('project', 'ask')->getProjectList();
// Set view
$this->view()->setTemplate('project-list');
$this->view()->assign('projects', $projects);
$this->view()->assign('config', $config);
$this->view()->assign('title', __('List of all projects'));
} else {
// Set question info
$where = array('status' => 1, 'type' => 'Q');
// Set paginator info
$template = array(
'controller' => 'index',
'action' => 'index',
);
// Get question List
$questions = $this->askList($where);
// Get paginator
$paginator = $this->askPaginator($template, $where);
// Set view
$this->view()->setTemplate('question-list');
$this->view()->assign('questions', $questions);
$this->view()->assign('paginator', $paginator);
$this->view()->assign('config', $config);
$this->view()->assign('title', __('List of all questions'));
}
}

public function askList($where)
Expand Down
58 changes: 58 additions & 0 deletions src/Controller/Front/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Pi Engine (http://pialog.org)
*
* @link http://code.pialog.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://pialog.org
* @license http://pialog.org/license.txt New BSD License
*/

/**
* @author Hossein Azizabadi <[email protected]>
*/
namespace Module\Ask\Controller\Front;

use Pi;
use Pi\Mvc\Controller\ActionController;
use Pi\Paginator\Paginator;
use Zend\Db\Sql\Predicate\Expression;

class ProjectController extends IndexController
{
public function indexAction()
{
// Get info from url
$module = $this->params('module');
$slug = $this->params('slug');
// Get config
$config = Pi::service('registry')->config->read($module);
// Get topic information from model
$project = Pi::api('project', 'ask')->getProject($slug, 'slug');
// Check slug set
if (empty($project) || $project['status'] != 1) {
$this->getResponse()->setStatusCode(404);
$this->terminate(__('Project not set.'), '', 'error-404');
$this->view()->setLayout('layout-simple');
return;
}
// Set question info
$where = array('status' => 1, 'type' => 'Q', 'project_id' => $project['id']);
// Set paginator info
$template = array(
'controller' => 'index',
'action' => 'index',
);
// Get question List
$questions = $this->askList($where);
// Get paginator
$paginator = $this->askPaginator($template, $where);
// Set view
$this->view()->setTemplate('question-list');
$this->view()->assign('questions', $questions);
$this->view()->assign('paginator', $paginator);
$this->view()->assign('project', $project);
$this->view()->assign('config', $config);
$this->view()->assign('title', __('List of all questions'));

}
}
Loading

0 comments on commit af237f3

Please sign in to comment.