dmBridge PHP API
DMCPEditCollectionForm.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 
00013 class DMCPEditCollectionForm extends DMAbstractForm {
00014 
00015    const NUM_RESULTS_FIELDS = 6;
00016 
00018    private $collection;
00019 
00023    public function __construct(DMCollection $col) {
00024       $this->collection = $col;
00025       parent::__construct();
00026    }
00027 
00028    protected function init() {
00029       // overview page URL
00030       $f = new DMTextField("overview_page_url");
00031       $f->addAttribute("style", "width:100%");
00032       $f->setType(DMFormFieldType::URL);
00033       $f->setRequired(false);
00034       $f->setLabel("Overview Page URL");
00035       $this->addField($f);
00036 
00037       // image URL
00038       $f = new DMTextField("image_url_512");
00039       $f->addAttribute("style", "width:100%");
00040       $f->setType(DMFormFieldType::URL);
00041       $f->setRequired(false);
00042       $f->setLabel("Image URL");
00043       $this->addField($f);
00044 
00045       // overview page
00046       $f = new DMTextareaField("description");
00047       $f->addAttribute("style", "width:100%");
00048       $f->setNumRows(5);
00049       $f->setMaxLength(2000);
00050       $f->setRequired(false);
00051       $f->setLabel("Description");
00052       $this->addField($f);
00053 
00054       // template set
00055       $f = new DMSelectField("template_set");
00056       $f->setLabel("Template set");
00057       foreach (DMConfigXML::getInstance()->getAllTemplateSets() as $ts) {
00058          $f->addOption($ts->getID(), $ts->getName());
00059       }
00060       $this->addField($f);
00061 
00062       // inheritances
00063       $f = new DMRadioField("inherit_template_set");
00064       $f->setValue(1);
00065       $f->setLabel("Inherit from Default Collection");
00066       $this->addField($f);
00067 
00068       $f = new DMRadioField("inherit_template_set");
00069       $f->setValue(0);
00070       $f->setLabel("Override");
00071       $this->addField($f);
00072 
00073       $f = new DMRadioField("inherit_object_view");
00074       $f->setValue(1);
00075       $f->setLabel("Inherit from Default Collection");
00076       $this->addField($f);
00077 
00078       $f = new DMRadioField("inherit_object_view");
00079       $f->setValue(0);
00080       $f->setLabel("Override");
00081       $this->addField($f);
00082 
00083       $f = new DMRadioField("inherit_results_view");
00084       $f->setValue(1);
00085       $f->setLabel("Inherit from Default Collection");
00086       $this->addField($f);
00087 
00088       $f = new DMRadioField("inherit_results_view");
00089       $f->setValue(0);
00090       $f->setLabel("Override");
00091       $this->addField($f);
00092 
00093       $f = new DMRadioField("inherit_search_view");
00094       $f->setValue(1);
00095       $f->setLabel("Inherit from Default Collection");
00096       $this->addField($f);
00097 
00098       $f = new DMRadioField("inherit_search_view");
00099       $f->setValue(0);
00100       $f->setLabel("Override");
00101       $this->addField($f);
00102 
00103       // object viewers
00104       $i = 0;
00105       foreach ($this->collection->getViewerDefinitions() as $def) {
00106          $f = new DMTextField("mime");
00107          $f->setMaxLength(100);
00108          $f->addAttribute("style", "width:100%");
00109          $this->addField($f);
00110 
00111          $f = new DMSelectField("class");
00112          foreach (DMObjectViewerDelegateManager::getAllViewerDelegateClassNames() as $class) {
00113             $f->addOption($class,
00114                   DMObjectViewerDelegateManager::getNameForClass($class));
00115          }
00116          $this->addField($f);
00117 
00118          $f = new DMTextField("width");
00119          $f->setSize(5);
00120          $f->setMaxLength(6);
00121          $this->addField($f);
00122 
00123          $f = new DMTextField("height");
00124          $f->setSize(4);
00125          $f->setMaxLength(6);
00126          $this->addField($f);
00127 
00128          $i++;
00129       }
00130 
00131       // results view
00132       foreach ($this->collection->getFields() as $field) {
00133          $f = new DMHiddenField("field_nicks");
00134          $f->setValue($field->getNick());
00135          $this->addField($f);
00136 
00137          $f = new DMCheckboxField("field_nicks_sortable");
00138          $f->setValue($field->getNick());
00139          $this->addField($f);
00140 
00141          $f = new DMRadioField("field_nicks_default_sort");
00142          $f->setValue($field->getNick());
00143          $this->addField($f);
00144 
00145          $f = new DMTextField("field_nicks_max_words");
00146          $f->setType(DMFormFieldType::INTEGER);
00147          $f->setSize(2);
00148          $f->setRequired(false);
00149          $f->setMin(0);
00150          $f->setMax(2000);
00151          $this->addField($f);
00152       }
00153 
00154       // grid view
00155       for ($i = 0; $i < self::NUM_RESULTS_FIELDS; $i++) {
00156          $f = new DMSelectField("grid_fields");
00157          $f->setRequired(false);
00158          $f->addOption("", DMLocalizedString::getString("DO_NOT_DISPLAY"));
00159          $f->addOption("index", "Index");
00160          $f->addOption("thumb", "Thumbnail");
00161          foreach ($this->collection->getFields() as $field) {
00162             $f->addOption($field->getNick(), $field->getName());
00163          }
00164          $this->addField($f);
00165       }
00166 
00167       // facets
00168       foreach ($this->collection->getFields() as $field) {
00169          $f = new DMCheckboxField("facet_fields");
00170          $f->setValue($field->getNick());
00171          $f->setLabel($field->getName());
00172          $this->addField($f);
00173       }
00174 
00175       // search view
00176       $f = new DMTextField("begin_year");
00177       $f->setLabel("Begin Year");
00178       $f->setSize(4);
00179       $f->setMaxLength(4);
00180       $f->setMin(-10000);
00181       $f->setMax(date('Y'));
00182       $f->setType(DMFormFieldType::INTEGER);
00183       $this->addField($f);
00184 
00185       $f = new DMTextField("end_year");
00186       $f->setLabel("End Year");
00187       $f->setSize(4);
00188       $f->setMaxLength(4);
00189       $f->setMin(-10000);
00190       $f->setMax(date('Y'));
00191       $f->setType(DMFormFieldType::INTEGER);
00192       $this->addField($f);
00193    }
00194 
00195    protected function loadFromDataStore() {
00196       // overview page URL
00197       $this->getFieldByName("overview_page_url")->setValue(
00198             (string) $this->collection->getOverviewURI());
00199       // image URL
00200       $this->getFieldByName("image_url_512")->setValue(
00201             (string) $this->collection->getImage512URI());
00202       // description
00203       $this->getFieldByName("description")->setValue(
00204             (string) $this->collection->getDescription());
00205 
00206       // template set
00207       if ($this->collection->getTemplateSet()) {
00208          $this->getFieldByName("template_set")->setValue(
00209                $this->collection->getTemplateSet()->getID());
00210       }
00211 
00212       // inheritances
00213       $this->checkRadioWithNameAndValue("inherit_template_set",
00214             $this->collection->isUsingDefaultTemplateSet() ? 1 : 0);
00215       $this->checkRadioWithNameAndValue("inherit_object_view",
00216             $this->collection->hasCustomObjectViewPrefs() ? 0 : 1);
00217       $this->checkRadioWithNameAndValue("inherit_results_view",
00218             $this->collection->hasCustomResultsViewPrefs() ? 0 : 1);
00219       $this->checkRadioWithNameAndValue("inherit_search_view",
00220             $this->collection->hasCustomSearchViewPrefs() ? 0 : 1);
00221 
00222       // object viewers
00223       $mime_fields = $this->getFieldsByName("mime");
00224       $class_fields = $this->getFieldsByName("class");
00225       $width_fields = $this->getFieldsByName("width");
00226       $height_fields = $this->getFieldsByName("height");
00227       $defs = $this->collection->getViewerDefinitions();
00228       $count = count($defs);
00229       for ($i = 0; $i < $count; $i++) {
00230          $mime_fields[$i]->setValue((string) $defs[$i]->getMediaType());
00231          $class_fields[$i]->setValue($defs[$i]->getClass());
00232          $width_fields[$i]->setValue($defs[$i]->getWidth());
00233          $height_fields[$i]->setValue($defs[$i]->getHeight());
00234       }
00235 
00236       // results view
00237       $col_fields = $this->collection->getFields();
00238       $sortable_fields = $this->getFieldsByName("field_nicks_sortable");
00239       $default_sort_fields = $this->getFieldsByName("field_nicks_default_sort");
00240       $max_words_fields = $this->getFieldsByName("field_nicks_max_words");
00241       $count = count($col_fields);
00242       for ($i = 0; $i < $count; $i++) {
00243          $sortable_fields[$i]->setChecked($col_fields[$i]->isSortable());
00244          if ($col_fields[$i]->isDefaultSort()) {
00245             foreach ($default_sort_fields as $f) {
00246                if ($f->getValue() == $col_fields[$i]->getNick()) {
00247                   $f->setChecked(true);
00248                   break;
00249                }
00250             }
00251          }
00252          $max_words_fields[$i]->setValue($col_fields[$i]->getMaxWords());
00253       }
00254 
00255       // grid view
00256       $col_fields = $this->collection->getGridViewFields();
00257       $i = 0;
00258       foreach ($this->getFieldsByName("grid_fields") as $field) {
00259          if ($i < count($col_fields)) {
00260             $field->setValue($col_fields[$i]->getNick());
00261          }
00262          $i++;
00263       }
00264 
00265       // facets
00266       foreach ($this->getFieldsByName("facet_fields") as $field) {
00267          foreach ($this->collection->getFacets() as $facet) {
00268             if ($field->getValue() == $facet->getField()->getNick()) {
00269                $field->setChecked(true);
00270                break;
00271             }
00272          }
00273       }
00274 
00275       // search view
00276       $f = $this->getFieldByName("begin_year");
00277       $f->setValue($this->collection->getDateSearchBeginYear());
00278 
00279       $f = $this->getFieldByName("end_year");
00280       $f->setValue($this->collection->getDateSearchEndYear());
00281    }
00282 
00283    public function doAction() {
00284       // overview page URL
00285       $uri = $this->getFieldByName("overview_page_url")->getValue();
00286       if (strlen($uri)) {
00287          $this->collection->setOverviewURI(new DMURI($uri));
00288       }
00289       // image URL
00290       $uri = $this->getFieldByName("image_url_512")->getValue();
00291       if (strlen($uri)) {
00292          $this->collection->setImage512URI(new DMURI($uri));
00293       }
00294       // description
00295       $desc = $this->getFieldByName("description")->getValue();
00296       $this->collection->setDescription($desc);
00297 
00298       // template set
00299       $this->collection->setTemplateSetID(
00300             $this->getFieldByName("template_set")->getValue());
00301 
00302       // inheritances
00303       $inherit_template_set = $this->getFieldByName("inherit_template_set")->isChecked();
00304       $this->collection->setUsingDefaultTemplateSet($inherit_template_set);
00305       $inherit_object_view = $this->getFieldByName("inherit_object_view")->isChecked();
00306       $this->collection->setHasCustomObjectViewPrefs(!$inherit_object_view);
00307       $inherit_results_view = $this->getFieldByName("inherit_results_view")->isChecked();
00308       $this->collection->setHasCustomResultsViewPrefs(!$inherit_results_view);
00309       $inherit_search_view = $this->getFieldByName("inherit_search_view")->isChecked();
00310       $this->collection->setHasCustomSearchViewPrefs(!$inherit_search_view);
00311 
00312       // object viewers
00313       $this->collection->unsetViewerDefinitions();
00314       if (!$inherit_object_view) {
00315          $mime_fields = $this->getFieldsByName("mime");
00316          $class_fields = $this->getFieldsByName("class");
00317          $width_fields = $this->getFieldsByName("width");
00318          $height_fields = $this->getFieldsByName("height");
00319          $count = count($mime_fields);
00320          for ($i = 0; $i < $count; $i++) {
00321             $def = new DMObjectViewerDefinition(
00322                   DMMediaType::getTypeForString($mime_fields[$i]->getValue()));
00323             $def->setClass($class_fields[$i]->getValue());
00324             $def->setWidth($width_fields[$i]->getValue());
00325             $def->setHeight($height_fields[$i]->getValue());
00326             $this->collection->addViewerDefinition($def);
00327          }
00328       }
00329 
00330       // results view
00331       $this->collection->unsetFields();
00332       $this->collection->unsetGridViewFields();
00333       $this->collection->unsetFacets();
00334       if (!$inherit_results_view) {
00335          $field_nicks = $this->getFieldsByName("field_nicks");
00336          $sortables = $this->getFieldsByName("field_nicks_sortable");
00337          $default_sorts = $this->getFieldsByName("field_nicks_default_sort");
00338          $max_words = $this->getFieldsByName("field_nicks_max_words");
00339          $count = count($sortables);
00340          for ($i = 0; $i < $count; $i++) {
00341             $field = new DMDCElement($field_nicks[$i]->getValue());
00342             $field->setSortable($sortables[$i]->isChecked());
00343             $field->setDefaultSort($default_sorts[$i]->isChecked());
00344             $field->setMaxWords($max_words[$i]->getValue());
00345             $this->collection->addField($field);
00346          }
00347 
00348          // grid view
00349          foreach ($this->getFieldsByName("grid_fields") as $field) {
00350             if ($field->getValue()) {
00351                $elem = new DMDCElement($field->getValue());
00352                $this->collection->addGridViewField($elem);
00353             }
00354          }
00355 
00356          // facets
00357          foreach ($this->getFieldsByName("facet_fields") as $field) {
00358             if ($field->isChecked()) {
00359                $term = new DMFacetTerm(new DMDCElement($field->getValue()));
00360                $this->collection->addFacet($term);
00361             }
00362          }
00363       }
00364 
00365       // search view
00366       if ($inherit_search_view) {
00367          $this->collection->unsetDateSearchRange();
00368       } else {
00369          $this->collection->setDateSearchRange(
00370                $this->getFieldByName("begin_year")->getValue(),
00371                $this->getFieldByName("end_year")->getValue());
00372       }
00373 
00374       DMConfigXML::getInstance()->saveCollection($this->collection);
00375    }
00376 
00377 }
 All Data Structures Functions Variables