Пример #1
0
 /**
  * Add a list of row_ids to the currently loaded set with minimal overhead.
  * Note: this method doesn't check access rights for the set
  *
  * @param array $pa_row_ids
  * @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 addItems($pa_row_ids)
 {
     $vn_set_id = $this->getPrimaryKey();
     global $g_ui_locale_id;
     if (!$g_ui_locale_id) {
         $g_ui_locale_id = 1;
     }
     if (!$vn_set_id) {
         return false;
     }
     if (!is_array($pa_row_ids)) {
         return false;
     }
     if (!sizeof($pa_row_ids)) {
         return false;
     }
     $vn_table_num = $this->get('table_num');
     $vn_type_id = $this->get('type_id');
     $va_item_values = array();
     $va_row_ids = array_unique($pa_row_ids);
     foreach ($va_row_ids as $vn_row_id) {
         $va_item_values[] = "(" . (int) $vn_set_id . "," . (int) $vn_table_num . "," . (int) $vn_row_id . "," . (int) $vn_type_id . ", '')";
     }
     if (sizeof($va_item_values)) {
         // Quickly create set item links
         // Peforming this with a single direct scales much much better than repeatedly populating a model and calling insert()
         $this->getDb()->query("INSERT INTO ca_set_items (set_id, table_num, row_id, type_id, vars) VALUES " . join(",", $va_item_values));
         if ($this->getDb()->numErrors()) {
             $this->errors = $this->getDb()->errors;
             return false;
         }
         // Get the item_ids for the newly created links
         $qr_res = $this->getDb()->query("SELECT item_id FROM ca_set_items WHERE set_id = ? AND table_num = ? AND type_id = ? AND row_id IN (?)", array((int) $vn_set_id, (int) $vn_table_num, (int) $vn_type_id, $va_row_ids));
         $va_item_ids = $qr_res->getAllFieldValues('item_id');
         // Set the ranks of the newly created links
         $this->getDb()->query("UPDATE ca_set_items SET rank = item_id WHERE set_id = ? AND table_num = ? AND type_id = ? AND row_id IN (?)", array($vn_set_id, $vn_table_num, $vn_type_id, $va_row_ids));
         // Add empty labels to newly created items
         foreach ($va_item_ids as $vn_item_id) {
             $va_label_values[] = "(" . (int) $vn_item_id . "," . (int) $g_ui_locale_id . ",'" . _t("[BLANK]") . "')";
         }
         $this->getDb()->query("INSERT INTO ca_set_item_labels (item_id, locale_id, caption) VALUES " . join(",", $va_label_values));
         if ($this->getDb()->numErrors()) {
             $this->errors = $this->getDb()->errors;
             return false;
         }
         // Index the links
         $o_indexer = new SearchIndexer($this->getDb());
         $o_indexer->reindexRows('ca_set_items', $va_item_ids);
     }
     return sizeof($va_item_values);
 }
 public function delete($pb_delete_related = false, $pa_options = null, $pa_fields = null, $pa_table_list = null)
 {
     $vb_web_set_change_log_unit_id = BaseModel::setChangeLogUnitID();
     $o_trans = null;
     if (!$this->inTransaction()) {
         $o_trans = new Transaction($this->getDb());
         $this->setTransaction($o_trans);
     }
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     $vn_id = $this->getPrimaryKey();
     if (($vn_rc = parent::delete($pb_delete_related, $pa_options, $pa_fields, $pa_table_list)) && (!$this->hasField('deleted') || caGetOption('hard', $pa_options, false))) {
         // Delete any associated attributes and attribute_values
         if (!($qr_res = $this->getDb()->query("\n\t\t\t\t\tDELETE FROM ca_attribute_values \n\t\t\t\t\tUSING ca_attributes \n\t\t\t\t\tINNER JOIN ca_attribute_values ON ca_attribute_values.attribute_id = ca_attributes.attribute_id \n\t\t\t\t\tWHERE ca_attributes.table_num = ? AND ca_attributes.row_id = ?\n\t\t\t\t", array((int) $this->tableNum(), (int) $vn_id)))) {
             $this->errors = $this->getDb()->errors();
             if ($o_trans) {
                 $o_trans->rollback();
             }
             if ($vb_web_set_change_log_unit_id) {
                 BaseModel::unsetChangeLogUnitID();
             }
             return false;
         }
         if (!($qr_res = $this->getDb()->query("\n\t\t\t\t\tDELETE FROM ca_attributes\n\t\t\t\t\tWHERE\n\t\t\t\t\t\ttable_num = ? AND row_id = ?\n\t\t\t\t", array((int) $this->tableNum(), (int) $vn_id)))) {
             $this->errors = $this->getDb()->errors();
             if ($o_trans) {
                 $o_trans->rollback();
             }
             if ($vb_web_set_change_log_unit_id) {
                 BaseModel::unsetChangeLogUnitID();
             }
             return false;
         }
         //
         // Remove any authority attributes that reference this row
         //
         if ($vn_element_type = (int) AuthorityAttributeValue::tableToElementType($this->tableName())) {
             if ($qr_res = $this->getDb()->query("\n\t\t\t\t\t\tSELECT ca.table_num, ca.row_id FROM ca_attributes ca\n\t\t\t\t\t\tINNER JOIN ca_attribute_values AS cav ON cav.attribute_id = ca.attribute_id \n\t\t\t\t\t\tINNER JOIN ca_metadata_elements AS cme ON cav.element_id = cme.element_id \n\t\t\t\t\t\tWHERE cme.datatype = ? AND cav.value_integer1 = ?\n\t\t\t\t\t", array($vn_element_type, (int) $vn_id))) {
                 $va_ids = array();
                 while ($qr_res->nextRow()) {
                     $va_ids[$qr_res->get('table_num')][] = $qr_res->get('row_id');
                 }
                 if (!($qr_res = $this->getDb()->query("\n\t\t\t\t\t\t\tDELETE FROM ca_attribute_values \n\t\t\t\t\t\t\tUSING ca_metadata_elements \n\t\t\t\t\t\t\tINNER JOIN ca_attribute_values ON ca_attribute_values.element_id = ca_metadata_elements.element_id \n\t\t\t\t\t\t\tWHERE ca_metadata_elements.datatype = ? AND ca_attribute_values.value_integer1 = ?\n\t\t\t\t\t\t", array($vn_element_type, (int) $vn_id)))) {
                     $this->errors = $this->getDb()->errors();
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     if ($vb_web_set_change_log_unit_id) {
                         BaseModel::unsetChangeLogUnitID();
                     }
                     return false;
                 } else {
                     $o_indexer = new SearchIndexer($this->getDb());
                     foreach ($va_ids as $vs_table => $va_ids) {
                         $o_indexer->reindexRows($vs_table, $va_ids, array('transaction' => $o_trans));
                     }
                 }
             }
         }
         if ($o_trans) {
             $o_trans->commit();
         }
         if ($vb_web_set_change_log_unit_id) {
             BaseModel::unsetChangeLogUnitID();
         }
         return $vn_rc;
     }
     if ($o_trans) {
         $vn_rc ? $o_trans->commit() : $o_trans->rollback();
     }
     if ($vb_web_set_change_log_unit_id) {
         BaseModel::unsetChangeLogUnitID();
     }
     return $vn_rc;
 }