Ejemplo n.º 1
0
 public function index($Template = null)
 {
     if (!$this->index_table) {
         return;
     }
     $table = PERCH_DB_PREFIX . $this->index_table;
     // clear out old items
     $sql = 'DELETE FROM ' . $table . ' WHERE itemKey=' . $this->db->pdb($this->pk) . ' AND itemID=' . $this->db->pdb($this->id());
     $this->db->execute($sql);
     $tags = $Template->find_all_tags_and_repeaters($Template->namespace);
     $tag_index = array();
     if (PerchUtil::count($tags)) {
         foreach ($tags as $Tag) {
             if (!isset($tag_index[$Tag->id()])) {
                 $tag_index[$Tag->id()] = $Tag;
             }
         }
     }
     $fields = $this->to_array(array_keys($tag_index));
     $sql = 'INSERT INTO ' . $table . ' (itemKey, itemID, indexKey, indexValue) VALUES ';
     $values = array();
     $id_set = false;
     if (PerchUtil::count($fields)) {
         foreach ($fields as $key => $value) {
             if (strpos($key, 'DynamicFields') !== false || substr($key, 0, 6) == 'perch_' || strpos($key, 'JSON') !== false) {
                 continue;
             }
             if (isset($tag_index[$key])) {
                 $tag = $tag_index[$key];
             } else {
                 $tag = new PerchXMLTag('<perch:x type="text" id="' . $key . '" />');
             }
             if ($tag->type() == 'PerchRepeater') {
                 $index_value = $tag->get_index($value);
             } else {
                 $FieldType = PerchFieldTypes::get($tag->type(), false, $tag);
                 $index_value = $FieldType->get_index($value);
             }
             if (is_array($index_value)) {
                 foreach ($index_value as $index_item) {
                     $data = array();
                     $data['itemKey'] = $this->db->pdb($this->pk);
                     $data['itemID'] = $this->pk_is_int ? (int) $this->id() : $this->db->pdb($this->id());
                     $data['indexKey'] = $this->db->pdb(substr($index_item['key'], 0, 64));
                     $data['indexValue'] = $this->db->pdb(substr($index_item['value'], 0, 255));
                     $values[] = '(' . implode(',', $data) . ')';
                     if ($index_item['key'] == '_id') {
                         $id_set = true;
                     }
                 }
             }
         }
     }
     // _id
     if (!$id_set) {
         $data = array();
         $data['itemKey'] = $this->db->pdb($this->pk);
         $data['itemID'] = $this->pk_is_int ? (int) $this->id() : $this->db->pdb($this->id());
         $data['indexKey'] = $this->db->pdb('_id');
         $data['indexValue'] = $this->pk_is_int ? (int) $this->id() : $this->db->pdb($this->id());
         $values[] = '(' . implode(',', $data) . ')';
     }
     $sql .= implode(',', $values);
     $this->db->execute($sql);
     // optimize index
     $sql = 'OPTIMIZE TABLE ' . $table;
     $this->db->get_row($sql);
     if ($this->event_prefix && !$this->suppress_events) {
         $Perch = Perch::fetch();
         $Perch->event($this->event_prefix . '.index', $this);
     }
     return true;
 }