Author: david
Date: 2008-09-29 11:19:14 -0700 (Mon, 29 Sep 2008)
New Revision: 1397

Added:
   trunk/web/js/multiInstanceSelect.js
Modified:
   trunk/apps/qubit/modules/informationobject/actions/editAction.class.php
   trunk/apps/qubit/modules/informationobject/actions/updateAction.class.php
   trunk/apps/qubit/modules/informationobject/templates/editISADSuccess.php
   trunk/lib/model/QubitInformationObject.php
Log:
Add jquery multiInstanceSelect.js to allow dynamically adding select boxes to 
add multiple access points to an information object on one form submit.  Create 
QubitInformationObject->addNameAccessPoint() method to move code for adding a 
new QubitActorRole relationship into model layer.


Modified: 
trunk/apps/qubit/modules/informationobject/actions/editAction.class.php
===================================================================
--- trunk/apps/qubit/modules/informationobject/actions/editAction.class.php     
2008-09-29 17:25:22 UTC (rev 1396)
+++ trunk/apps/qubit/modules/informationobject/actions/editAction.class.php     
2008-09-29 18:19:14 UTC (rev 1397)
@@ -24,10 +24,14 @@
   public function execute($request)
   {
     $this->informationObject = 
QubitInformationObject::getById($this->getRequestParameter('id'));
-
     $this->forward404Unless($this->informationObject);
 
     $request->setAttribute('informationObject', $this->informationObject);
+    
+    // Javascript libraries to allow selecting multiple access points
+    $this->getResponse()->addJavaScript('jquery');
+    $this->getResponse()->addJavaScript('/vendor/drupal/misc/drupal');
+    $this->getResponse()->addJavaScript('multiInstanceSelect');
 
     //Actor (Event) Relations
     $this->creationEvents = $this->informationObject->getCreationEvents();

Modified: 
trunk/apps/qubit/modules/informationobject/actions/updateAction.class.php
===================================================================
--- trunk/apps/qubit/modules/informationobject/actions/updateAction.class.php   
2008-09-29 17:25:22 UTC (rev 1396)
+++ trunk/apps/qubit/modules/informationobject/actions/updateAction.class.php   
2008-09-29 18:19:14 UTC (rev 1397)
@@ -299,19 +299,39 @@
       $this->foreignKeyUpdate = true;
     }
   }
-
+  
+  /**
+   * Update ObjectTermRelations (Subject and place access points)
+   * 
+   * @param QubitInformationObject $informationObject current information 
objects 
+   */
   public function updateObjectTermRelations($informationObject)
   {
-    if ($this->getRequestParameter('subject_id'))
+    if ($subject_ids = $this->getRequestParameter('subject_id'))
     {
-      
$informationObject->setTermRelation($this->getRequestParameter('subject_id'), 
QubitTaxonomy::SUBJECT_ID);
-      $this->foreignKeyUpdate = true;
+      // Make sure that $subject_id is an array, even if it's only got one 
value
+      $subject_ids = (is_array($subject_ids)) ? $subject_ids : 
array($subject_ids);
+      
+      foreach ($subject_ids as $subject_id) {
+        if (intval($subject_id)) {
+          $informationObject->setTermRelation($subject_id, 
QubitTaxonomy::SUBJECT_ID);
+          $this->foreignKeyUpdate = true;
+      
+        }
+      }
     }
 
-    if ($this->getRequestParameter('place_id'))
+    if ($place_ids = $this->getRequestParameter('place_id'))
     {
-      
$informationObject->setTermRelation($this->getRequestParameter('place_id'), 
$relationTypeId = 337);
-      $this->foreignKeyUpdate = true;
+      // Make sure that $place_id is an array, even if it's only got one value
+      $place_ids = (is_array($place_ids)) ? $place_ids : array($place_ids);
+      
+      foreach ($place_ids as $place_id) {
+        if (intval($place_id)) {
+               $informationObject->setTermRelation($place_id, 
QubitTaxonomy::PLACE_ID);
+                     $this->foreignKeyUpdate = true;
+        }
+      }
     }
   }
 
@@ -366,15 +386,17 @@
       $this->foreignKeyUpdate = true;
     }
 
-    if ($this->getRequestParameter('name_id'))
+    if ($name_ids = $this->getRequestParameter('name_id'))
     {
-        $newNameAccessPoint = new QubitEvent;
-        $newNameAccessPoint->setActorId($this->getRequestParameter('name_id'));
-        $newNameAccessPoint->setActorRoleId(QubitTerm::SUBJECT_ID);
-        
$newNameAccessPoint->setInformationObjectId($informationObject->getId());
-
-        $newNameAccessPoint->save();
-        $this->foreignKeyUpdate = true;
+      // Make sure that $name_ids is an array, even if it's only got one value
+      $name_ids = (is_array($name_ids)) ? $name_ids : array($name_ids);
+      
+      foreach ($name_ids as $name_id) {
+        if (intval($name_id)) {
+               $informationObject->addNameAccessPoint($name_id, 
QubitTerm::SUBJECT_ID);
+               $this->foreignKeyUpdate = true;
+        }
+      }
     }
   }
 

Modified: 
trunk/apps/qubit/modules/informationobject/templates/editISADSuccess.php
===================================================================
--- trunk/apps/qubit/modules/informationobject/templates/editISADSuccess.php    
2008-09-29 17:25:22 UTC (rev 1396)
+++ trunk/apps/qubit/modules/informationobject/templates/editISADSuccess.php    
2008-09-29 18:19:14 UTC (rev 1397)
@@ -310,7 +310,7 @@
           <?php endforeach; ?>
         <?php endif; ?>
         <tr valign="top">
-          <td><?php echo object_textarea_tag($newNote, 'getContent', 
array('size' => '10x1')) ?></td>
+          <td><?php echo object_textarea_tag($newNote, 'getContent', 
array('size' => '10x1', 'class'=>'multiInstance')) ?></td>
           <td><?php echo object_select_tag($newNote, 'getTypeId', array('name' 
=> 'note_type_id', 'id' => 'note_type_id', 'related_class' => 'QubitTerm', 
'include_blank' => true, 'peer_method' => 'getNoteTypes', 'style' => 'width: 
120px;')) ?></td>
         </tr>
       </table>
@@ -327,7 +327,7 @@
           <?php echo $subject->getTerm() ?>&nbsp;<?php echo 
link_to(image_tag('delete', 'align=top'), 
'informationobject/deleteTermRelation?TermRelationId='.$subject->getId()) 
?><br/>
         <?php endforeach; ?>
       <?php endif; ?>
-      <?php echo object_select_tag($newSubjectAccessPoint, 'getTermId', 
array('name' => 'subject_id', 'id' => 'subject_id', 'include_blank' => true, 
'peer_method' => 'getSubjects')) ?>
+      <?php echo object_select_tag($newSubjectAccessPoint, 'getTermId', 
array('name' => 'subject_id', 'id' => 'subject_id', 'include_blank' => true, 
'peer_method' => 'getSubjects', 'class'=>'multiInstance')) ?>
     </div>
 
     <div class="form-item">
@@ -337,7 +337,7 @@
           <?php echo $place->getTerm() ?>&nbsp;<?php echo 
link_to(image_tag('delete', 'align=top'), 
'informationobject/deleteTermRelation?TermRelationId='.$place->getId()) ?><br/>
         <?php endforeach; ?>
       <?php endif; ?>
-      <?php echo object_select_tag($newPlaceAccessPoint, 'getTermId', 
array('name' => 'place_id', 'id' => 'place_id', 'include_blank' => true, 
'peer_method' => 'getPlaces')) ?>
+      <?php echo object_select_tag($newPlaceAccessPoint, 'getTermId', 
array('name' => 'place_id', 'id' => 'place_id', 'include_blank' => true, 
'peer_method' => 'getPlaces', 'class'=>'multiInstance')) ?>
     </div>
 
     <div class="form-item">
@@ -351,7 +351,7 @@
           <br/>
         <?php endforeach; ?>
        <?php endif; ?>
-       <?php echo select_tag('name_id', options_for_select($nameSelectList, 
null, array('include_blank' => true))) ?>
+       <?php echo select_tag('name_id', options_for_select($nameSelectList, 
null, array('include_blank' => true)), array('class'=>'multiInstance')) ?>
     </div>
   </fieldset>
 

Modified: trunk/lib/model/QubitInformationObject.php
===================================================================
--- trunk/lib/model/QubitInformationObject.php  2008-09-29 17:25:22 UTC (rev 
1396)
+++ trunk/lib/model/QubitInformationObject.php  2008-09-29 18:19:14 UTC (rev 
1397)
@@ -403,31 +403,50 @@
     return QubitProperty::get($criteria);
   }
 
-public function setSubjectAccessPoint($subject)
+  public function setSubjectAccessPoint($subject)
   {
-  // add the subject to term list (assuming it's a new subject)
-  // TODO: check first to see if this term exists, in which case, just get its 
ID
-  $newTerm = new QubitTerm;
-  $newTerm->setTaxonomyId(QubitTaxonomy::SUBJECT_ID);
-  $newTerm->setName($subject);
-  $newTerm->save();
-
-  // associate this new subject term with this information object
-  $this->setTermRelation($newTerm->getId());
+         // add the subject to term list (assuming it's a new subject)
+         // TODO: check first to see if this term exists, in which case, just 
get its ID
+         $newTerm = new QubitTerm;
+         $newTerm->setTaxonomyId(QubitTaxonomy::SUBJECT_ID);
+         $newTerm->setName($subject);
+         $newTerm->save();
+       
+         // associate this new subject term with this information object
+         $this->setTermRelation($newTerm->getId());
   }
 
-public function getSubjectAccessPoints()
+  public function getSubjectAccessPoints()
   {
-  return $this->getTermRelations(QubitTaxonomy::SUBJECT_ID);
+    return $this->getTermRelations(QubitTaxonomy::SUBJECT_ID);
   }
 
-public function getPlaceAccessPoints()
+  public function getPlaceAccessPoints()
   {
-  return $this->getTermRelations(QubitTaxonomy::PLACE_ID);
+    return $this->getTermRelations(QubitTaxonomy::PLACE_ID);
   }
-
-public function getNameAccessPoints()
+  
+  /**
+   * Add a name access point to this info object
+   * 
+   * @param integer $actorId primary key of actor
+   * @param integer $actorRoleId foriegn key to QubitTerm for actor role 
taxonomy
+   * @return QubitInformationObject this object
+   */
+  public function addNameAccessPoint($actorId, 
$actorRoleId=QubitTerm::SUBJECT_ID)
   {
+    $newNameAccessPoint = new QubitEvent;
+    $newNameAccessPoint->setActorId($actorId);
+    $newNameAccessPoint->setActorRoleId($actorRoleId);
+    $newNameAccessPoint->setInformationObjectId($this->getId());
+  
+    $newNameAccessPoint->save();
+    
+    return $this;
+  }
+  
+  public function getNameAccessPoints()
+  {
     $this->nameAccessPoints = array();
     $actorEvents =  $this->informationObject->getActorEvents();
     foreach ($actorEvents as $event)
@@ -439,9 +458,9 @@
     }
   }
 
-public function getMediaTypes()
+  public function getMediaTypes()
   {
-  //TO DO: get via linked digital objects & physical objects
+    //TO DO: get via linked digital objects & physical objects
   }
 
   public function getReferenceCode(array $options = array())

Added: trunk/web/js/multiInstanceSelect.js
===================================================================
--- trunk/web/js/multiInstanceSelect.js                         (rev 0)
+++ trunk/web/js/multiInstanceSelect.js 2008-09-29 18:19:14 UTC (rev 1397)
@@ -0,0 +1,82 @@
+// Add string method for removing trailing numeric characters
+String.prototype.trimTrailingDigits = function() { return this.replace(/\d+$/, 
''); };
+
+/**
+ * Javascript to spawn new select box to create multiple relations per form 
submit.
+ *
+ * @param object thisElement dom <select> element to operate on
+ * @return void
+ */
+function multiInstanceSelector(thisElement)
+{   
+  var static 
+  var elementName = thisElement.attr('name').toString();
+  
+  // Append array braces '[]' to element name (if not present)
+  if (elementName.substr(-2) != '[]')  {
+    thisElement.attr('name', elementName += '[]');
+  }
+  
+  // String to select all elements in this set
+  var setSelector = "[name='"+elementName+"']";
+  
+  // Remove element if blank and it's not the only element in the set
+  if (thisElement.children('option:selected').attr('value') == null && 
$(setSelector).length > 1)
+  {
+    thisElement.hide('slow', function () { thisElement.remove() } );
+    // NOTE: rest of script executes before thisElement.remove is called due 
to hide('slow') delay!
+  }
+  
+  // If the last element is not blank, then add a new blank element
+  var lastElement = $(setSelector).eq(($(setSelector).length)-1);
+  if (lastElement.children('option:selected').attr('value') != null)
+  {
+   
+    // Clone lastElement and insert clone after lastElement (hidden)
+    var newElement = lastElement.clone(true);
+    newElement.css('display', 'none');
+    newElement.insertAfter(lastElement);
+      
+    // Fancy fade-in effect (ooh, ahh!)
+    newElement.show('slow');
+  }
+  
+  // Build unique ids by appending array index (e.g. thisId0, thisId1, etc.)
+  $(setSelector).each(function(i) 
+  {
+    // Remove trailing digits from "id" attribute and append array index
+    var idRoot = this.id.trimTrailingDigits();
+    this.id = idRoot + i.toString();
+    
+    // Remove already selected values
+    
+  })
+}
+
+// TODO: remove options that have already been chosen
+// from subsequent select boxes
+function restrictOptionList(setSelector, optionList)
+{
+  // alert('First option:'+fullOptionList['subject_id'][1]);
+}
+
+// On page load, link multiInstanceSelector function to "onChange" event for
+// selectboxes with class="multiInstance"
+var fullOptionList = new Array;
+Drupal.behaviors.addMultiInstanceSelect = function (context)
+{  
+  $('select.multiInstance').change(function(event)
+  {
+    multiInstanceSelector($(this));
+       });
+       
+       // Store an array of intial options for each selectbox option list
+       $('select.multiInstance').each( function(i)
+       {
+         fullOptionList[this.name] = new Array;
+         for (var j=0; j<this.options.length; j++)
+         {
+           fullOptionList[this.name][j] = this.options[j].value;
+         }
+       });
+}


Property changes on: trunk/web/js/multiInstanceSelect.js
___________________________________________________________________
Added: svn:keywords
   + Author Id Revision
Added: svn:eol-style
   + native


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.ca/group/qubit-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to