Ejemplo n.º 1
0
 /**
  * Add item to currently loaded set
  *
  * @param int $pn_row_id The row_id to add to the set. Assumed to be a key to a record in the sets's content type's table.
  * @param array $pa_labels An array of ca_set_item label values to create. The array is keyed on locale_id; each value is an array with keys set to ca_set_item_labels fields with associated values.
  * @param int $pn_user_id user_id of user adding the item. Used to check if the user has editing access to the set. If omitted no checking of access is done.
  * @param int $pn_rank position in the set of the newly added item
  * @return int Returns item_id of newly created set item entry. The item_id is a unique identifier for the row_id in the city at the specified position (rank). It is *not* the same as the row_id.
  */
 public function addItem($pn_row_id, $pa_labels = null, $pn_user_id = null, $pn_rank = null)
 {
     if (!($vn_set_id = $this->getPrimaryKey())) {
         return null;
     }
     if ($pn_user_id && !$this->haveAccessToSet($pn_user_id, __CA_SET_EDIT_ACCESS__)) {
         return false;
     }
     $vn_table_num = $this->get('table_num');
     $o_trans = null;
     if ($this->inTransaction()) {
         $o_trans = $this->getTransaction();
     }
     // Verify existance of row before adding to set
     $t_instance = $this->getAppDatamodel()->getInstanceByTableNum($vn_table_num, true);
     if ($o_trans) {
         $t_instance->setTransaction($o_trans);
     }
     if (!$t_instance->load($pn_row_id)) {
         $this->postError(750, _t('Item does not exist'), 'ca_sets->addItem()');
         return false;
     }
     // Add it to the set
     $t_item = new ca_set_items();
     if ($o_trans) {
         $t_item->setTransaction($o_trans);
     }
     $t_item->setMode(ACCESS_WRITE);
     $t_item->set('set_id', $this->getPrimaryKey());
     $t_item->set('table_num', $vn_table_num);
     $t_item->set('row_id', $pn_row_id);
     $t_item->set('type_id', $this->get('type_id'));
     $t_item->insert();
     if (!is_null($pn_rank)) {
         $t_item->set('rank', $pn_rank);
         $t_item->update();
     }
     if ($t_item->numErrors()) {
         $this->errors = $t_item->errors;
         return false;
     }
     if (is_array($pa_labels) && sizeof($pa_labels)) {
         foreach ($pa_labels as $vn_locale_id => $va_label) {
             if (!isset($va_label['caption']) || !trim($va_label['caption'])) {
                 continue;
             }
             $t_item->addLabel($va_label, $vn_locale_id);
             if ($t_item->numErrors()) {
                 $this->errors = $t_item->errors;
                 return false;
             }
         }
     } else {
         global $g_ui_locale_id;
         if (!$g_ui_locale_id) {
             $g_ui_locale_id = 1;
         }
         $t_item->addLabel(array('caption' => _t('[BLANK]')), $g_ui_locale_id);
         if ($t_item->numErrors()) {
             $t_item->delete();
             $this->errors = $t_item->errors;
             return false;
         }
     }
     return (int) $t_item->getPrimaryKey();
 }