/**
  * relationship_sort_order_update sets a relationship id to a rel sort value
  *
  * @param int $rel_id the id of the relationship to update
  * @param int $rel_sort_value the value to set the rel_sort_order column to
  * @return void
  */
 function relationship_sort_order_update($rel_id, $rel_sort_value)
 {
     update_relationship($rel_id, array('rel_sort_order' => $rel_sort_value));
 }
 function update_image_relationships($images, $order)
 {
     $i = 1;
     foreach ($order as $id) {
         if (!isset($images[$id])) {
             trigger_error('All images in $images must have their key as a value in $order, and vice versa!');
             continue;
         }
         if (!$images[$id]->get_value('rel_id')) {
             trigger_error('All images in $images must have a rel_id value.');
             continue;
         }
         update_relationship($images[$id]->get_value('rel_id'), array('rel_sort_order' => $i));
         $i++;
     }
     return true;
 }
 function setup_associated_items()
 {
     // populate associated entity selector from scratch
     $ass_es = new entity_selector();
     $ass_es->add_type($this->type_id);
     if ($this->rel_direction == 'a_to_b') {
         $ass_es->add_right_relationship($this->admin_page->id, $this->admin_page->rel_id);
     } else {
         $ass_es->add_left_relationship($this->admin_page->id, $this->admin_page->rel_id);
     }
     $ass_es->add_right_relationship_field('owns', 'entity', 'id', 'site_owner_id');
     if ($this->rel_direction == 'a_to_b' && $this->check_is_rel_sortable()) {
         $this->columns['rel_sort_order'] = true;
         $ass_es->add_field('relationship', 'id', 'rel_id');
         $ass_es->add_rel_sort_field($this->admin_page->id);
         $ass_es->set_order('relationship.rel_sort_order ASC');
         if ($this->_cur_user_has_edit_privs() && !$this->get_relationship_lock_state()) {
             $this->alter_order_enable = true;
         }
     } else {
         $ass_es->add_field('relationship', 'site', 'rel_site_id');
     }
     if ($this->assoc_viewer_order_by($ass_es)) {
         $this->alter_order_enable = false;
     }
     $this->ass_vals = $ass_es->run_one();
     // check sharing on associated entities
     foreach ($this->ass_vals as $k => $val) {
         // setup sharing value
         if ($this->site_id == $val->get_value('site_owner_id')) {
             $this->ass_vals[$k]->set_value('sharing', 'owns');
         } else {
             $this->ass_vals[$k]->set_value('sharing', $this->check_borrow_status($k));
         }
     }
     // this verifies and updates the associated items rel_sort_order if this is an a to b relationship
     if ($this->rel_direction == 'a_to_b' && $this->check_is_rel_sortable()) {
         if (count($this->ass_vals) == 1 && isset($this->columns['rel_sort_order'])) {
             unset($this->columns['rel_sort_order']);
         }
         if ($ass_es->orderby == 'relationship.rel_sort_order ASC') {
             $rel_update_array = $this->validate_rel_sort_order($this->ass_vals, true);
         } else {
             $rel_update_array = $this->validate_rel_sort_order($this->ass_vals);
         }
         if (count($rel_update_array) > 0) {
             foreach ($rel_update_array as $k => $v) {
                 update_relationship($k, array('rel_sort_order' => $v));
             }
         }
     }
 }