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          $f = new DMTextField("max_file_size_k");
00129          $f->setSize(7);
00130          $f->setMaxLength(7);
00131          $this->addField($f);
00132 
00133          $i++;
00134       }
00135 
00136       // results view
00137       foreach ($this->collection->getFields() as $field) {
00138          $f = new DMHiddenField("field_nicks");
00139          $f->setValue($field->getNick());
00140          $this->addField($f);
00141 
00142          $f = new DMCheckboxField("field_nicks_sortable");
00143          $f->setValue($field->getNick());
00144          $this->addField($f);
00145 
00146          $f = new DMRadioField("field_nicks_default_sort");
00147          $f->setValue($field->getNick());
00148          $this->addField($f);
00149 
00150          $f = new DMTextField("field_nicks_max_words");
00151          $f->setType(DMFormFieldType::INTEGER);
00152          $f->setSize(2);
00153          $f->setRequired(false);
00154          $f->setMin(0);
00155          $f->setMax(2000);
00156          $this->addField($f);
00157       }
00158 
00159       // grid view
00160       for ($i = 0; $i < self::NUM_RESULTS_FIELDS; $i++) {
00161          $f = new DMSelectField("grid_fields");
00162          $f->setRequired(false);
00163          $f->addOption("", DMLocalizedString::getString("DO_NOT_DISPLAY"));
00164          $f->addOption("index", "Index");
00165          $f->addOption("thumb", "Thumbnail");
00166          foreach ($this->collection->getFields() as $field) {
00167             $f->addOption($field->getNick(), $field->getName());
00168          }
00169          $this->addField($f);
00170       }
00171 
00172       // facets
00173       foreach ($this->collection->getFields() as $field) {
00174          $f = new DMCheckboxField("facet_fields");
00175          $f->setValue($field->getNick());
00176          $f->setLabel($field->getName());
00177          $this->addField($f);
00178       }
00179 
00180       // search view
00181       $f = new DMTextField("begin_year");
00182       $f->setLabel("Begin Year");
00183       $f->setSize(4);
00184       $f->setMaxLength(4);
00185       $f->setMin(-10000);
00186       $f->setMax(date('Y'));
00187       $f->setType(DMFormFieldType::INTEGER);
00188       $this->addField($f);
00189 
00190       $f = new DMTextField("end_year");
00191       $f->setLabel("End Year");
00192       $f->setSize(4);
00193       $f->setMaxLength(4);
00194       $f->setMin(-10000);
00195       $f->setMax(date('Y'));
00196       $f->setType(DMFormFieldType::INTEGER);
00197       $this->addField($f);
00198    }
00199 
00200    protected function loadFromDataStore() {
00201       // overview page URL
00202       $this->getFieldByName("overview_page_url")->setValue(
00203             (string) $this->collection->getOverviewURI());
00204       // image URL
00205       $this->getFieldByName("image_url_512")->setValue(
00206             (string) $this->collection->getImage512URI());
00207       // description
00208       $this->getFieldByName("description")->setValue(
00209             (string) $this->collection->getDescription());
00210 
00211       // template set
00212       if ($this->collection->getTemplateSet()) {
00213          $this->getFieldByName("template_set")->setValue(
00214                $this->collection->getTemplateSet()->getID());
00215       }
00216 
00217       // inheritances
00218       $this->checkRadioWithNameAndValue("inherit_template_set",
00219             $this->collection->isUsingDefaultTemplateSet() ? 1 : 0);
00220       $this->checkRadioWithNameAndValue("inherit_object_view",
00221             $this->collection->hasCustomObjectViewPrefs() ? 0 : 1);
00222       $this->checkRadioWithNameAndValue("inherit_results_view",
00223             $this->collection->hasCustomResultsViewPrefs() ? 0 : 1);
00224       $this->checkRadioWithNameAndValue("inherit_search_view",
00225             $this->collection->hasCustomSearchViewPrefs() ? 0 : 1);
00226 
00227       // object viewers
00228       $mime_fields = $this->getFieldsByName("mime");
00229       $class_fields = $this->getFieldsByName("class");
00230       $width_fields = $this->getFieldsByName("width");
00231       $height_fields = $this->getFieldsByName("height");
00232       $size_fields = $this->getFieldsByName("max_file_size_k");
00233       $defs = $this->collection->getViewerDefinitions();
00234       $count = count($defs);
00235       for ($i = 0; $i < $count; $i++) {
00236          $mime_fields[$i]->setValue((string) $defs[$i]->getMediaType());
00237          $class_fields[$i]->setValue($defs[$i]->getClass());
00238          $width_fields[$i]->setValue($defs[$i]->getWidth());
00239          $height_fields[$i]->setValue($defs[$i]->getHeight());
00240          $size_fields[$i]->setValue($defs[$i]->getMaxFileSize());
00241       }
00242 
00243       // results view
00244       $col_fields = $this->collection->getFields();
00245       $sortable_fields = $this->getFieldsByName("field_nicks_sortable");
00246       $default_sort_fields = $this->getFieldsByName("field_nicks_default_sort");
00247       $max_words_fields = $this->getFieldsByName("field_nicks_max_words");
00248       $count = count($col_fields);
00249       for ($i = 0; $i < $count; $i++) {
00250          $sortable_fields[$i]->setChecked($col_fields[$i]->isSortable());
00251          if ($col_fields[$i]->isDefaultSort()) {
00252             foreach ($default_sort_fields as $f) {
00253                if ($f->getValue() == $col_fields[$i]->getNick()) {
00254                   $f->setChecked(true);
00255                   break;
00256                }
00257             }
00258          }
00259          $max_words_fields[$i]->setValue($col_fields[$i]->getMaxWords());
00260       }
00261 
00262       // grid view
00263       $col_fields = $this->collection->getGridViewFields();
00264       $i = 0;
00265       foreach ($this->getFieldsByName("grid_fields") as $field) {
00266          if ($i < count($col_fields)) {
00267             $field->setValue($col_fields[$i]->getNick());
00268          }
00269          $i++;
00270       }
00271 
00272       // facets
00273       foreach ($this->getFieldsByName("facet_fields") as $field) {
00274          foreach ($this->collection->getFacets() as $facet) {
00275             if ($field->getValue() == $facet->getField()->getNick()) {
00276                $field->setChecked(true);
00277                break;
00278             }
00279          }
00280       }
00281 
00282       // search view
00283       $f = $this->getFieldByName("begin_year");
00284       $f->setValue($this->collection->getDateSearchBeginYear());
00285 
00286       $f = $this->getFieldByName("end_year");
00287       $f->setValue($this->collection->getDateSearchEndYear());
00288    }
00289 
00290    public function doAction() {
00291       // overview page URL
00292       $uri = $this->getFieldByName("overview_page_url")->getValue();
00293       if (strlen($uri)) {
00294          $this->collection->setOverviewURI(new DMURI($uri));
00295       }
00296       // image URL
00297       $uri = $this->getFieldByName("image_url_512")->getValue();
00298       if (strlen($uri)) {
00299          $this->collection->setImage512URI(new DMURI($uri));
00300       }
00301       // description
00302       $desc = $this->getFieldByName("description")->getValue();
00303       $this->collection->setDescription($desc);
00304 
00305       // template set
00306       $this->collection->setTemplateSetID(
00307             $this->getFieldByName("template_set")->getValue());
00308 
00309       // inheritances
00310       $inherit_template_set = $this->getFieldByName("inherit_template_set")->isChecked();
00311       $this->collection->setUsingDefaultTemplateSet($inherit_template_set);
00312       $inherit_object_view = $this->getFieldByName("inherit_object_view")->isChecked();
00313       $this->collection->setHasCustomObjectViewPrefs(!$inherit_object_view);
00314       $inherit_results_view = $this->getFieldByName("inherit_results_view")->isChecked();
00315       $this->collection->setHasCustomResultsViewPrefs(!$inherit_results_view);
00316       $inherit_search_view = $this->getFieldByName("inherit_search_view")->isChecked();
00317       $this->collection->setHasCustomSearchViewPrefs(!$inherit_search_view);
00318 
00319       // object viewers
00320       $this->collection->unsetViewerDefinitions();
00321       if (!$inherit_object_view) {
00322          $mime_fields = $this->getFieldsByName("mime");
00323          $class_fields = $this->getFieldsByName("class");
00324          $width_fields = $this->getFieldsByName("width");
00325          $height_fields = $this->getFieldsByName("height");
00326          $size_fields = $this->getFieldsByName("max_file_size_k");
00327          $count = count($mime_fields);
00328          for ($i = 0; $i < $count; $i++) {
00329             $def = new DMObjectViewerDefinition(
00330                   DMMediaType::getTypeForString($mime_fields[$i]->getValue()));
00331             $def->setClass($class_fields[$i]->getValue());
00332             $def->setWidth($width_fields[$i]->getValue());
00333             $def->setHeight($height_fields[$i]->getValue());
00334             $def->setMaxFileSize($size_fields[$i]->getValue());
00335             $this->collection->addViewerDefinition($def);
00336          }
00337       }
00338 
00339       // results view
00340       $this->collection->unsetFields();
00341       $this->collection->unsetGridViewFields();
00342       $this->collection->unsetFacets();
00343       if (!$inherit_results_view) {
00344          $field_nicks = $this->getFieldsByName("field_nicks");
00345          $sortables = $this->getFieldsByName("field_nicks_sortable");
00346          $default_sorts = $this->getFieldsByName("field_nicks_default_sort");
00347          $max_words = $this->getFieldsByName("field_nicks_max_words");
00348          $count = count($sortables);
00349          for ($i = 0; $i < $count; $i++) {
00350             $field = new DMDCElement($field_nicks[$i]->getValue());
00351             $field->setSortable($sortables[$i]->isChecked());
00352             $field->setDefaultSort($default_sorts[$i]->isChecked());
00353             $field->setMaxWords($max_words[$i]->getValue());
00354             $this->collection->addField($field);
00355          }
00356 
00357          // grid view
00358          foreach ($this->getFieldsByName("grid_fields") as $field) {
00359             if ($field->getValue()) {
00360                $elem = new DMDCElement($field->getValue());
00361                $this->collection->addGridViewField($elem);
00362             }
00363          }
00364 
00365          // facets
00366          foreach ($this->getFieldsByName("facet_fields") as $field) {
00367             if ($field->isChecked()) {
00368                $term = new DMFacetTerm(new DMDCElement($field->getValue()));
00369                $this->collection->addFacet($term);
00370             }
00371          }
00372       }
00373 
00374       // search view
00375       if ($inherit_search_view) {
00376          $this->collection->unsetDateSearchRange();
00377       } else {
00378          $this->collection->setDateSearchRange(
00379                $this->getFieldByName("begin_year")->getValue(),
00380                $this->getFieldByName("end_year")->getValue());
00381       }
00382 
00383       DMConfigXML::getInstance()->saveCollection($this->collection);
00384    }
00385 
00386 }
 All Data Structures Functions Variables