/**
  * Add the content of this region into the content index
  * @param  boolean $item_id [description]
  * @param  boolean $rev [description]
  * @return [type]       [description]
  */
 public function index($rev = false)
 {
     if ($rev === false) {
         $rev = $this->regionLatestRev();
     }
     $Items = new PerchContent_Items();
     // clear out old items
     $sql = 'DELETE FROM ' . PERCH_DB_PREFIX . 'content_index 
             WHERE regionID=' . $this->db->pdb((int) $this->id()) . ' AND itemRev<' . $this->db->pdb((int) $Items->get_oldest_rev($this->id()));
     $this->db->execute($sql);
     $items = $Items->get_for_region($this->id(), $rev);
     if (PerchUtil::count($items)) {
         $sql = 'DELETE FROM ' . PERCH_DB_PREFIX . 'content_index 
                 WHERE regionID=' . $this->db->pdb((int) $this->id()) . ' AND itemRev=' . $this->db->pdb((int) $rev);
         $this->db->execute($sql);
         $Template = new PerchTemplate('content/' . $this->regionTemplate(), 'content');
         $tags = $Template->find_all_tags_and_repeaters('content');
         $tag_index = array();
         if (PerchUtil::count($tags)) {
             foreach ($tags as $Tag) {
                 if (!isset($tag_index[$Tag->id()])) {
                     $tag_index[$Tag->id()] = $Tag;
                 }
             }
         }
         foreach ($items as $Item) {
             $fields = PerchUtil::json_safe_decode($Item->itemJSON(), true);
             $sql = 'INSERT INTO ' . PERCH_DB_PREFIX . 'content_index (itemID, regionID, pageID, itemRev, indexKey, indexValue) VALUES ';
             $values = array();
             $id_set = false;
             if (PerchUtil::count($fields)) {
                 foreach ($fields as $key => $value) {
                     if (isset($tag_index[$key])) {
                         $tag = $tag_index[$key];
                         if ($tag->no_index()) {
                             continue;
                         }
                         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['itemID'] = (int) $Item->itemID();
                                 $data['regionID'] = (int) $this->id();
                                 $data['pageID'] = (int) $Item->pageID();
                                 $data['itemRev'] = (int) $Item->itemRev();
                                 $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['itemID'] = (int) $Item->itemID();
                 $data['regionID'] = (int) $this->id();
                 $data['pageID'] = (int) $Item->pageID();
                 $data['itemRev'] = (int) $Item->itemRev();
                 $data['indexKey'] = $this->db->pdb('_id');
                 $data['indexValue'] = (int) $Item->itemID();
                 $values[] = '(' . implode(',', $data) . ')';
             }
             // natural order
             $data = array();
             $data['itemID'] = (int) $Item->itemID();
             $data['regionID'] = (int) $this->id();
             $data['pageID'] = (int) $Item->pageID();
             $data['itemRev'] = (int) $Item->itemRev();
             $data['indexKey'] = $this->db->pdb('_order');
             $data['indexValue'] = $this->db->pdb($Item->itemOrder());
             $values[] = '(' . implode(',', $data) . ')';
             $sql .= implode(',', $values);
             $this->db->execute($sql);
         }
     }
     // optimize index
     $sql = 'OPTIMIZE TABLE ' . PERCH_DB_PREFIX . 'content_index';
     $this->db->get_row($sql);
     $Perch = Perch::fetch();
     $Perch->event('region.index', $this);
 }
 /**
  * Takes the page, region and field name and gets select box options for the dataselect field type
  * @param  string $regionPage Page path
  * @param  string $regionKey  The name of a region, as used in the page
  * @param  string $fieldID    The title of a field
  * @param  string $valueID    If set, the field to use for values
  * @return array             Options array with label and value keys for select field type.
  */
 public function find_data_select_options($regionPage, $regionKey, $fieldID, $valueID = false)
 {
     $sql = 'SELECT * FROM ' . $this->table . ' 
             WHERE regionPage=' . $this->db->pdb($regionPage) . '
                 AND regionKey=' . $this->db->pdb($regionKey);
     $region = $this->db->get_row($sql);
     if (PerchUtil::count($region)) {
         $Items = new PerchContent_Items();
         $items = $Items->get_for_region($region['regionID'], $region['regionLatestRev']);
         $opts = array();
         if (PerchUtil::count($items)) {
             foreach ($items as $Item) {
                 $details = PerchUtil::json_safe_decode($Item->itemJSON());
                 if (is_object($details)) {
                     $tmp = array();
                     $fieldIDs = explode(' ', $fieldID);
                     $label = array();
                     if (PerchUtil::count($fieldIDs)) {
                         foreach ($fieldIDs as $field) {
                             if ($details->{$field}) {
                                 $label[] = $details->{$field};
                             }
                         }
                     }
                     if ($label) {
                         $tmp['label'] = implode(' ', $label);
                         if ($valueID && $details->{$valueID}) {
                             $tmp['value'] = $details->{$valueID};
                         } else {
                             $tmp['value'] = $tmp['label'];
                         }
                     }
                     if (count($tmp)) {
                         $opts[] = $tmp;
                     }
                 }
             }
         }
         return $opts;
     }
 }