/**
  * Init form storage options
  * @param string $form 
  */
 protected function init_data($form)
 {
     if (in_array($form, $this->init_status)) {
         //already done
         return true;
     }
     if (!parent::init_data($form)) {
         return false;
     }
     if (!array_key_exists('svs', $this->namespaces[$form])) {
         $this->namespaces[$form]['svs'] = "svs:urn:ihe:iti:svs:2008";
     }
     $options = $this->getStorageOptions($form);
     if (!$options instanceof I2CE_MagicDataNode) {
         I2CE::raiseError("Invalid storage options for {$form}");
         return false;
     }
     $url = false;
     if (is_array($this->global_options) && array_key_exists('url', $this->global_options) && is_string($this->global_options['url']) && strlen($this->global_options['url']) > 0) {
         $url = $this->global_options['url'];
     }
     $options->setIfIsSet($url, "url", true);
     $curl_opts = array('HEADER' => 0);
     $cache_time = 0;
     if (is_array($this->global_options) && array_key_exists('cache_time', $this->global_options) && is_scalar($this->global_options['cache_time'])) {
         $cache_time = (int) $this->global_options['cache_time'];
     }
     $options->setIfIsSet($cache_time, "cache_time");
     $cache_time = (int) $cache_time;
     $request_args = array();
     if (array_key_exists('request_args', $this->global_options) && is_array($this->global_options['request_args'])) {
         $request_args = $this->global_options['request_args'];
     }
     $options->setIfIsSet($request_args, "request_args", true);
     $services = array('getRecords' => array('results' => '//svs:Concept/@id', 'url' => $url, 'request_args' => $request_args, 'cache_time' => $cache_time, 'transforms' => array('out' => '0')), 'getAllRecordData' => array('url' => $url, 'request_args' => $request_args, 'cache_time' => $cache_time, 'transforms' => array('out' => '0')));
     foreach (array_keys($services) as $endpoint) {
         if (!array_key_exists($endpoint, $this->services[$form])) {
             $this->services[$form][$endpoint] = array();
         }
     }
     foreach ($this->services[$form] as $endpoint => &$data) {
         if (!array_key_exists($endpoint, $services)) {
             continue;
         }
         I2CE_Util::merge_recursive($services[$endpoint], $data);
         $data = $services[$endpoint];
     }
     unset($data);
     return true;
 }
 /**
  * Init form storage options
  * @param string $form 
  */
 protected function init_data($form)
 {
     if (in_array($form, $this->init_status)) {
         //already done
         return true;
     }
     if (!parent::init_data($form)) {
         return false;
     }
     $options = $this->getStorageOptions($form);
     if (!$options instanceof I2CE_MagicDataNode) {
         I2CE::raiseError("Invalid storage options for {$form}");
         return false;
     }
     $options = $this->getStorageOptions($form);
     if (!$options instanceof I2CE_MagicDataNode) {
         I2CE::raiseError("Invalid storage options for {$form}");
         return false;
     }
     $directory = false;
     if (!$options->setIfIsSet($directory, 'csd_directory') || !in_array($directory, array('provider', 'service', 'facility', 'organization'))) {
         I2CE::raiseError("No valid defined for {$form}");
         return false;
     }
     if (!is_array($remote_services = I2CE::getConfig()->getAsArray("/modules/forms/storage_options/CSD/remote_services")) || !array_key_exists($directory, $remote_services) || !is_scalar($selected = $remote_services[$directory])) {
         I2CE::raiseError("No remote service directory selected in global CSD  options");
         return false;
     }
     list($t_form, $id) = array_pad(explode('|', $selected, 2), 2, '');
     if ($t_form != 'csd_info_manager' || $id == '0' || !is_array($urls = I2CE_FormStorage::lookupField('csd_info_manager', $id, array('url'), false)) || !array_key_exists('url', $urls) || !$urls['url']) {
         I2CE::raiseError("Invalid connection details from selected service: {$selected}");
     }
     if (!array_key_exists('csd', $this->namespaces[$form])) {
         $this->namespaces[$form]['csd'] = "urn:ihe:iti:csd:2013";
     }
     $cache_time = 0;
     if (is_array($this->global_options) && array_key_exists('cache_time', $this->global_options) && is_scalar($this->global_options['cache_time'])) {
         $cache_time = (int) $this->global_options['cache_time'];
     }
     $options->setIfIsSet($cache_time, "cache_time");
     $cache_time = (int) $cache_time;
     if (!array_key_exists('populate', $this->services[$form])) {
         $this->services[$form]['populate'] = array();
     }
     $populate = array('url' => $urls['url'], 'curl_opts' => array('HEADER' => 0, 'POST' => 1, 'HTTPHEADER' => array('content-type' => 'content-type: text/xml')), 'cache_time' => $cache_time);
     I2CE_Util::merge_recursive($this->services[$form]['populate'], $populate);
     return true;
 }
 /**
  * @param string $form  The form name.
  * @param boolean $parent. Defaults to false.    If it is scalar and non-boolean, it is consider to be the ID of the parent, 
  * and then we get all forms with parent the given id.
  * @param mixed $where_data array or class implementing ArrayAccess, Iterator, and Countable (e.g. MagicDataNode) . the where data.  
  * @param array $ordering. An array of fields to order by.  Defaults to the empty array.  Prepend a - to order by in descending order.
  * @param mixed $limit. Defaults to false.  It true, returns only one result.  If an integer it is the numeber of records to limit to.
  *  If it is as an array of two integers, it is the offset and then number of results to limit to.  
  * @returns mixed an array of matching form ids.  However, ff $limit_one is true or 1 or 
  * array ($offset,1) then then we return either the id or false,  if none found or there was an error.
  */
 public function search($form, $parent = false, $where_data = array(), $ordering = array(), $limit = false)
 {
     if (!$this->init_data($form) || !$this->has_service($form, 'getRecords') || is_array($where_data) && count($where_data) > 0 || $parent) {
         return parent::search($form, $parent, $where_data, $ordering, $limit);
     }
     //we have a limit.  use getRecords
     return $this->getRecords_worker($form, -1, false, $limit);
 }