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; }
private function _cache_block_template_variation($type, $opening_tag, $condition_contents, $exact_match, $template_contents, $content_vars, $index_in_group = false) { $OpeningTag = new PerchXMLTag($opening_tag); $block_index = count($this->blocks); $this->blocks[$OpeningTag->type()] = $condition_contents; $replacement = ''; if ($block_index == 0) { $replacement = '<BLOCKS />'; } return str_replace($exact_match, $replacement, $template_contents); }
private function _replace_inputs($template, $form_inner) { $s = '/<perch:input[^>]*>/'; $count = preg_match_all($s, $template, $matches); if ($count > 0) { foreach ($matches[0] as $match) { $Tag = new PerchXMLTag($match); switch ($Tag->type()) { case 'submit': $template = $this->_replace_submit_field($match, $Tag, $template); break; case 'cms': $template = $this->_replace_cms_field($match, $Tag, $template); break; case 'textarea': $template = $this->_replace_textarea_field($match, $Tag, $template); break; case 'select': $template = $this->_replace_select_field($match, $Tag, $template); break; case 'radio': $template = $this->_replace_radio_field($match, $Tag, $template); break; case 'file': case 'image': $template = $this->_replace_file_field($match, $Tag, $template); break; default: $template = $this->_replace_basic_field($match, $Tag, $template); break; } } } return $template; }