/**
 * Smarty Function: Picks a specified key from an array and returns it
 *
 * @access public
 * @param   array       Smarty parameter input array:
 *                          array: The array you want to check
 *                          key: The keyname
 *                          default: What (string) to return when array does not contain the key.
 * @param   object       Smarty object
 * @return  string      The requested filename with full path
 */
function serendipity_smarty_pickKey($params, &$smarty)
{
    if (!isset($params['array'])) {
        trigger_error("Smarty Error: " . __FUNCTION__ . ": missing 'array' parameter", E_USER_WARNING);
        return;
    }
    if (!isset($params['key'])) {
        trigger_error("Smarty Error: " . __FUNCTION__ . ": missing 'key' parameter", E_USER_WARNING);
        return;
    }
    return serendipity_pickKey($params['array'], $params['key'], $params['default']);
}
/**
 * Parse/Convert properties
 *
 * @param  array    Holds the property key array
 * @param  array    Holds the keyword key array
 * @param  int      Holds the media metadata
 * @param  int      Holds the media properties
 * @param  int      How many keyword checkboxes to display next to each other?
 * @param  boolean  Can existing data be modified?
 * @return boolean
 *
 */
function serendipity_parseMediaProperties(&$dprops, &$keywords, &$media, &$props, $keywordsPerBlock, $is_edit)
{
    global $serendipity;
    if (!is_array($dprops)) {
        $dprops = explode(';', $serendipity['mediaProperties']);
    }
    if (!is_array($keywords)) {
        $keywords = explode(';', $serendipity['mediaKeywords']);
    }
    $media['references'] = serendipity_db_query("SELECT link, name\n                            FROM {$serendipity['dbPrefix']}references\n                           WHERE entry_id = " . $media['id'] . "\n                             AND type = 'media'\n                        ORDER BY name DESC\n                           LIMIT 15", false, 'assoc');
    if (!is_array($media['references'])) {
        $media['references'] = false;
    }
    foreach ($dprops as $prop) {
        $type = 'input';
        $parts = explode(':', trim($prop));
        if (in_array('MULTI', $parts)) {
            $type = 'textarea';
        }
        if (preg_match('@(AUDIO|VIDEO|DOCUMENT|IMAGE|ARCHIVE|BINARY)@i', $prop)) {
            $show_item = false;
            if ($media['mediatype'] == 'video' && in_array('VIDEO', $parts)) {
                $show_item = true;
            }
            if ($media['mediatype'] == 'audio' && in_array('AUDIO', $parts)) {
                $show_item = true;
            }
            if ($media['mediatype'] == 'image' && in_array('IMAGE', $parts)) {
                $show_item = true;
            }
            if ($media['mediatype'] == 'document' && in_array('DOCUMENT', $parts)) {
                $show_item = true;
            }
            if ($media['mediatype'] == 'archive' && in_array('ARCHIVE', $parts)) {
                $show_item = true;
            }
            if ($media['mediatype'] == 'binary' && in_array('BINARY', $parts)) {
                $show_item = true;
            }
            if (!$show_item) {
                continue;
            }
        }
        if (!$is_edit) {
            $type = 'readonly';
        }
        $val =& serendipity_mediaTypeCast($parts[0], $props['base_property'][$parts[0]], true);
        $propkey = htmlspecialchars($parts[0]) . $idx;
        $media['base_property'][$propkey] = array('label' => htmlspecialchars(defined('MEDIA_PROPERTY_' . strtoupper($parts[0])) ? constant('MEDIA_PROPERTY_' . strtoupper($parts[0])) : $parts[0]), 'type' => $type, 'val' => $val, 'title' => htmlspecialchars($parts[0]));
        if (!is_array($GLOBALS['IPTC'])) {
            // Your templates config.inc.php or any of the language files can declare this variable,
            // if you want to use other default settings for this. No interface ability to declare this
            // yet, sorry.
            $GLOBALS['IPTC'] = array('DATE' => array('DateCreated'), 'RUN_LENGTH' => array('RunLength'), 'DPI' => array('XResolution'), 'COPYRIGHT' => array('Creator'), 'TITLE' => array('Title', 'ObjectName'), 'COMMENT1' => array('Description'), 'ALT' => array('Title', 'ObjectName'), 'COMMENT2' => array('Keywords', 'PhotoLocation'));
        }
        $default_iptc_val = null;
        if (empty($val)) {
            switch ($parts[0]) {
                case 'DATE':
                    $default_iptc_val = $now;
                case 'RUN_LENGTH':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = '00:00:00.00';
                    }
                case 'DPI':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = '72';
                    }
                case 'COPYRIGHT':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = $serendipity['serendipityUser'];
                    }
                case 'TITLE':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = $media['realname'];
                    }
                case 'ALT':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = '';
                    }
                case 'COMMENT1':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = '';
                    }
                case 'COMMENT2':
                    if (!isset($default_iptc_val)) {
                        $default_iptc_val = '';
                    }
                    $media['base_property'][$propkey]['val'] = serendipity_pickKey($media['metadata'], 'Keywords', '');
                    $new_iptc_val = false;
                    foreach ($GLOBALS['IPTC'][$parts[0]] as $iptc_key) {
                        if (empty($new_iptc_val)) {
                            $new_iptc_val = serendipity_pickKey($media['metadata'], $iptc_key, '');
                        }
                    }
                    if (empty($new_iptc_val)) {
                        $new_iptc_val = $default_iptc_val;
                    }
                    if ($parts[0] == 'DATE') {
                        $media['base_property'][$propkey]['val'] = serendipity_strftime(DATE_FORMAT_SHORT, $new_iptc_val);
                    } else {
                        $media['base_property'][$propkey]['val'] = $new_iptc_val;
                    }
                    break;
                default:
                    serendipity_plugin_api::hook_event('media_showproperties', $media, $propkey);
                    break;
            }
        }
    }
    if ($keywordsPerBlock > 0) {
        $rows = ceil(count($keywords) / $keywordsPerBlock);
        for ($i = 0; $i < $rows; $i++) {
            for ($j = 0; $j < $keywordsPerBlock; $j++) {
                $kidx = $i * $keywordsPerBlock + $j;
                if (isset($keywords[$kidx])) {
                    $media['base_keywords'][$i][$j] = array('name' => htmlspecialchars($keywords[$kidx]), 'selected' => isset($props['base_keyword'][$keywords[$kidx]]) ? true : false);
                } else {
                    $media['base_keywords'][$i][$j] = array();
                }
            }
        }
    }
}
/**
 * Smarty Function: Picks a specified key from an array and returns it
 *
 * @access public
 * @param   array       Smarty parameter input array:
 *                          array: The array you want to check
 *                          key: The keyname
 *                          default: What (string) to return when array does not contain the key.
 * @param   object       Smarty object
 * @return  string      The requested filename with full path
 */
function serendipity_smarty_pickKey($params, &$smarty)
{
    if (!isset($params['array'])) {
        $smarty->trigger_error(__FUNCTION__ . ": missing 'array' parameter");
        return;
    }
    if (!isset($params['key'])) {
        $smarty->trigger_error(__FUNCTION__ . ": missing 'key' parameter");
        return;
    }
    return serendipity_pickKey($params['array'], $params['key'], $params['default']);
}