Advertisement
AmourSpirit

PHP - cn - orderid.php

Jul 25th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @version     $Id$
  4.  * @package     cn_reports
  5.  * @subpackage
  6.  * @copyright
  7.  * @license     GNU General Public License version 2 or later.
  8.  */
  9. // Creating a server side valadition for joomla
  10. // file name: orderid.php
  11. // this is a sample rule for a Joomla Component Table Field. This example overrides the test method from the base class
  12. // and checks to see if the input via $value is a valid number and that it exist in a an order table of the database
  13. // in your form you mus implement the path in the  such as
  14. // <fieldset addrulepath="/administrator/components/com_cn_reports/models/rules">
  15. // your field must implement validate="orderid" in you table xml file.
  16. // This rule file must be saved in your joomla component folder such as
  17. // /administrator/components/com_cn_reports/models/rules/orderid.php
  18.  
  19. // No direct access
  20. defined('_JEXEC') or die;
  21.  
  22. // import Joomla formrule library
  23. jimport('joomla.form.formrule');
  24. use Joomla\Registry\Registry;
  25.  
  26. /**
  27.  * Form Rule class for the Joomla Framework.
  28. */
  29. class JFormRuleOrderid extends JFormRule
  30. {
  31.     // protected $regex = '/-?[0-9]+/';
  32.     /**
  33.      * Method to test the url for a valid parts.
  34.      *
  35.      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
  36.      * @param   mixed             $value    The form field value to validate.
  37.      * @param   string            $group    The field name group control value. This acts as as an array container for the field.
  38.      *                                      For example if the field has name="foo" and the group value is set to "bar" then the
  39.      *                                      full field name would end up being "bar[foo]".
  40.      * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
  41.      * @param   JForm             $form     The form object for which the field is being tested.
  42.      *
  43.      * @return  boolean  True if the value is valid, false otherwise.
  44.      *
  45.      * @since   11.1
  46.      */
  47.     public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
  48.     {
  49.         // If the field is empty and not required, the field is valid.
  50.         $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
  51.  
  52.         if (!$required && empty($value))
  53.         {
  54.             return true;
  55.         }
  56.  
  57.         $regxNum = '/^\d+$/';
  58.  
  59.  
  60.         // Test the value against the regular expression.
  61.         if (preg_match($regxNum, $value) == false)
  62.         {
  63.             JFactory::getApplication()->enqueueMessage(JText::_('COM_CN_REPORTS_OVERRIDES_FORM_ERR_ORDER_ID_WHOLENUMBER'),'warning');
  64.             return false;
  65.         }
  66.  
  67.         // now tha we have a valid integer lets check to see if the orderid actuall exist in the database as a valid order
  68.         $db = JFactory::getDbo();
  69.         $query = $db->getQuery(true);
  70.         $query
  71.             ->select('Count(order_id)')
  72.             ->from('oc_order')
  73.             ->where('order_id = ' . $value);
  74.         $db->setQuery($query);
  75.         $dbResult = $db->loadResult();
  76.         if ((int)$dbResult == 0)
  77.         {
  78.             JFactory::getApplication()->enqueueMessage(JText::_('COM_CN_REPORTS_OVERRIDES_FORM_ERR_ORDER_ID_NON_EXIST_ORDER'),'warning');
  79.             return false;
  80.         }
  81.         /*
  82.          * @see http://www.nanpa.com/
  83.          * @see http://tools.ietf.org/html/rfc4933
  84.          * @see http://www.itu.int/rec/T-REC-E.164/en
  85.          *
  86.          * Regex by Steve Levithan
  87.          * @see http://blog.stevenlevithan.com/archives/validate-phone-number
  88.          * @note that valid ITU-T and EPP must begin with +.
  89.          */
  90.         return true;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement