SortByFunctionAndField

The Contact Form DB SortByFunctionAndField class.

Defined (1)

The class is defined in the following location(s).

/SortByFunctionAndField.php  
  1. class SortByFunctionAndField extends SortTransform { 
  2.  
  3. var $functionName; 
  4. var $fieldName; 
  5. var $reverse; 
  6. var $functionPermitted; 
  7.  
  8. /** 
  9. * @param $functionName string name of sort function such as strcmp, strcasecmp, strnatcmp 
  10. * @param $fieldName 
  11. * @param $ascDesc string 'ASC' or 'DESC' 
  12. */ 
  13. function __construct($functionName, $fieldName, $ascDesc = 'ASC') { 
  14. $this->functionName = $functionName; 
  15. $this->fieldName = $fieldName; 
  16. $this->reverse = strtoupper($ascDesc) == 'DESC'; 
  17. $this->functionPermitted = CFDBPermittedFunctions::getInstance()->isFunctionPermitted($functionName); 
  18.  
  19. /** 
  20. * @param $a array associative containing $this->field_name 
  21. * @param $b array associative containing $this->field_name 
  22. * @return int -1 if $a>$b, 0 if equal, 1 if $a<$b 
  23. */ 
  24. public function sort($a, $b) { 
  25. if (!$this->functionPermitted) { 
  26. trigger_error('Function not permitted by CFDB: ' . $this->functionName, E_USER_NOTICE); 
  27. return 0; 
  28.  
  29. if (!isset($a[$this->fieldName]) || !isset($b[$this->fieldName])) { 
  30. return 0; // ambiguous due to field missing 
  31.  
  32. $result = call_user_func_array($this->functionName, array($a[$this->fieldName], $b[$this->fieldName])); 
  33. if ($this->reverse) { 
  34. $result *= -1; 
  35. return $result; 
  36.  
  37.