function acf_load_pof_language_choices($field)
{
    $languages = pof_settings_get_all_languages();
    if (count($languages) > 1) {
        $field['choices'] = array();
        foreach ($languages as $lang) {
            $field['choices'][$lang->lang_code] = $lang->lang_title;
        }
    }
    return $field;
}
function pof_settigs_get_active_lang_codes()
{
    global $pof_settings_langs;
    $toret = array();
    if (!isset($pof_settings_langs) || empty($pof_settings_langs) || count($pof_settings_langs) < 1) {
        $pof_settings_langs = pof_settings_get_all_languages(false);
    }
    foreach ($pof_settings_langs as $lang) {
        if ($lang->is_active || $lang->is_default) {
            array_push($toret, $lang->lang_code);
        }
    }
    return $toret;
}
function pof_taxonomy_translate_get_languages()
{
    $languages = pof_settings_get_all_languages();
    $ret = array();
    foreach ($languages as $lang) {
        if ($lang->is_active || $lang->is_default) {
            $ret[$lang->lang_code] = $lang->lang_title;
        }
    }
    /*
    		$ret['fi'] = "Suomi";
    	$ret['sv'] = "Ruotsi";
    	$ret['en'] = "Englanti";
    */
    return $ret;
}
function pof_item_task_details_add_meta_box_callback($post)
{
    $langs = pof_settings_get_all_languages();
    $guid = get_post_meta($post->ID, "post_guid", true);
    $isEmpty = true;
    foreach ($langs as $lang) {
        $dt = get_post_meta($post->ID, "content_imported_" . $lang->lang_code, true);
        if ($dt != null && $dt != "") {
            echo '<a href="/item-json/?postGUID=' . $guid . '&lang=' . $lang->lang_code . '" target="_blank">';
            echo $lang->lang_title . "</a>: " . $dt;
            echo "<br />";
            $isEmpty = false;
        }
    }
    if ($isEmpty) {
        echo "Ei importoitu.";
    }
}
function pof_importer_tasksdrivelocalizationcontent()
{
    echo '<div class="wrap">';
    echo '<h1>POF Importer, aktiviteettien kieliversio sis&auml;ll&ouml;t google drivest&auml;</h1>';
    /*
        echo "<pre>";
        print_r(pof_importer_get_agegroups_and_taskgroups());
        echo "</pre>";
    */
    if (!isset($_POST) || !isset($_POST["lang"])) {
        $langs = pof_settings_get_all_languages();
        echo "<form method=\"POST\">";
        echo "Valitse kieli: <br />";
        echo "<select name=\"lang\">";
        foreach ($langs as $lang) {
            if ($lang->lang_code == 'fi') {
                continue;
            }
            echo "<option value=\"" . $lang->lang_code . "\">" . $lang->lang_title . "</option>\n";
        }
        echo "</select>";
        echo "<br />";
        echo '<input type="submit" name="Submit" value="Valitse kieli" />';
        echo "</form>";
    } else {
        if (!isset($_POST["drive_file_id"]) && isset($_POST["lang"])) {
            $service = pof_importer_get_google_service();
            echo '<form method="post" action="">';
            // Print the names and IDs for up to 10 files.
            $optParams = array('maxResults' => 999, 'q' => "mimeType = 'application/vnd.google-apps.spreadsheet' and title != 'fi_' and title contains '" . $_POST["lang"] . "_'", 'orderBy' => 'folder,modifiedDate desc,title');
            $results = $service->files->listFiles($optParams);
            if (count($results->getItems()) == 0) {
                print "No files found.\n";
            } else {
                print "Valitse importoitava tiedosto:<br />";
                echo '<select name="drive_file_id">';
                foreach ($results->getItems() as $file) {
                    $fileLastModified = strtotime($file->getModifiedDate());
                    printf("<option value=\"%s\">%s (%s)</option>\n", $file->getId(), $file->getTitle(), date('d.m.Y', $fileLastModified));
                    echo "<br />";
                }
                echo "</select>";
                echo '<input type="hidden" name="lang" value="' . $_POST["lang"] . '" />';
                echo '<br />';
                echo '<input type="submit" name="Submit" value="Valitse tiedosto" />';
                echo '</form>';
            }
        } else {
            if (!isset($_POST["SaveToDatabase"])) {
                echo '<form method="post" action="">';
                echo '<input type="hidden" name="lang" value="' . $_POST["lang"] . '" />';
                echo '<input type="hidden" name="drive_file_id" value="' . $_POST["drive_file_id"] . '" />';
                echo '<input type="submit" name="SaveToDatabase" value="Tallenna tietokantaan" />';
                echo '<br /><br />';
                echo '<input type="submit" name="RunAgain" value="Aja uudestaan" />';
                echo '</form>';
                pof_importer_tasksdrivelocalizationcontent_run($_POST["drive_file_id"], $_POST["lang"]);
            } else {
                echo '<form method="post" action="">';
                echo '<input type="hidden" name="lang" value="' . $_POST["lang"] . '" />';
                echo '<input type="hidden" name="drive_file_id" value="' . $_POST["drive_file_id"] . '" />';
                echo '<input type="submit" name="SaveToDatabase" value="Tallenna tietokantaan" />';
                echo '<br /><br />';
                echo '<input type="submit" name="RunAgain" value="Aja uudestaan" />';
                echo '</form>';
                pof_importer_tasksdrivelocalizationcontent_run($_POST["drive_file_id"], $_POST["lang"], true);
            }
        }
    }
    echo "</div>";
}
function pof_content_status_localization_get_form()
{
    $ret = "";
    $langs = pof_settings_get_all_languages();
    $ret .= "<form method=\"POST\">";
    $ret .= "Valitse kieli: <br />";
    $ret .= "<select name=\"lang\">";
    foreach ($langs as $lang) {
        if ($lang->lang_code == 'fi') {
            continue;
        }
        $selected = "";
        if (isset($_POST) && isset($_POST["lang"])) {
            if ($_POST["lang"] == $lang->lang_code) {
                $selected = " selected=\"selected\"";
            }
        }
        $ret .= "<option" . $selected . " value=\"" . $lang->lang_code . "\">" . $lang->lang_title . "</option>\n";
    }
    $ret .= "</select>";
    $ret .= "<br />";
    $ret .= "Valitse ik&auml;kausi: <br />";
    $ret .= '<select name="agegroup">';
    $agegroups = pof_taxonomy_translate_get_agegroups();
    foreach ($agegroups as $agegroup) {
        if ($agegroup->id == 0) {
            continue;
        }
        $selected = "";
        if (isset($_POST) && isset($_POST["agegroup"])) {
            if ($_POST["agegroup"] == $agegroup->id) {
                $selected = " selected=\"selected\"";
            }
        }
        $ret .= "<option" . $selected . " value=\"" . $agegroup->id . "\">" . $agegroup->title . "</option>\n";
    }
    $ret .= '</select>';
    $ret .= '<br /><br /><input type="submit" name="Submit" value="N&auml;yt&auml;" />';
    $ret .= "</form>";
    return $ret;
}
function pof_settings_languages()
{
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    $languages = pof_settings_get_all_languages();
    if (isset($_POST['Submit'])) {
        global $wpdb;
        global $pof_settings_db_version;
        $table_name_languages = pof_settings_get_table_name_languages(false);
        if (isset($_POST["pof_settings_add_lang"]) && !empty($_POST["pof_settings_add_lang"]) && isset($_POST["pof_settings_add_lang"]["title"]) && !empty($_POST["pof_settings_add_lang"]["title"]) && isset($_POST["pof_settings_add_lang"]["code"]) && !empty($_POST["pof_settings_add_lang"]["code"])) {
            $lang_active = 0;
            if (isset($_POST["pof_settings_add_lang"]["active"]) && !empty($_POST["pof_settings_add_lang"]["active"]) && $_POST["pof_settings_add_lang"]["active"] == "1") {
                $lang_active = 1;
            }
            $default = 0;
            if (empty($languages)) {
                $default = 1;
                $lang_active = 1;
            }
            $tmp = $wpdb->insert($table_name_languages, array('lang_title' => $_POST["pof_settings_add_lang"]["title"], 'lang_code' => $_POST["pof_settings_add_lang"]["code"], 'is_active' => (int) $lang_active, 'is_default' => (int) $default), array('%s', '%s', '%d'));
        }
        if (isset($_POST["pof_settings_language"]) && !empty($_POST["pof_settings_language"])) {
            foreach ($_POST["pof_settings_language"] as $lang_id => $lang) {
                if (isset($lang["title"]) && !empty($lang["title"]) && isset($lang["code"]) && !empty($lang["code"])) {
                    $lang_active = 0;
                    if (isset($lang["active"]) && !empty($lang["active"]) && $lang["active"] == "1") {
                        $lang_active = 1;
                    }
                    $tmp = $wpdb->update($table_name_languages, array('lang_title' => $lang["title"], 'lang_code' => $lang["code"], 'is_active' => (int) $lang_active), array('id' => $lang_id), array('%s', '%s', '%d'), array('%d'));
                }
            }
        }
        $languages = pof_settings_get_all_languages(false);
    }
    echo '<div class="wrap">';
    echo '<h1>POF Settings, kielet</h1>';
    echo '<form method="post" action="">';
    echo '<table cellpadding="2" cellspacing="2" border="2">';
    echo '<thead>';
    echo '<tr>';
    echo '<th><h2>Kieli</h2></th>';
    echo '<th><h2>Koodi</h2></th>';
    echo '<th><h2>Aktiivinen</h2></th>';
    echo '<th><h2>Default</h2></th>';
    echo '<tr>';
    echo '</thead>';
    echo '<tbody>';
    foreach ($languages as $language) {
        ?>
	<tr>
		<td><input type="text" name="pof_settings_language[<?php 
        echo $language->id;
        ?>
][title]" value="<?php 
        echo $language->lang_title;
        ?>
" /></td>
		<td><input type="text" name="pof_settings_language[<?php 
        echo $language->id;
        ?>
][code]"  value="<?php 
        echo $language->lang_code;
        ?>
" /></td>
<?php 
        if ($language->is_default) {
            ?>
		<td>X</td>
		<td>X</td>

<?php 
        } else {
            ?>
		<td><input type="checkbox" name="pof_settings_language[<?php 
            echo $language->id;
            ?>
][active]" value="1" <?php 
            if ($language->is_active) {
                echo " checked='checked'";
            }
            ?>
 /></td>
		<td></td>
<?php 
        }
        ?>
	</tr>

<?php 
    }
    ?>

	<tr>
		<td><input type="text" name="pof_settings_add_lang[title]" /></td>
		<td><input type="text" name="pof_settings_add_lang[code]" /></td>
		<td><input type="checkbox" name="pof_settings_add_lang[active]" value="1" /></td>
	</tr>

<?php 
    echo '</tbody>';
    echo '</table>';
    echo '<br /><input type="submit" name="Submit" value="Submit" />';
    echo '</form>';
    echo '</div>';
}
function pof_content_status_images_content_get_tasks($taskgroup_id, $indentation = 0)
{
    $langs = pof_settings_get_all_languages();
    $langs_count = count($langs);
    $args = array('numberposts' => -1, 'posts_per_page' => -1, 'post_type' => 'pof_post_task', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => 'suoritepaketti', 'meta_value' => $taskgroup_id);
    $the_query = new WP_Query($args);
    $intendation_str = "";
    if ($indentation > 0) {
        for ($i = 0; $i < $indentation; $i++) {
            $intendation_str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
    }
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            // try to avoid infinite loops
            if ($the_query->post->ID == $taskgroup_id) {
                continue;
            }
            ?>
            <tr>
                <td>Aktiviteetti</td>
                <td class="title"><?php 
            echo $intendation_str . "<a href=\"/wp-admin/post.php?post=" . $the_query->post->ID . "&action=edit\" target=\"_blank\">" . $the_query->post->post_title . "</a>";
            ?>
</td>
                <td><?php 
            echo pof_content_status_images_get_image_field($the_query->post->ID, 'logo_image');
            ?>
</td>
                <td><?php 
            echo pof_content_status_images_get_image_field($the_query->post->ID, 'main_image');
            ?>
</td>
                <?php 
            foreach ($langs as $lang) {
                ?>
                    <td><?php 
                echo pof_content_status_images_get_additional_images($the_query->post->ID, $lang->lang_code);
                ?>
</td>
                    <td><?php 
                echo pof_content_status_images_get_additional_files($the_query->post->ID, $lang->lang_code);
                ?>
</td>
                    <td><?php 
                echo pof_content_status_images_get_additional_links($the_query->post->ID, $lang->lang_code);
                ?>
</td>
                    <?php 
            }
            ?>
                    <td><?php 
            echo pof_content_status_images_get_additional_images($the_query->post->ID, "");
            ?>
</td>
                    <td><?php 
            echo pof_content_status_images_get_additional_files($the_query->post->ID, "");
            ?>
</td>
                    <td><?php 
            echo pof_content_status_images_get_additional_links($the_query->post->ID, "");
            ?>
</td>
            </tr>

            <?php 
        }
    }
    wp_reset_query();
    pof_content_status_images_clear_additional_content_arrays();
}
function pof_content_status_get_suggestions($post_id)
{
    global $field_counters;
    $tmp = array();
    $args = array('numberposts' => -1, 'posts_per_page' => -1, 'post_type' => 'pof_post_suggestion', 'meta_key' => 'pof_suggestion_task', 'meta_value' => $post_id);
    $the_query_suggestion = new WP_Query($args);
    if ($the_query_suggestion->have_posts()) {
        while ($the_query_suggestion->have_posts()) {
            $the_query_suggestion->the_post();
            $lang = get_post_meta($the_query_suggestion->post->ID, "pof_suggestion_lang", true);
            if (!array_key_exists($lang, $tmp)) {
                $tmp[$lang] = 1;
            } else {
                $tmp[$lang]++;
            }
        }
    }
    $langs = pof_settings_get_all_languages();
    foreach ($langs as $lang) {
        if (!array_key_exists('suggestion_' . $lang->lang_code, $field_counters)) {
            $field_counters['suggestion_' . $lang->lang_code] = new stdClass();
            $field_counters['suggestion_' . $lang->lang_code]->total = 0;
            $field_counters['suggestion_' . $lang->lang_code]->green = 0;
        }
        $field_counters['suggestion_' . $lang->lang_code]->total++;
        if (array_key_exists($lang->lang_code, $tmp)) {
            echo "<td class=\"pof_content_status_green\">";
            $field_counters['suggestion_' . $lang->lang_code]->green++;
            echo $tmp[$lang->lang_code];
            echo "</td>";
        } else {
            echo "<td></td>";
        }
    }
}