<?php
/**
 * @copyright © 2007, 2008, 2009 Intrahealth International, Inc.
 * This File is part of I2CE
 *
 * I2CE is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * @package I2CE
 * @author Luke Duncan <lduncan@intrahealth.org>
 * @since v3.2.0
 * @version v3.2.0
 */
/**
 * Class for defining all the database fields used by a {@link I2CE_Form} object.
 * @package I2CE
 * @access public
 */
class I2CE_FormField_MAP extends I2CE_FormField_MAPPED {


    /**
     * Hooked method to remap a given id on a given form and field
     * @param I2CE_List $lsit
     * @param string $oldid
     * @param string $newid
     */
    public function remapField($form,$oldid,$newid) {
        $where = array(
            'operator'=>'FIELD_LIMIT',
            'style'=>'equals',
            'field'=>$this->getName(),
            'data'=>array('value'=>"$form|$oldid")
            );

        $set_sql = "'" . mysql_escape_string($form .'|'  . $newid) . "'";
        $set_func = create_function('$val',"return '$form|$newid';");
        return $this->globalFieldUpdate($where,$set_func,$set_sql);
    }

    

    /**
     * Componentizes the given $db_value based on component
     * @param string $db_value.  The non-componentized value
     * @param array $forms of stirng. The form names which we wish to componentize.
     * @param string $component The component we wish to encode
     * @returns string The componentized db_value
     */
    public function getComponentizedValue($db_value,$forms,$component) {
        list ($form,$id) = array_pad(explode('|',$db_value,2),2,'');
        if (in_array($form,$forms)) {
            return $db_value . '@' . $component;
        } else {
            return $db_value;
        }
    }


    /**
     * Componentizes the given $db_value based on component
     * @param string $db_ref.  The reference to the data
     * @param array $forms of stirng. The form names which we wish to componentize.
     * @param string $component The component we wish to encode
     * @returns string The componentized db_value
     */
    public function getSQLComponentization($db_ref,$forms,$component) {
        return I2CE_List::componentizeQuery($db_ref,$forms,$component);
    }



    /**
     *get the default display style for the given type of display
     */         
    public function getDefaultDisplayStyle($type) {
        if (count($this->getDisplayedFields($type)) > 1) {                
            return 'tree';
        } else {
            return 'list';
        }
    }
    



    /**
     * Gets the default value from the field's data
     * @param array $fieldData
     * @returns array where the first element is boolean (true if the has been a default value set, false otherwise) and the 
     * second element is the default value to be set.
     */
    protected function getDefaultValue($fieldData) {
        $default_lookup = null;
        $value_set = false;
        $value = null;
        if (array_key_exists('default_lookup',$fieldData)  && is_string($fieldData['default_lookup']) && strlen($fieldData['default_lookup'])>0) {
            $default_lookup = $fieldData['default_lookup'];
            foreach ( $this->getMapOptions() as $data) {
                if (array_key_exists('display',$data) && $data['display'] == $default_lookup) {
                    return array(true,$data['value']);
                }
            }
            //if we made it here we did not find our lookup value
            I2CE::raiseError( "Lookup value $default_lookup not found for " . $this->getHTMLName());
            return array(false,null);
        }
        //nothing to lookup handle it with the parent
        return parent::getDefaultValue($fieldData);
    }


    /**
     * Return the value of this field from the database format for the given type.
     * @param mixed $value
     */
    public function getFromDB( $value ) {
        if (strpos($value,'|') !== false) {
            return  explode( "|", $value, 2 );
        } else {
            return false;
        }
    }

    /**
     * Sets the value of this field from the posted form.
     * @param array $post The $_POST array holding the values for this form.
     */
    public function setFromPost( $post ) {
        $val = $this->getFromDB( $post);
        if (is_array($val)) {               
            $this->value = $val;
        } else {
            $this->value = array("","");
        }
    }


    /**
     * Checks to see if the value has been set.
     * @return boolean
     */
    public function issetValue() {
        return is_array($this->value);
    }

    /**
     * Return the native value for this form field.
     * @return array
     */
    public function getValue() {
        if ( !$this->isSetValue() ) {
            return array( "", "" );
        }
        return $this->value;
    }
    
    /**
     * Get the form that this field maps to
     * @returns string.
     */
    public function getMappedForm() {
        if (!$this->isSetValue()) {
            return '';
        }
        return $this->value[0];
    }

    /**
     * Get the id of the form that this field maps to
     * If not set, returns 0.
     * @returns string
     */
    public function getMappedID() {
        if (!$this->isSetValue()) {
            return '0';
        }
        if ($this->value[1] == '') {
            return '0';
        }
        return $this->value[1];
    }

    /**
     * Return the DB value for this form field.
     * @return string
     */
    public function getDBValue() {
        if ( $this->isValid() ) {
            return implode( "|", $this->getValue() );
        } else {
            return "";
        }
    }

    /**
     * Return the mapped form object.
     * @param boolean $populate 
     * @return I2CE_Form
     */
    public function getMappedFormObject( $populate = true ) {
        $mapped_form = I2CE_FormFactory::instance()->createContainer( $this->getDBValue() );
        if (!$mapped_form instanceof I2CE_Form) {
            return null;
        }
        if ( $populate ) {
            $mapped_form->populate();
        }
        return $mapped_form;
    }

    /**
     * Checks to see if the current value for this is set and valid.
     * @return boolean
     */
    public function isValid() {
        if ( !$this->isSetValue() ) {
            return false;
        }
        return count($this->value) == 2 && I2CE_Validate::checkString( $this->value[1] ) 
            && ( $this->canSelectAnyForm() || in_array($this->value[0],$this->getSelectableForms()));
    }


    /**
     * Returns the value of this field as a human readable format.
     * @param I2CE_Entry $entry If a I2CE_Entry object has been passed to this method then it will return the value for that entry assuming it's an 
     * entry for this field.
     * @return mixed
     */
    public function getDisplayValue( $entry=false ) {
        if ( $entry instanceof I2CE_Entry ) {
            $value = $entry->getValue();
        } else {
            if ( !$this->isValid() ) {
                return "";
            }
            $value = $this->getValue();
        }
        return I2CE_List::lookup( $value[1], $value[0] );
    }


    /**
     * Creates a drop down list of options.
     * @param I2CE_Template $template
     * @param DOMNode $node -- the node we wish to add the drop down list under
     * @param boolean $show_hidden.  Show the hidden members of the list, defaults to false.
     * @returns mixed DOMNode or an array of DOMNodes to add.
     */
    protected function create_DOMEditable_list($node, $template, $form_node,$show_hidden= false) {
        $list = $this->getMapOptions('default',$show_hidden);
        $selected =$this->getDBValue();
        $selectNode = $template->createElement('select',array( 'name'=>$this->getHTMLName()));                
        $this->setElement($selectNode);
        if ( $form_node->hasAttribute( "blank" ) ) {
            $blank_text = $form_node->getAttribute( "blank" );
        } else {
            $blank_text = "Select One";
        }
        $selectNode->appendChild( $template->createElement( "option", array( 'value' => '' ), $blank_text ) );
        foreach ($list as $d) {
            $attrs = array('value'=>$d['value']);
            if ($d['value'] == $selected) {
                $attrs['selected'] = 'selected';
            }            
            $selectNode->appendChild($template->createElement('option', $attrs, $d['display']));
        }
        $node->appendChild($selectNode);
    }



    /**
     *Check to see if the tree style is allowed.
     * @returns boolean
     */
    public function checkStyle_tree() {
        return I2CE_ModuleFactory::instance()->isEnabled('TreeSelect');
    }

    /**
     * Creates a selectable auto-suggest tree view of the options
     * @param I2CE_Template $template
     * @param DOMNode $node -- the node that requested this drop down
     * @param boolean $show_hidden.  Show the hidden members of the list, defaults to false.
     * @returns mixed DOMNode or an array of DOMNodes to add.
     */
    protected function create_DOMEditable_tree($node, $template, $form_node,$show_hidden =false) {
        $ele_name = $this->getHTMLName();
        $ele_id = 'tree:'.$ele_name;
        $tree_options = array();                
        if ($form_node->hasAttribute('treeOptions')) {
            $tree_options = $form_node->getAttribute('treeOptions');
            $form_node->removeAttribute('treeOptions');
        }
        $auto_complete_options = array();
        if ($form_node->hasAttribute('autoCompleteOptions')) {
            $auto_complete_options = $form_node->getAttribute('autoCompleteOptions');
            $form_node->removeAttribute('autoCompleteOptions');
        }
        $data = $this->getMapOptions('default',$show_hidden,false);
        $main = $template->createElement('span');        
        $node->appendChild($main);
        
        if ($this->isSetValue()) {
            $selected =  array(
                'value'=>$this->getDBValue(), 
                'display'=>I2CE_List::lookup($this->getMappedID(),$this->getMappedForm())
                );
        } else {
            $selected = array();
        }
        $template->addAutoCompleteInputTree($main, $ele_name, $ele_id, $selected,$data,$tree_options,$auto_complete_options);        
        $nodes = $template->query('./input[@id="' . $ele_id . '_inputtree_display"]',$main);
        if ($nodes->length > 0) {
            $this->setElement($nodes->item(0));
        }        
    }

}

# Local Variables:
# mode: php
# c-default-style: "bsd"
# indent-tabs-mode: nil
# c-basic-offset: 4
# End:
