dmBridge PHP API
DMAbstractFormField.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 abstract class DMAbstractFormField {
00016 
00018    protected $allowed_values = array();
00020    protected $attrs = array();
00022    protected $form;
00024    protected $is_enabled = true;
00026    protected $is_user_data = false;
00028    protected $is_required = true;
00030    protected $label;
00032    protected $max;
00034    protected $maxlength = 0;
00036    protected $min;
00038    protected $minlength = 0;
00040    protected $name;
00042    protected $static_validation_msg;
00044    protected $type = 1;
00046    protected $validation_msg;
00048    protected $value;
00049 
00050 
00055    public function __construct($name) {
00056       $this->setName($name);
00057    }
00058 
00063    protected function getAllowedValues() {
00064       return $this->allowed_values;
00065    }
00066 
00071    public function addAllowedValue($v) {
00072       $this->allowed_values[] = $v;
00073    }
00074 
00079    protected function getAttributes() {
00080       return $this->attrs;
00081    }
00082 
00088    public function addAttribute($k, $v) {
00089       $this->attrs[$k] = $v;
00090    }
00091 
00095    public function isEnabled() {
00096       return $this->is_enabled;
00097    }
00098 
00102    public function setEnabled($enabled) {
00103       $this->is_enabled = (bool) $enabled;
00104    }
00105 
00109    public function getForm() {
00110       return $this->form;
00111    }
00112 
00116    public function setForm(DMAbstractForm $form) {
00117       $this->form = $form;
00118    }
00119 
00124    public function getLabel() {
00125       return $this->label;
00126    }
00127 
00132    public function setLabel($str) {
00133       $this->label = $str;
00134    }
00135 
00140    protected function getHTMLName() {
00141       $name = sprintf("%s[%s]",
00142             DMAbstractForm::DATA_PREFIX,
00143             $this->getName());
00144       foreach ($this->getForm()->getFields() as $f) {
00145          if ($f != $this && $f->getName() == $this->getName()
00146                && !$f instanceof DMRadioField) {
00147             $name = sprintf("%s[%s][]",
00148                   DMAbstractForm::DATA_PREFIX,
00149                   $this->getName());
00150             break;
00151          }
00152       }
00153       return $name;
00154    }
00155 
00160    protected function getMax() {
00161       return $this->max;
00162    }
00163 
00168    public function setMax($int) {
00169       $this->max = (int) $int;
00170    }
00171 
00176    protected function getMaxLength() {
00177       return $this->maxlength;
00178    }
00179 
00184    public function setMaxLength($int) {
00185       $this->maxlength = (int) $int;
00186    }
00187 
00192    protected function getMin() {
00193       return $this->min;
00194    }
00195 
00200    public function setMin($int) {
00201       $this->min = (int) $int;
00202    }
00203 
00208    protected function getMinLength() {
00209       return $this->minlength;
00210    }
00211 
00216    public function setMinLength($int) {
00217       $this->minlength = (int) $int;
00218    }
00219 
00224    public function getName() {
00225       return $this->name;
00226    }
00227 
00232    protected function setName($name) {
00233       $this->name = $name;
00234    }
00235 
00240    protected function isRequired() {
00241       return $this->is_required;
00242    }
00243 
00248    public function setRequired($bool) {
00249       $this->is_required = (bool) $bool;
00250    }
00251 
00256    protected function getStaticValidationMessage() {
00257       return $this->static_validation_msg;
00258    }
00259 
00264    public function setStaticValidationMessage($str) {
00265       $this->static_validation_msg = (string) $str;
00266    }
00267 
00272    protected function getType() {
00273       return $this->type;
00274    }
00275 
00280    public function setType($type) {
00281       $this->type = $type;
00282    }
00283 
00287    protected function isUserData() {
00288       return $this->is_user_data;
00289    }
00290 
00294    public function setUserData($bool) {
00295       $this->is_user_data = $bool;
00296    }
00297 
00302    public function isValid() {
00303       $this->validate();
00304       return strlen($this->getValidationMessage()) ? false : true;
00305    }
00306 
00311    public function getValidationMessage() {
00312       return $this->validation_msg;
00313    }
00314 
00319    private function setValidationMessage($str) {
00320       $this->validation_msg = $str;
00321    }
00322 
00327    public function getValue() {
00328       return $this->value;
00329    }
00330 
00335    public function setValue($value) {
00336       if ($value != $this->getValue()) {
00337          $this->validation_msg = null;
00338       }
00339       $this->value = (string) $value;
00340    }
00341 
00342    protected function validate() {
00343       $this->validateRequired();
00344       $this->validateAllowedValues();
00345       switch ($this->getType()) {
00346       case DMFormFieldType::EMAIL:
00347          $this->validateEmail();
00348          break;
00349       case DMFormFieldType::INTEGER:
00350          $this->validateInteger();
00351          $this->validateRange();
00352          break;
00353       case DMFormFieldType::STRING:
00354          $this->validateLength();
00355          break;
00356       case DMFormFieldType::URL:
00357          $this->validateURL();
00358          break;
00359       }
00360 
00361       if ($this->getValidationMessage()
00362             && $this->getStaticValidationMessage()) {
00363          $this->setValidationMessage($this->getStaticValidationMessage());
00364       }
00365    }
00366 
00367    private function validateAllowedValues() {
00368       if (count($this->getAllowedValues()) > 0) {
00369          if (!in_array($this->getValue(), $this->getAllowedValues())) {
00370             $this->setValidationMessage(
00371                DMLocalizedString::getString("FORM_INVALID_VALUE"));
00372          }
00373       }
00374    }
00375 
00376    private function validateEmail() {
00377       if (strlen($this->getValue())) {
00378          if (!DMMailer::isValidEmail($this->getValue())) {
00379             $this->setValidationMessage(
00380                   DMLocalizedString::getString("FORM_INVALID_EMAIL"));
00381          }
00382       }
00383    }
00384 
00385    private function validateInteger() {
00386       if ($this->getValue() <> round($this->getValue())) {
00387          $this->setValidationMessage(
00388                DMLocalizedString::getString("FORM_NON_INTEGER"));
00389       }
00390    }
00391 
00392    private function validateLength() {
00393       if (strlen($this->getValue()) < $this->getMinLength()
00394             && $this->getMinLength() > 0) {
00395          $this->setValidationMessage(
00396             sprintf(
00397                DMLocalizedString::getString("FORM_MIN_LENGTH"),
00398                $this->getMinLength()));
00399       }
00400       else if (strlen($this->getValue()) > $this->getMaxLength()
00401             && $this->getMaxLength() > 0) {
00402          $this->setValidationMessage(
00403             sprintf(
00404                DMLocalizedString::getString("FORM_MAX_LENGTH"),
00405                $this->getMaxLength()));
00406       }
00407    }
00408 
00409    private function validateRange() {
00410       if ($this->getValue() < $this->getMin()) {
00411          $this->setValidationMessage(
00412             sprintf(DMLocalizedString::getString("FORM_MIN"),
00413                   $this->getMin()));
00414       }
00415       else if ($this->getMax() > 0 && $this->getValue() > $this->getMax()) {
00416          $this->setValidationMessage(
00417             sprintf(DMLocalizedString::getString("FORM_MAX"),
00418                   $this->getMax()));
00419       }
00420    }
00421 
00422    private function validateRequired() {
00423       if ($this->isRequired() && strlen($this->getValue()) < 1) {
00424          switch (get_class($this)) {
00425             default:
00426                $this->setValidationMessage(
00427                      DMLocalizedString::getString("FORM_REQUIRED"));
00428                break;
00429          }
00430       }
00431    }
00432 
00433    private function validateURL() {
00434       if ($this->getValue()) {
00435          try {
00436             $url = new DMURI($this->getValue());
00437          } catch (DMIllegalArgumentException $e) {
00438             $this->setValidationMessage(
00439                   DMLocalizedString::getString("FORM_INVALID_URL"));
00440          }
00441       }
00442    }
00443 
00444 }
00445 
 All Data Structures Functions Variables