Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into prep-lumen-update-…
Browse files Browse the repository at this point in the history
…packages
  • Loading branch information
rjmackay committed Apr 5, 2017
2 parents 9506232 + 38c1eef commit 4128575
Show file tree
Hide file tree
Showing 23 changed files with 954 additions and 57 deletions.
11 changes: 11 additions & 0 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ public static function init()
'upload' => $di->lazyGet('tool.uploader'),
];

// Form Stage repository parameters
$di->params['Ushahidi_Repository_Form_Stage'] = [
'form_repo' => $di->lazyGet('repository.form')
];

// Form Attribute repository parameters
$di->params['Ushahidi_Repository_Form_Attribute'] = [
'form_stage_repo' => $di->lazyGet('repository.form_stage'),
'form_repo' => $di->lazyGet('repository.form')
];

// Post repository parameters
$di->params['Ushahidi_Repository_Post'] = [
'form_attribute_repo' => $di->lazyGet('repository.form_attribute'),
Expand Down
17 changes: 17 additions & 0 deletions application/classes/Ushahidi/Repository/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public function getTotalCount(Array $where = [])
return $this->selectCount($where);
}

/**
* Get value of Form property hide_author
* if no form is found return false
* @param $form_id
* @return Boolean
*/
public function isAuthorHidden($form_id)
{
$query = DB::select('hide_author')
->from('forms')
->where('id', '=', $form_id);

$results = $query->execute($this->db)->as_array();

return count($results) > 0 ? $results[0]['hide_author'] : false;
}

/**
* Get `everyone_can_create` and list of roles that have access to post to the form
* @param $form_id
Expand Down
98 changes: 95 additions & 3 deletions application/classes/Ushahidi/Repository/Form/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,80 @@
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\FormAttribute;
use Ushahidi\Core\Entity\FormAttributeRepository;
use Ushahidi\Core\Entity\FormStageRepository;
use Ushahidi\Core\Entity\FormRepository;
use Ushahidi\Core\Traits\PostValueRestrictions;
use Ushahidi\Core\Traits\UserContext;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;

class Ushahidi_Repository_Form_Attribute extends Ushahidi_Repository implements
FormAttributeRepository
{
use UserContext;

use PostValueRestrictions;

protected $form_stage_repo;

protected $form_repo;

protected $form_id;

// Use the JSON transcoder to encode properties
use Ushahidi_JsonTranscodeRepository;

/**
* Construct
* @param Database $db
* @param FormStageRepository $form_stage_repo
* @param FormRepository $form_repo
*/
public function __construct(
Database $db,
FormStageRepository $form_stage_repo,
FormRepository $form_repo
)
{
parent::__construct($db);

$this->form_stage_repo = $form_stage_repo;
$this->form_repo = $form_repo;

}

// Ushahidi_JsonTranscodeRepository
protected function getJsonProperties()
{
return ['options', 'config'];
}

protected function getFormId($form_stage_id)
{
$form_id = $this->form_stage_repo->getFormByStageId($form_stage_id);
if ($form_id) {
return $form_id;
}
return null;
}

// Override selectQuery to fetch attribute 'key' too
protected function selectQuery(Array $where = [], $form_id = null, $form_stage_id = null)
{
$query = parent::selectQuery($where);

if (!$form_id && $form_stage_id) {
$form_id = $this->getFormId();
}

if ($this->isRestricted($form_id)) {
$query->where('response_private', '=', '0');
}

return $query;
}

// CreateRepository
public function create(Entity $entity)
{
Expand All @@ -43,6 +101,37 @@ public function create(Entity $entity)
return $this->executeInsertAttribute($this->removeNullValues($record));
}

// Override SearchRepository
public function setSearchParams(SearchData $search)
{
$form_id = null;
if ($search->form_id) {
$form_id = $search->form_id;
}

$this->search_query = $this->selectQuery([], $form_id);

$sorting = $search->getSorting();

if (!empty($sorting['orderby'])) {
$this->search_query->order_by(
$this->getTable() . '.' . $sorting['orderby'],
Arr::get($sorting, 'order')
);
}

if (!empty($sorting['offset'])) {
$this->search_query->offset($sorting['offset']);
}

if (!empty($sorting['limit'])) {
$this->search_query->limit($sorting['limit']);
}

// apply the unique conditions of the search
$this->setSearchConditions($search);
}

// SearchRepository
protected function setSearchConditions(SearchData $search)
{
Expand Down Expand Up @@ -84,7 +173,8 @@ public function getSearchFields()
// FormAttributeRepository
public function getByKey($key, $form_id = null, $include_no_form = false)
{
$query = $this->selectQuery()

$query = $this->selectQuery([], $form_id)
->select('form_attributes.*')
->join('form_stages', 'LEFT')
->on('form_stages.id', '=', 'form_attributes.form_stage_id')
Expand Down Expand Up @@ -122,7 +212,7 @@ public function getByForm($form_id)
{
$query = $this->selectQuery([
'form_stages.form_id' => $form_id,
])
], $form_id)
->select('form_attributes.*')
->join('form_stages', 'INNER')
->on('form_stages.id', '=', 'form_attributes.form_stage_id');
Expand All @@ -135,10 +225,12 @@ public function getByForm($form_id)
// FormAttributeRepository
public function getRequired($stage_id)
{
$form_id = $this->getFormId($stage_id);

$query = $this->selectQuery([
'form_attributes.form_stage_id' => $stage_id,
'form_attributes.required' => true
])
], $form_id)
->select('form_attributes.*');

$results = $query->execute($this->db);
Expand Down
107 changes: 105 additions & 2 deletions application/classes/Ushahidi/Repository/Form/Stage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,54 @@
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\FormStage;
use Ushahidi\Core\Entity\FormStageRepository;
use Ushahidi\Core\Entity\FormRepository;
use Ushahidi\Core\Traits\PostValueRestrictions;
use Ushahidi\Core\Traits\UserContext;

class Ushahidi_Repository_Form_Stage extends Ushahidi_Repository implements
FormStageRepository
{
use UserContext;

use PostValueRestrictions;

protected $form_id;
protected $form_repo;

/**
* Construct
* @param Database $db
* @param FormRepository $form_repo
*/
public function __construct(
Database $db,
FormRepository $form_repo
)
{
parent::__construct($db);

$this->form_repo = $form_repo;

}

// Ushahidi_Repository
protected function getTable()
{
return 'form_stages';
}

// Override selectQuery to fetch attribute 'key' too
protected function selectQuery(Array $where = [], $form_id = null)
{
$query = parent::selectQuery($where);

if ($this->isRestricted($form_id)) {
$query->where('show_when_published', '=', '1');
}

return $query;
}

// CreateRepository
// ReadRepository
public function getEntity(Array $data = null)
Expand All @@ -36,6 +74,37 @@ public function getSearchFields()
return ['form_id', 'label'];
}

// Override SearchRepository
public function setSearchParams(SearchData $search)
{
$form_id = null;
if ($search->form_id) {
$form_id = $search->form_id;
}

$this->search_query = $this->selectQuery([], $form_id);

$sorting = $search->getSorting();

if (!empty($sorting['orderby'])) {
$this->search_query->order_by(
$this->getTable() . '.' . $sorting['orderby'],
Arr::get($sorting, 'order')
);
}

if (!empty($sorting['offset'])) {
$this->search_query->offset($sorting['offset']);
}

if (!empty($sorting['limit'])) {
$this->search_query->limit($sorting['limit']);
}

// apply the unique conditions of the search
$this->setSearchConditions($search);
}

// Ushahidi_Repository
protected function setSearchConditions(SearchData $search)
{
Expand All @@ -51,15 +120,49 @@ protected function setSearchConditions(SearchData $search)
}
}

public function getFormByStageId($id)
{
$query = DB::select('form_id')
->from('form_stages')
->where('id', '=', $id);

$results = $query->execute($this->db);

return count($results) > 0 ? $results[0]['form_id'] : false;
}

// FormStageRepository
public function getByForm($form_id)
{
$query = $this->selectQuery(compact($form_id));
$query = $this->selectQuery(compact($form_id), $form_id);
$results = $query->execute($this->db);

return $this->getCollection($results->as_array());
}

/**
* Retrieve Hidden Stage IDs for a given form
* if no form is found return false
* @param $form_id
* @return Array
*/
public function getHiddenStageIds($form_id)
{
$stages = [];

$query = DB::select('id')
->from('form_stages')
->where('show_when_published', '=', 0);

$results = $query->execute($this->db)->as_array();

foreach($results as $stage) {
array_push($stages, $stage['id']);
}

return $stages;
}

// FormStageRepository
public function existsInForm($id, $form_id)
{
Expand All @@ -72,7 +175,7 @@ public function getRequired($form_id)
$query = $this->selectQuery([
'form_stages.form_id' => $form_id,
'form_stages.required' => true
])
], $form_id)
->select('form_stages.*');

$results = $query->execute($this->db);
Expand Down
Loading

0 comments on commit 4128575

Please sign in to comment.