dmBridge PHP API
DMAbstractForm.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 
00030 abstract class DMAbstractForm {
00031 
00036    const ALIAS_PTR_SEPARATOR = "|";
00037 
00042    const DATA_PREFIX = "dmForm";
00043 
00044    private static $captcha_operands = array();
00045 
00046    protected $is_valid = false;
00047    private $fields = array();
00048 
00053    public static function getCaptchaOperands() {
00054       if (count(self::$captcha_operands) < 2) {
00055          for ($i = 0; $i < 2; $i++) {
00056             self::$captcha_operands[$i] = rand(0, 10);
00057          }
00058       }
00059       return self::$captcha_operands;
00060    }
00061 
00062    public function __construct() {
00063       $this->init();
00064       $post = DMHTTPRequest::getCurrent()->getRepresentation()->getFormValue(
00065             DMAbstractForm::DATA_PREFIX);
00066       if (is_array($post)) {
00067          $this->loadFromPOSTData($post);
00068       } else {
00069          $this->loadFromDataStore();
00070       }
00071    }
00072 
00073    public function __wakeup() {
00074       $post = DMHTTPRequest::getCurrent()->getRepresentation()->getFormValue(
00075             DMAbstractForm::DATA_PREFIX);
00076       if (is_array($post)) {
00077          $this->loadFromPOSTData($post);
00078       }
00079    }
00080 
00087    public function addField(DMFormField $field) {
00088       $field->setForm($this);
00089       $this->fields[] = $field;
00090    }
00091 
00096    public function getFields() {
00097       return $this->fields;
00098    }
00099 
00106    public function getFieldByName($name) {
00107       foreach ($this->getFields() as $f) {
00108          if ($f->getName() == $name) {
00109             return $f;
00110          }
00111       }
00112       return null;
00113    }
00114 
00121    public function getFieldsByName($name) {
00122       $fields = array();
00123       foreach ($this->getFields() as $f) {
00124          $bracket_pos = strpos($f->getName(), "[");
00125          if (($f->getName() == $name)
00126                || substr($f->getName(), 0, $bracket_pos) == $name) {
00127             $fields[] = $f;
00128          }
00129       }
00130       return $fields;
00131    }
00132 
00138    abstract protected function init();
00139 
00146    protected function loadFromDataStore() {
00147    }
00148 
00154    protected function loadFromPOSTData(array $data) {
00155       foreach ($data as $field => $value) {
00156          $matching_fields = $this->getFieldsByName($field);
00157          foreach ($matching_fields as $mf) {
00158             $mf->setUserData(true);
00159          }
00160 
00161          if (count($matching_fields) == 1) {
00162             $matching_fields[0]->setValue($value);
00163             if ($matching_fields[0] instanceof DMCheckboxField
00164                   || $matching_fields[0] instanceof DMRadioField) {
00165                $matching_fields[0]->setChecked(true);
00166             }
00167          } else if (count($matching_fields) > 1) {
00168             if (!is_array($value)) {
00169                $value = array($value);
00170             }
00171             for ($i = 0; $i < count($value); $i++) {
00172                foreach ($matching_fields as $mf) {
00173                   if ($mf instanceof DMRadioField
00174                         || $mf instanceof DMCheckboxField) {
00175                      if ($mf->getValue() == $value[$i]) {
00176                         $mf->setChecked(true);
00177                         break;
00178                      }
00179                   }
00180                }
00181                $matching_fields[$i]->setValue($value[$i]);
00182             } // for
00183          } // else if
00184       } // foreach
00185    }
00186 
00192    public function checkRadioWithNameAndValue($name, $value) {
00193       foreach ($this->getFieldsByName($name) as $f) {
00194          if (!$f instanceof DMRadioField) {
00195             continue;
00196          }
00197          $f->setChecked(($f->getValue() == $value));
00198       }
00199    }
00200 
00208    public function validate() {
00209       foreach ($this->getFields() as $f) {
00210          if (!$f->isValid()) {
00211             throw new DMIllegalArgumentException(
00212                DMLocalizedString::getString("INVALID_FORM_DATA"));
00213          }
00214       }
00215       $this->is_valid = true;
00216    }
00217 
00218 }
 All Data Structures Functions Variables