Permalink
Browse files

Review controller base code

Fix some code
Fix some tests
  • Loading branch information...
1 parent c09d8b4 commit a6268ff683b4d4be35bf89fa977016996d69afaf @stefanneculai committed Aug 16, 2012
@@ -195,7 +195,7 @@ protected function getArrayFields($fields)
* @param JInput $input The input object.
* @param JApplicationBase $app The application object.
*
- * @since 12.1
+ * @since 1.0
*/
public function __construct($type, JInput $input = null, JApplicationBase $app = null)
{
@@ -219,7 +219,7 @@ public function __construct($type, JInput $input = null, JApplicationBase $app =
/**
* Prunes fields in an array of JContent objects to a set list.
*
- * @param mixed $list An array of Jcontent or a Jcontent object
+ * @param mixed $list An array of JContent or a JContent object
* @param array $fields An array of the field names to preserve (strip all others).
*
* @return mixed
@@ -239,6 +239,7 @@ protected function pruneFields($list, $fields)
// Flip the fields so we can find the intersection by the array keys.
$fields = array_flip($fields);
+ // $list is an array of JContent
if (is_array($list))
{
/* @var $object JContent */
@@ -254,6 +255,8 @@ protected function pruneFields($list, $fields)
$list[$key] = $this->mapFieldsOut($list[$key]);
}
}
+
+ // $list a JContent object
else
{
$list = array_uintersect_assoc(
@@ -268,6 +271,7 @@ protected function pruneFields($list, $fields)
// All fields should be returned
else
{
+ // $list is an array of JContent
if (is_array($list))
{
foreach ($list as $key => $object)
@@ -277,6 +281,8 @@ protected function pruneFields($list, $fields)
$list[$key] = $this->mapFieldsOut($list[$key]);
}
}
+
+ // $list a JContent object
else
{
$list = (array) $list->dump();
@@ -425,7 +431,7 @@ protected function itemExists($id, $type)
return $exists;
}
- /** Get the user_id from input and check if it exists
+ /** Get the user_id from input and check if it exists in database
*
* @return boolean
*
@@ -443,7 +449,7 @@ protected function checkUserId()
}
/**
- * Get the user ID associated with the content
+ * Get the user_id passed in input or null if it does not exist in database
*
* @return string
*
@@ -30,7 +30,7 @@ class WebServiceControllerV1JsonBaseCreate extends WebServiceControllerV1Base
*/
protected function init()
{
- // Set the fields
+ // Read fields from config file
$this->readFields();
// Init mandatory fields
@@ -45,6 +45,7 @@ protected function init()
$this->optionalFields['media'] = $this->getMedia();
}
+ // Load user
$this->loadUser();
}
@@ -241,6 +242,7 @@ protected function loadUser()
$session = $this->app->getSession();
$session->set('user', new JUser($user_id));
+ // Remove user_id from mandatory fields, because we have it stored is session
unset($this->mandatoryFields['user_id']);
}
@@ -250,6 +252,8 @@ protected function loadUser()
$this->app->errors->addError("201", array($this->input->get->getString('user_id')));
}
}
+
+ // Get a guest session
else
{
$session = $this->app->getSession();
@@ -290,10 +294,10 @@ public function execute()
* @return JContent
*
* @since 1.0
+ * @throws Exception - if there was a problem when creating the content
*/
protected function createContent()
{
-
$fields = implode(',', $this->mapFieldsIn(array_keys($this->mandatoryFields)));
$fields = $fields . ',' . implode(',', $this->mapFieldsIn(array_keys($this->optionalFields)));
@@ -41,35 +41,35 @@ class WebServiceControllerV1JsonBaseDelete extends WebServiceControllerV1Base
protected $before = 'now';
/**
- * Get route parts from the input or the default one
+ * Get the content ID from the input. It may also return '*' refeering all the content
*
* @return string
*
* @since 1.0
*/
protected function getContentId()
{
- // Get route from the input
- $route = $this->input->get->getString('@route');
+ // Get content id from input and convert it to integer
+ $id = $this->input->get->getInteger('content_id');
- // Break route into more parts
- $routeParts = explode('/', $route);
-
- // Contet is not refered by a number id
- if ( count($routeParts) > 0 && (!is_numeric($routeParts[0]) || $routeParts[0] < 0) && !empty($routeParts[0]))
+ // Check if content_id is set
+ if (isset($id))
{
- $this->app->errors->addError("301");
- return;
+ // Check if ID is positive
+ if ($id <= 0)
+ {
+ $this->app->errors->addError("301");
+ return;
+ }
+ else
+ {
+ return $id;
+ }
}
-
- // All content is refered
- if ( count($routeParts) == 0 || strlen($routeParts[0]) === 0 )
+ else
{
return $this->id;
}
-
- // Specific content id
- return $routeParts[0];
}
/**
@@ -83,9 +83,13 @@ protected function getSince()
{
$since = $this->input->get->getString('since');
+ // Check if since is set
if (isset($since))
{
+ // Convert since to date
$date = new JDate($since);
+
+ // Test if date exists
if (!empty($since) && checkdate($date->__get('month'), $date->__get('day'), $date->__get('year')))
{
return $date->toSql();
@@ -96,6 +100,7 @@ protected function getSince()
}
else
{
+ // Return the default date
$date = new JDate($this->since);
return $date->toSql();
}
@@ -110,12 +115,15 @@ protected function getSince()
*/
protected function getBefore()
{
-
$before = $this->input->get->getString('before');
+ // Check if before is set
if (isset($before))
{
+ // Convert before to date
$date = new JDate($before);
+
+ // Check if date exists
if (!empty($before) && checkdate($date->__get('month'), $date->__get('day'), $date->__get('year')))
{
return $date->toSql();
@@ -126,6 +134,7 @@ protected function getBefore()
}
else
{
+ // Return the default date
$date = new JDate($this->before);
return $date->toSql();
}
@@ -140,6 +149,7 @@ protected function getBefore()
*/
protected function init()
{
+ // Read fields from config file
$this->readFields();
// Content id
@@ -198,6 +208,7 @@ public function deleteContent()
$modelState->set('content.type', $this->type);
$modelState->set('content.id', $this->id);
+ // Set since and before
$modelState->set('filter.since', $this->since);
$modelState->set('filter.before', $this->before);
Oops, something went wrong. Retry.

0 comments on commit a6268ff

Please sign in to comment.