Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
phproberto committed Dec 6, 2013
1 parent 9e65ae7 commit 89e5ebf
Show file tree
Hide file tree
Showing 15 changed files with 2,157 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# OSX
.DS_Store
._*
.Spotlight-V100
.Trashes

# Windows
Thumbs.db
Desktop.ini

# PHPStorm
.idea/

# Sublime Text
*.sublime*

# Eclipse
.buildpath
.project
.settings

# Temp files
*.tmp
*.bak
*.swp
*~.nib
*~

# Never ignore
!.gitignore
!index.html
!index.php
340 changes: 340 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
lib_sample
Sample Joomla! library
==========

Sample Joomla! Library to use as reference
To use it as reference for 3rd part developers. It includes common libraries requirements:

* Setup autoloading
* Language autoloading
* Media folder + assets management
* Form fields
* Form rules
* JHtml helpers

## Use

To use the library you have to include and use it like:

```php
// Include the library
JLoader::import('sample.library');

// Load Assets from media
SampleHelperAsset::load('sample.css', 'sample');
SampleHelperAsset::load('sample.js', 'sample');

// Custom helper
$result = SampleHelperNumbers::sum(array(4,3));

// JHtml use
JHtml::_('sample.fontawesome');
```

## Form example

This is a sample form that uses the library form fields and form rules

```XML
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset addfieldpath="/libraries/sample/form/field" addrulepath="/libraries/sample/form/rule">
<field
name="username"
type="text"
label="COM_SAMPLE_FIELD_NAME"
description="COM_SAMPLE_FIELD_NAME_DESC"
validate="login"
/>
<field
name="list"
type="sample"
label="COM_SAMPLE_FIELD_LIST"
description="COM_SAMPLE_FIELD_LIST_DESC"
default="1"
>
</field>
</fieldset>
</form>
```

The first field uses the `login` form rule (which is just a copy&rename of the standard `username` joomla rule) to validate a username/login.

The second field uses a custom field defined inside the library (`sample`).
92 changes: 92 additions & 0 deletions form/field/sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* @package Sample.Library
* @subpackage Field
*
* @copyright Copyright (C) 2013 Roberto Segura. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('list');

/**
* Sample form field
*
* @package Sample.Library
* @subpackage Field
* @since 1.0
*/
class JFormFieldSample extends JFormFieldList
{
/**
* The form field type.
*
* @var string
*/
protected $type = 'Sample';

/**
* Cached array of the category items.
*
* @var array
*/
protected static $options = array();

/**
* Available predefined options
*
* @var array
*/
protected $predefinedOptions = array(
1 => 'LIB_SAMPLE_FIELD_SAMPLE_OPTION1',
2 => 'LIB_SAMPLE_FIELD_SAMPLE_OPTION2'
);

/**
* Translate options labels ?
*
* @var boolean
*/
protected $translate = true;

/**
* Method to get the options to populate list
*
* @return array The field option objects.
*/
protected function getOptions()
{
// Hash for caching
$hash = md5($this->element);
$type = strtolower($this->type);

if (!isset(static::$options[$type][$hash]) && !empty($this->predefinedOptions))
{
static::$options[$type][$hash] = parent::getOptions();

$options = array();

// Allow to only use specific values of the predefined list
$filter = isset($this->element['filter']) ? explode(',', $this->element['filter']) : array();

foreach ($this->predefinedOptions as $value => $text)
{
if (empty($filter) || in_array($value, $filter))
{
$text = $this->translate ? JText::_($text) : $text;

$options[] = (object) array(
'value' => $value,
'text' => $text
);
}
}

static::$options[$type][$hash] = array_merge(static::$options[$type][$hash], $options);
}

return static::$options[$type][$hash];
}
}
62 changes: 62 additions & 0 deletions form/rule/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Form Rule class for the Joomla Platform.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
class JFormRuleLogin extends JFormRule
{
/**
* Method to test the username for uniqueness.
*
* @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param JRegistry $input An optional JRegistry object with the entire data set to validate against the entire form.
* @param JForm $form The form object for which the field is being tested.
*
* @return boolean True if the value is valid, false otherwise.
*
* @since 11.1
*/
public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
{
// Get the database object and a new query object.
$db = JFactory::getDbo();
$query = $db->getQuery(true);

// Build the query.
$query->select('COUNT(*)')
->from('#__users')
->where('username = ' . $db->quote($value));

// Get the extra field check attribute.
$userId = ($form instanceof JForm) ? $form->getValue('id') : '';
$query->where($db->quoteName('id') . ' <> ' . (int) $userId);

// Set and query the database.
$db->setQuery($query);
$duplicate = (bool) $db->loadResult();

if ($duplicate)
{
return false;
}

return true;
}
}
71 changes: 71 additions & 0 deletions helper/asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* @package Sample.Library
* @subpackage Helper
*
* @copyright Copyright (C) 2013 Roberto Segura. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('_JEXEC') or die;

/**
* Asset helper
*
* @package Sample.Library
* @subpackage Helper
* @since 1.0
*/
abstract class SampleHelperAsset extends JHtml
{
/**
* Includes assets from media directory, looking in the
* template folder for a style override to include.
*
* @param string $filename Path to file.
* @param string $extension Current extension name. Will auto detect component name if null.
* @param array $attribs Extra attribs array
*
* @return mixed False if asset type is unsupported, nothing if a css or js file, and a string if an image
*/
public static function load($filename, $extension = null, $attribs = array())
{
if (is_null($extension))
{
$extensionParts = explode(DIRECTORY_SEPARATOR, JPATH_COMPONENT);
$extension = array_pop($extensionParts);
}

$toLoad = "$extension/$filename";

// Discover the asset type from the file name
$type = substr($filename, (strrpos($filename, '.') + 1));

switch (strtoupper($type))
{
case 'CSS':
return self::stylesheet($toLoad, $attribs, true, false);
break;
case 'JS':
return self::script($toLoad, false, true);
break;
case 'GIF':
case 'JPG':
case 'JPEG':
case 'PNG':
case 'BMP':
$alt = null;

if (isset($attribs['alt']))
{
$alt = $attribs['alt'];
unset($attribs['alt']);
}

return self::image($toLoad, $alt, $attribs, true);
break;
default:
return false;
}
}
}
32 changes: 32 additions & 0 deletions helper/numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @package Sample.Library
* @subpackage Helper
*
* @copyright Copyright (C) 2013 Roberto Segura. All rights reserved
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('_JEXEC') or die;

/**
* Sample class with dummy functions
*
* @package Sample.Library
* @subpackage Helper
* @since 1.0
*/
abstract class SampleHelperNumbers
{
/**
* Convert associative array into attributes.
*
* @param mixed $numbers Number or array of numbers to sum up
*
* @return string
*/
public static function sum($numbers)
{
return array_sum((array) $numbers);
}
}
Loading

0 comments on commit 89e5ebf

Please sign in to comment.