<?php
namespace NCBS\WebsiteBundle\Controller;
//s33: can remove now?
use Skymark\UtilitiesBundle\Controller\DefaultController as BaseController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\Response;
//s33: i think we can use just 'Controller'
use Symfony\Bundle\FrameworkBundle\Controller\Controller as SymCon;
//class NCBSWController extends BaseController
class NCBSWController extends SymCon
{
private $language = null;
protected $prefix = "NCBSW";
protected $TRANSLATION_TYPE_ID_TEXT = 1;
protected $TRANSLATION_TYPE_ID_FILE = 2;
protected $TRANSLATION_TYPE_ID_LINK = 3;
protected $SC_SIMPLE_ENCRYPT_SEED = 38;
protected $STORY_LEVEL_ID_BIBLE = 1;
protected $STORY_LEVEL_ID_VERSE = 2;
protected $STORY_LEVEL_ID_CHAPTER = 3;
protected $MEDIA_TYPE_ID_IMAGE = 1;
protected $MEDIA_TYPE_ID_AUDIO = 2;
protected $CATEGORY_ID_CONCEPT = 12;
protected $CATEGORY_ID_SPIRITUAL_TOPIC = 13;
protected $CATEGORY_ID_BIBLE_STUDY = 14;
protected $CATEGORY_ID_SERMON = 42;
protected $CATEGORY_ID_DOCUMENTS = 45;
protected $CATEGORY_ID_BIBLE_STORY = 46;
protected $CATEGORY_ID_QA = 47;
protected $CATEGORY_ID_PROJECT = 52;
protected $MULTI_URL_INDICATOR_BIBLE = 'bible';
protected $MULTI_URL_INDICATOR_BIBLE_STORYTEXT = 'biblestory';
protected $MULTI_URL_INDICATOR_SWEDENBORG_WORK = 'swedenborg';
protected $MULTI_URL_INDICATOR_EXPLANATION = 'explanation';
protected $DEFAULT_BIBLE_VERSION = 'king-james-version';
protected $DEFAULT_BIBLE_TRANSLATION_ID = 38; // king-james-version
protected $LANGUAGE_ID_CHINESE = 79;
protected $LANGUAGE_ID_JAPANESE = 205;
protected $LANGUAGE_ID_KOREAN = 231;
protected $LANGUAGE_ID_ENGLISH = 123;
protected $LANGUAGE_ID_LATIN = 246;
protected $LANGUAGE_ID_THAI = 426;
protected $LANGUAGE_ID_BURMESE = 66;
protected $LANGUAGE_ID_MALAYALAM = 268;
protected $LANGUAGE_ID_GEORGIAN = 149;
protected $LANGUAGE_ID_AMHARIC = 17;
protected $LANGUAGE_ID_TAGALOG = 425;
protected $LANGUAGE_ID_ZULU = 482;
protected $DEFAULT_PATH_TITLE_COLOR = '#6b9054;';
protected $READING_PLAN_STATUS_ID_COMPLETED = 4;
protected $RELATIONSHIP_TYPE_ID_PARALLEL = 1;
protected $USER_LEVEL_ID_EXPLANATION_AUTHOR = 2;
protected $TESTAMENT_OLDTESTAMENT = 1;
protected $TESTAMENT_NEWTESTAMENT = 2;
protected $TESTAMENT_APOCRYPHA = 3;
protected $TRANSLATION_METHOD_ID_NA = 1; // original
protected $TRANSLATION_METHOD_ID_MACHINE = 2;
protected $TRANSLATION_METHOD_ID_HUMAN = 3;
protected $TRANSLATION_METHOD_ID_MACHINE_HUMAN = 4;
protected $USER_LEVEL_ID_ADMIN = -1;
protected $USER_LEVEL_ID_APPLICATION_MANAGER = 6;
protected $USER_LEVEL_ID_LNC_ADMIN = 12;
protected $USER_LEVEL_ID_LNC_MEMBER_WEB_USER = 13;
protected $OTLE_SHOW_TYPE_ID_MUSIC = 18;
protected $PRIMARY_FORMAT_ID_TEXT = 1;
protected $PRIMARY_FORMAT_ID_AUDIO = 2;
protected $PRIMARY_FORMAT_ID_VIDEO = 3;
protected $BRAND_ID_OFF_THE_LEFT_EYE = 3;
protected $LOCATIONTYPE_ID_VIRTUAL = 1;
protected $LOCATIONTYPE_ID_IN_PERSON = 2;
protected $LNCSERMON_TYPE_ID_ADMIN = 1;
protected $LNCSERMON_TYPE_ID_PUBLIC = 2;
protected $LNCSERMON_TYPE_ID_LNC_MEMBER = 3;
protected $PAGE_MESSAGE_ID_EMAIL_BY_DATE = 1;
protected $READINGPLAN_STEP_TYPE_ID_BIBLE = 1;
protected $READINGPLAN_STEP_TYPE_ID_SWEDENBORG_WORK = 2;
protected $READINGPLAN_STEP_TYPE_ID_EXPLANATION = 3;
protected $READINGPLAN_STEP_TYPE_ID_ADHOC = 4;
protected $BRANCH_ID_LNC = 6;
protected $NC_ITEM_TYPE_ID_PLAIN_TEXT = 1;
protected $NC_ITEM_TYPE_ID_HYPERLINK = 2;
protected $NC_ITEM_TYPE_ID_FOOTNOTE = 3;
protected $NC_ITEM_TYPE_ID_TRANSLATOR_FOOTNOTE_TEXT = 4;
protected $NC_ITEM_TYPE_ID_SWEDENBORG_FOOTNOTE_TEXT = 5;
protected $NC_ITEM_TYPE_ID_NCBS_FOOTNOTE_TEXT = 6;
protected $PERSON_ROLE_ID_AUTHOR = 1;
protected $PERSON_ROLE_ID_TRANSLATOR = 2;
protected $NC_PARAMETER_ID = 1;
protected $LINK_TYPE_ID_BIBLE = 1;
protected $LINK_TYPE_ID_SWEDENBORG = 2;
protected $LINK_TYPE_ID_OTHER = 3;
public function setLanguage($languageUrl)
{
$language = $this->getRepository('Language')->getOneByUrlCached($languageUrl);
if(empty($language)) {
throw $this->createNotFoundException("We don't seem to have that language available.");
}
$this->language = $language;
}
public function getLanguage()
{
return $this->language;
}
// previously in utils ? or orm ?
function getManager() {
return $this->getDoctrine()->getManager();
}
public function getNCBSWRepository($entityName)
{
// error_log("1545 NC now .. 2251 FIND A REPO: $entityName");
// return parent::getRepository('NCBSWBundle:' . $entityName);
return $this->getManager()->getRepository('NCBSWBundle:' . $entityName);
}
function bibleData($x) {
// error_log("1551 NC: bibleData called: $x");
$em = $this->getDoctrine()->getManager();
// $em = $this->getContainer()->get('doctrine.orm.entity_manager');
$bx = $em->getRepository('NCBSWBundle:Bible\\' . $x);
return $bx;
}
public function canonicalData($canonicalEntityName)
{
return $this->getNCBSWRepository('Bible\Canonical\\' . $canonicalEntityName);
}
public function expositionData($expositionEntityName)
{
return $this->getNCBSWRepository('Exposition\\' . $expositionEntityName);
}
// moved from Skymark\Utils
public function getRepository($entityName)
{
// error_log("1611 NC_Con - new getRepo($entityName) -- still called??");
// $em = $this->getDoctrine()->getManager();
$em = $this->getManager();
// $em = $this->getContainer()->get('doctrine.orm.entity_manager');
$r = $em->getRepository('NCBSWBundle:' . $entityName);
return $r;
}
public function renderView($view, array $parameters = array(), Response $response = null)
{
// $path = $this->prefix . ':' . $view;
$path = $this->prefix . '/' . $view;
if($response != null)
{
$response->setETag(md5($response->getContent()));
$response->setPublic(); // make sure the response is public/cacheable
// $response->isNotModified($this->getRequest());
}
//s33: cleanup -- except for templateName probably not needed
// error_log("0111 renderView 1 - " . $path);
$gtp = $this->getGeneralTemplateParameters();
// error_log("0111 renderView 2 gtp: " . json_encode($gtp)); // , 0, 2));
// error_log("0111 renderView 2 p: " . json_encode($parameters)); // , 0, 2));
#$r = $this->render("NCBSWBundle:$path.html.twig", array_merge($gtp, $parameters), $response);
// for 3.3:
$templateName = "@NCBSW/$path.html.twig";
// error_log("2134 template name: $templateName");
$r = $this->render($templateName, array_merge($gtp, $parameters), $response);
// error_log("0111 renderView 3 ");
return $r;
}
protected function getGeneralTemplateParameters()
{
$generalParams = array();
$aLocale = $this->container->getParameter('jms_i18n_routing.locales');
$aLang = $this->getLanguageList($aLocale);
/* foreach($this->container->getParameter('jms_i18n_routing.locales') as $localeCode) {
$generalParams['available_locales'][$localeCode] = $this->getRepository('Language')->findOneByShortCode($localeCode);
} */
foreach($aLocale as $localeCode) {
$generalParams['available_locales'][$localeCode] = $aLang[$localeCode];
}
return $generalParams;
}
function getLanguageList($vLocal)
{
$sLocals = "'".implode("','", $vLocal)."'";
$sSql = "select shortcode, (case when nativename is not null and nativename <> '' then nativename else name end) as name FROM language where shortcode in (" . $sLocals . ")";
$aTemp = $this->getNativeQueryResults($sSql);
$aRet = array();
foreach($aTemp as $t)
{
$aRet[$t['shortcode']] = $t['name'];
}
return $aRet;
}
function getNativeQueryResults($sql)
{
$query = $this->getManager()->getConnection()->prepare($sql);
$query->execute();
return $query->fetchAll();
}
function getExplainedStoryList($locale, $nStoryId=null)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (select distinct on (id) * from (SELECT bs.id, case when bsl.name is null then bs.name else bsl.name end as name, url, explanationtitle, commentary_url, work_id, case when bs.language_id = " . $nLanguageID . " then 1 else case when bs.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order, book_id, chapter_order, verse_order from vbiblestoryexplained bs left join (select * from biblestory_language where language_id = " . $nLanguageID . ") as bsl on bs.id = bsl.biblestory_id where ispublic";
if($nStoryId != null)
$sSql .= " and bs.id = " . $nStoryId;
$sSql .= " ) as t order by id, lang_order) as t2 order by book_id, chapter_order, verse_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getWorkListForStory($locale, $nStoryId=null)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (select distinct on (work_id) * from (SELECT bs.id, case when bsl.name is null then bs.name else bsl.name end as name, url, explanationtitle, commentary_url, work_id, case when bs.language_id = " . $nLanguageID . " then 1 else case when bs.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order, book_id, chapter_order, verse_order, bs.ordering as story_order from vbiblestoryexplained bs left join (select * from biblestory_language where language_id = " . $nLanguageID . ") as bsl on bs.id = bsl.biblestory_id where ispublic";
if($nStoryId != null)
$sSql .= " and bs.id = " . $nStoryId;
$sSql .= " ) as t order by work_id, lang_order) as t2 order by story_order, book_id, chapter_order, verse_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
/*
// not used
function getUnexplainedStoryList($locale)
{
$sSql = "SELECT name, url from vbiblestory where";
$sSql .= ' id not in (select id from vbiblestoryexplained)';
$sSql .= " order by book_id, chapter_order, verse_order, name";
return $this->getNativeQueryResults($sSql);
}
*/
function getChapterExplanation($nChapterID, $sBibleTranslationUrl, $request, $vFromSlider=false)
{
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nSelectedExplTransID = $request->query->get('et');
$nBookLanguageID = null;
$sEndStr = "";
$sLangFilter = "";
$sLangOrder = "";
if(!empty($sBibleTranslationUrl))
{
$sSql1 = "select language_id from bibletranslation where url = '" . str_replace("'", "''", $sBibleTranslationUrl) . "'";
//echo $sSql1;
$nBookLanguageID = $this->getSingleData($sSql1);
}
if(!$vFromSlider && !$this->showBibleChapterSummary($nBookLanguageID))
{
return array();
}
if(!empty($nBookLanguageID))
{
if(!empty($nSelectedExplTransID))
{
$sLangOrder .= "case when t.id = " . $nSelectedExplTransID . " then 1 else ";
$sEndStr .= " end ";
}
$sLangOrder .= "case when t.language_id = " . $nBookLanguageID . " then 2";
$sLangFilter .= " and (t.language_id in (" . $nBookLanguageID;
if($nBookLanguageID != $nUILanguageID)
{
$sLangOrder .= " else case when t.language_id = " . $nUILanguageID . " then 3";
$sEndStr .= " end ";
$sLangFilter .= "," . $nUILanguageID;
}
if(!empty($nPreferredLanguageID) && $nBookLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangOrder .= ' else case when t.language_id = ' . $nPreferredLanguageID . ' then 4';
$sEndStr .= " end ";
$sLangFilter .= "," . $nPreferredLanguageID;
}
if($nBookLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangOrder .= " else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 5";
$sEndStr .= " end ";
$sLangFilter .= "," . $this->LANGUAGE_ID_ENGLISH;
}
$sLangOrder .= " else 6";
}
else
{
$sLangFilter .= " and (t.language_id in (";
if(!empty($nSelectedExplTransID))
{
$sLangOrder .= "case when t.id = " . $nSelectedExplTransID . " then 1 else";
$sEndStr .= " end ";
}
$sLangOrder .= " case when t.language_id = " . $nUILanguageID . " then 2";
$sLangFilter .= $nUILanguageID;
if($nUILanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangOrder .= " else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3";
$sEndStr .= " end ";
$sLangFilter .= "," . $this->LANGUAGE_ID_ENGLISH;
}
else
{
$sLangOrder .= " else 3";
}
}
$sLangOrder .= $sEndStr . " end as lang_order";
$sLangFilter .= ")";
if(!empty($nSelectedExplTransID))
{
$sLangFilter .= " or t.id = " . $nSelectedExplTransID;
}
$sLangFilter .= ")";
$sSql = "SELECT bs.chapter_id, t.id as translation_id, t.url, t.is_modern, t.translatedtitle as explanationtitle, " . $sLangOrder
." FROM expositionwork w, expositiontranslation t, expositiontranslationtext tt, story_work sw, biblestory bs"
." WHERE bs.chapter_id = " . $nChapterID . " and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.id = tt.translation_id and tt.text is not null and tt.text <> ''"
. $sLangFilter
." and w.ispublic AND t.ispublic order by lang_order, t.is_modern desc, t.islanguagedefault desc limit 1";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getAuthorInfo($request, $translationId)
{
$sAuthor = "";
$nTransID = "";
// check if it's machine translated
$sSql1 = "select translationmethod_id from expositiontranslation where id = " . $translationId;
$nTransMethodID = $this->getSingleData($sSql1);
$bOriginal = ($nTransMethodID == $this->TRANSLATION_METHOD_ID_NA);
$bMachineTranslated = ($nTransMethodID == $this->TRANSLATION_METHOD_ID_MACHINE);
// get the original translation with the same work ID as the translated one
$sSql2 = "select t2.id from expositiontranslation t1, expositiontranslation t2 where t1.id = " . $translationId . " and t1.work_id = t2.work_id and t2.isoriginal order by case when t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end";
$nTransID = $this->getSingleData($sSql2);
if(!empty($nTransID))
{
$sSql = "SELECT array_to_string(array_agg( (CASE WHEN p.name is not null then p.name else (case when p.title is null then '' else p.title || ' ' end) || (case when p.firstname is null then '' else p.firstname || ' ' end) || (case when p.middlename is null then '' else p.middlename || ' ' end) || (case when p.lastname is null then '' else p.lastname || ' ' end) || (case when p.suffix is null then '' else p.suffix || ' ' end) end) ), ', ') as authors "
." FROM translation_bibliographicdata tbg, bibliographicdata_person bp, person p"
." WHERE tbg.translation_id = " . $nTransID . " and tbg.bibliographicdata_id = bp.bibliographicdata_id and bp.person_id = p.id";
$sAuthor = $this->getSingleData($sSql);
}
if(!$bOriginal)
{
// get the translated language
$sSql3 = "select (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name from expositiontranslation t, language l where t.id = " . $translationId . " and l.id = t.language_id";
$sLang = $this->getSingleData($sSql3);
if(!empty($sLang))
{
if($bMachineTranslated)
{
$sAuthor .= " (" . $request->getSession()->get('ui_str')["generic.machinetranslated"] . " " . $sLang . ")";
}
else
{
$sSql = "SELECT array_to_string(array_agg( (CASE WHEN p.name is not null then p.name else (case when p.title is null then '' else p.title || ' ' end) || (case when p.firstname is null then '' else p.firstname || ' ' end) || (case when p.middlename is null then '' else p.middlename || ' ' end) || (case when p.lastname is null then '' else p.lastname || ' ' end) || (case when p.suffix is null then '' else p.suffix || ' ' end) end) ), ', ') as authors "
." FROM translation_bibliographicdata tbg, bibliographicdata_person bp, person p"
." WHERE tbg.translation_id = " . $translationId . " and tbg.bibliographicdata_id = bp.bibliographicdata_id and bp.person_id = p.id and bp.role_id = " . $this->PERSON_ROLE_ID_TRANSLATOR;
$sTranslator = $this->getSingleData($sSql);
if(empty($sTranslator))
{
$aReplace = array(
'_9600_' => $sLang
);
$sAuthor .= " (" . $this->ui_string_replace($request->getSession()->get('ui_str')["exposition.translation.translated.into"], $aReplace) . ")";
}
else
{
$aReplace = array(
'_9600_' => $sLang,
'_9700_' => $sTranslator
);
$sAuthor .= " (" . $this->ui_string_replace($request->getSession()->get('ui_str')["exposition.translation.translated.intoby"], $aReplace) . ")";
}
}
}
}
return $sAuthor;
}
function checkBibleTranslationMsg($translationUrl)
{
$msgData = array();
if($translationUrl == 'albanian') {
$msgData['no_content_msg'] = "Unfortunately, there's currently no longer a Shqip (Albanian) Bible translation available to us. Wikipedia lists some translations here: <a href='https://en.wikipedia.org/wiki/Bible_translations_into_Albanian'>https://en.wikipedia.org/wiki/Bible_translations_into_Albanian</a>";
}
if($translationUrl == 'romanian-cornilescu') {
$msgData['no_content_msg'] = "Unfortunately, there's currently no longer a Cornilescu (Romanian) Bible translation available to us. Wikipedia lists some translations here: <a href='https://en.wikipedia.org/wiki/Bible_translations_into_Cornilescu'>https://en.wikipedia.org/wiki/Bible_translations_into_Cornilescun</a>";
}
return $msgData;
}
function getTextStyleOverride($languageId)
{
$sSql = "SELECT text_style_override "
." FROM language"
." WHERE id = " . $languageId;
$sRet = '';
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
function getTextStyleOverrideForBible($book_id)
{
$sSql = "SELECT l.text_style_override "
." FROM language l, bibletranslation bt, biblebook b"
." WHERE b.id = " . $book_id . " and b.translation_id = bt.id and l.id = bt.language_id";
$sRet = '';
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
function getNextPassageWithContent($passage,$translation)
{
$sSql = "SELECT p.id, t.url as translation_url, d.url as division_url, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as passage_num, p.ordering as passage_order, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '_' || p.ordering end) as multi_spec "
." FROM expositionpassage p, expositiondivision d, expositiontranslationtext tt, expositiontextunit tu, expositiontranslation t, expositionwork w"
." WHERE d.work_id = " . $passage->getDivision()->getWork()->getId() . " and p.division_id = d.id and p.ordering > " . $passage->getOrdering() . " and tt.translation_id=" . $translation->getId() . " and tt.placement_id = tu.id and tu.passage_id = p.id and t.id = tt.translation_id and d.work_id = w.id order by p.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getPreviousPassageWithContent($passage,$translation)
{
$sSql = "SELECT p.id, t.url as translation_url, d.url as division_url, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as passage_num, p.ordering as passage_order, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '_' || p.ordering end) as multi_spec "
." FROM expositionpassage p, expositiondivision d, expositiontranslationtext tt, expositiontextunit tu, expositiontranslation t, expositionwork w"
." WHERE d.work_id = " . $passage->getDivision()->getWork()->getId() . " and p.division_id = d.id and p.ordering < " . $passage->getOrdering() . " and tt.translation_id=" . $translation->getId() . " and tt.placement_id = tu.id and tu.passage_id = p.id and t.id = tt.translation_id and d.work_id = w.id order by p.ordering desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getCategorizedSwedenborgWorks($request, $language_id=123)
{
return $this->getCategorizedSwedenborgWorksForTranslation($request, null, $language_id);
}
function getCategorizedSwedenborgWorksForTranslation($request, $translation_id, $language_id=123)
{
$sPublished = $request->getSession()->get('ui_str')["exposition.breadcrumbs.publishedworks"];
$sUnpublished = $request->getSession()->get('ui_str')["exposition.breadcrumbs.unpublishedworks"];
$sScientific = $request->getSession()->get('ui_str')["exposition.breadcrumbs.scientificworks"];
$sTransitional = $request->getSession()->get('ui_str')["exposition.breadcrumbs.transitionalworks"];
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as translation_id, t.url as translation_url, t.translatedtitle AS title, (case when w.swedenborgtype = 'published' then '" . str_replace("'", "''", $sPublished) . "' else case when w.swedenborgtype = 'unpublished' then '" . str_replace("'", "''", $sUnpublished) . "' else case when w.swedenborgtype = 'transitional' then '" . str_replace("'", "''", $sTransitional) . "' else case when w.swedenborgtype = 'scientific' then '" . str_replace("'", "''", $sScientific) . "' else 'Unknown' end end end end) as sw_type, case when t.language_id = ".$language_id ." then 1 else 2 end as lang_order, case when t.islanguagedefault then 1 else 2 end as default_order, w.swedenborgtype, wt.ordering as type_order"
." FROM expositionwork w, expositiontranslation t, expositionworktype wt"
." WHERE w.swedenborgtype IS NOT NULL and t.work_id=w.id and w.swedenborgtype = wt.name";
if($language_id == $this->LANGUAGE_ID_ENGLISH)
$sSql .= " and t.language_id = ".$language_id;
else
$sSql .= " and t.language_id in (" . $this->LANGUAGE_ID_ENGLISH . ", ".$language_id.")";
if($translation_id != null)
$sSql .= " and (t.id = " . $translation_id . " or w.id not in (select work_id from expositiontranslation where id = " . $translation_id . "))";
$bShowScientific = $this->showScientific($request);
if(!$bShowScientific)
$sSql .= " and w.swedenborgtype <> 'scientific'";
$sSql .= " and w.ispublic AND t.ispublic and t.url is not null and t.url <> '' order by w.id, lang_order, default_order) as w2 order by w2.type_order, w2.lang_order, w2.title";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getWorkPassageRefs($passage_id,$translation_id, $request, $bCheck=false)
{
$language_id = $this->getLanguageIDByTranslation($translation_id);
$bLangStr = ($language_id != null && $language_id != $this->LANGUAGE_ID_ENGLISH);
$sSql1 = "select array_to_string(array_agg(DISTINCT d.ref_id),',') as ref from expositiontextunit t, expositiontranslationtext tt, expositiondisplayitem d where t.passage_id = " . $passage_id . " and tt.translation_id = " . $translation_id
." and t.id = tt.placement_id and d.container_id = tt.id and d.type_id = " . $this->NC_ITEM_TYPE_ID_HYPERLINK;
$aHyperLink = $this->getNativeQueryResults($sSql1);
$sHyperLinkFilter = '';
$sHyperLinkIDs = '';
if(count($aHyperLink) > 0)
{
$sHyperLinkIDs = implode('', $aHyperLink[0]);
}
if(!empty($sHyperLinkIDs))
$sHyperLinkFilter = ' and rc.id not in (' . $sHyperLinkIDs . ')';
$sBibleStr = '(';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
$sBibleStr .= 'case when trans.id = ' . $nBibleTransID . ' then 1 else ';
if($bLangStr)
$sBibleStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sBibleStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.languagedefault then 3 else 4 end end';
if($nBibleTransID != '')
$sBibleStr .= ' end';
if($bLangStr)
$sBibleStr .= ' end';
$sBibleStr .= ')';
$sWorkStr = '(case when trans.id = ' . $translation_id . ' then 1 else ';
if($bLangStr)
$sWorkStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sWorkStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.islanguagedefault then 3 else 4 end end end';
if($bLangStr)
$sWorkStr .= ' end';
$sWorkStr .= ')';
$sSql = "select t3.* from "
." (select * from (SELECT distinct on (tc.textunit_id, rc.id) tc.textunit_id, rc.id as ref_id, rc.originallinktext as link_text, cc1.book_id, rb.startverse_id as startloc_id, rb.endverse_id as endloc_id, true as is_bibleref, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc1.ordering || (case when cc1.id <> cc2.id then '-' || cc2.ordering else '' end) || '_' || cv1.ordering || (case when rb.startverse_id <> rb.endverse_id then '-' || cv2.ordering else '' end) as multilink_text, cc1.ordering as ordering1, cv1.ordering as ordering2, " . $sBibleStr . " as priority_order, cb.url as book_url, trans.url as trans_url, cc1.ordering as chapter_order1, cc2.ordering as chapter_order2, cv1.ordering as verse_order1, cv2.ordering as verse_order2, trans.url as div_url, 0 as passage_order"
." FROM refbible rb, refcombined rc, textunit_combinedreference tc, expositiontextunit t, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbiblebook cb, canonicalbibleverse_bibleverse cbb1, bibleverse v1, biblechapter c1, canonicalbibleverse cv2, canonicalbiblechapter cc2, biblebook b, bibletranslation trans"
." WHERE t.passage_id = " . $passage_id . " and t.id = tc.textunit_id and rc.id = tc.combinedreference_id " . $sHyperLinkFilter . " and rb.container_id = rc.id and cv1.id = rb.startverse_id and cv1.chapter_id = cc1.id and cc1.book_id = cb.id and cv1.id = cbb1.canonicalbibleverse_id and v1.id = cbb1.bibleverse_id and v1.chapter_id = c1.id and c1.book_id = b.id and cv2.id = rb.endverse_id and cv2.chapter_id = cc2.id and b.translation_id = trans.id and trans.enabled order by tc.textunit_id, rc.id, priority_order asc, trans.languagedefault desc) as b"
." union"
." select * from (SELECT distinct on (tc.textunit_id, rc.id) tc.textunit_id, rc.id as ref_id, rc.originallinktext as link_text, d.work_id as book_id, re.startlocation_id as startloc_id, re.endlocation_id as endloc_id, false as is_bibleref, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "_' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' end) || trans.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as multilink_text, p.ordering as ordering1, t2.ordering as ordering2, " . $sWorkStr . " as priority_order, trans.url as book_url, trans.url as trans_url, 0 as chapter_order1, 0 as chapter_order2, 0 as verse_order1, 0 as verse_order2, d.url as div_url, p.ordering as passage_order"
." FROM refexposition re, refcombined rc, textunit_combinedreference tc, expositiontextunit t, expositiontextunit t2, expositionpassage p, expositiondivision d, expositionwork w, expositiontranslation trans, expositiontranslationtext tt"
." WHERE t.passage_id = " . $passage_id . " and t.id = tc.textunit_id and rc.id = tc.combinedreference_id " . $sHyperLinkFilter . " and re.container_id = rc.id and t2.id = re.startlocation_id and p.id = t2.passage_id and d.id = p.division_id and d.work_id = w.id and w.id = trans.work_id and trans.url is not null and trans.ispublic and w.ispublic and t2.id = tt.placement_id and trans.id = tt.translation_id order by tc.textunit_id, rc.id, priority_order asc, trans.islanguagedefault desc, trans.isoriginal desc) as e";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") as t3 order by split_part(link_text, ' ', 1), book_id, ordering1, ordering2";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$aTemp = $aRet;
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
if($i > 0 && $aRet[$i]["textunit_id"] == $aRet[$i-1]["textunit_id"] && $aRet[$i]["book_id"] == $aRet[$i-1]["book_id"] && $aRet[$i]["is_bibleref"] == $aRet[$i-1]["is_bibleref"])
{
$sLink = $aRet[$i]["link_text"];
$sPrevLink = $aTemp[$i-1]["link_text"];
$aRet[$i]["link_text"] = $this->removeRepeatStrForRef($sLink, $sPrevLink);
}
else
{
if(!empty($language_id) && $language_id != $this->LANGUAGE_ID_ENGLISH)
{
// check book names in other languages
if($aRet[$i]["is_bibleref"])
$aRet[$i]["link_text"] = $this->getBibleBookName($aRet[$i]["link_text"], $aRet[$i]["book_id"], $language_id);
else
$aRet[$i]["link_text"] = $this->getWorkBookName($aRet[$i]["link_text"], $aRet[$i]["book_id"], $language_id);
}
}
$aRet[$i]["link_text"] = str_replace('_', ' ', $aRet[$i]["link_text"]);
$aRet[$i]["singlelink"] = '';
if($aRet[$i]["is_bibleref"])
{
if($aTemp[$i]["chapter_order1"] != $aTemp[$i]["chapter_order2"])
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_chapter_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterStartIndex' => $aTemp[$i]["chapter_order1"],
'chapterEndIndex' => $aTemp[$i]["chapter_order2"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else
{
if($aTemp[$i]["verse_order1"] != $aTemp[$i]["verse_order2"])
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseIndex' => $aTemp[$i]["verse_order1"]
));
}
}
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('exposition_translation_division_passage', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'divisionUrls' => $aTemp[$i]["div_url"],
'passageNumber' => $aTemp[$i]["passage_order"]
));
}
}
// print_r($aRet);
return $aRet;
// For this task: Test turning off the code that looks for and displays the missing links under
// swedenborg sections. See if we get a speed improvement.
// comment out the above block in this function and add:
// Task: Don't show the textunit refs for all works. (8/4/2021)
// Don't show the textunit refs for all swedenborg works. Show them for explanations.
// return array();
}
public function removeRepeatStrForRef($vLink, $vPrevLink)
{
$sRet = $vLink;
$aLink = explode(' ',$vLink);
$nLinkNum = count($aLink);
$aPrevLink = explode(' ',$vPrevLink);
$nPrevLinkNum = count($aPrevLink);
$nDiffStr = -1;
if($nPrevLinkNum <= $nLinkNum)
{
for($i=0;$i<$nLinkNum;$i++)
{
if($i > $nPrevLinkNum-1)
break;
$sPrevLinkStr = trim($aPrevLink[$i]);
$sLinkStr = trim($aLink[$i]);
$sFirstLinkChar = substr($sLinkStr, 0, 1);
if($sPrevLinkStr != $sLinkStr && (is_numeric($sFirstLinkChar) || $sFirstLinkChar == '['))
{
$nDiffStr = $i;
break;
}
}
}
else
{
for($i=0;$i<$nPrevLinkNum;$i++)
{
if($i > $nLinkNum-1)
break;
$sPrevLinkStr = trim($aPrevLink[$i]);
$sLinkStr = trim($aLink[$i]);
$sFirstLinkChar = substr($sLinkStr, 0, 1);
if($sPrevLinkStr != $sLinkStr && (is_numeric($sFirstLinkChar) || $sFirstLinkChar == '['))
{
$nDiffStr = $i;
break;
}
}
}
if($nDiffStr > 0)
{
$sRet = '';
for($i=$nDiffStr;$i<$nLinkNum;$i++)
{
if($sRet != '')
$sRet .= ' ';
$sRet .= $aLink[$i];
}
}
return $sRet;
}
public function getRefName($vLink)
{
$sRet = '';
$sTempLink = str_replace(" [", "[", $vLink);
$nPos = strrpos($sTempLink, ' ');
if($nPos > -1)
{
$sRet = substr($sTempLink, 0, $nPos);
}
return $sRet;
}
public function removeRefName($vLink, $vName)
{
$sRet = trim(str_replace($vName, '', $vLink));
return $sRet;
}
function getWorkPassageMultiLink($passage_id,$translation_id, $request)
{
$aRet = array();
$sBibleStr = '';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
{
$sBibleStr = '(case when trans.id = ' . $nBibleTransID . ' then 1 else case when trans.language_id in (select language_id from expositiontranslation where id = ' . $translation_id . ') then 2 else case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 3 else 4 end end end)';
}
else
{
$sBibleStr = '(case when trans.language_id in (select language_id from expositiontranslation where id = ' . $translation_id . ') then 1 else case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else 3 end end )';
}
// section_order is used for exposition references only
$sSql = "select * from (SELECT distinct on (di.ref_id) di.ref_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc1.ordering || (case when cc1.id <> cc2.id then '-' || cc2.ordering else '' end) || '_' || cv1.ordering || (case when rb.startverse_id <> rb.endverse_id then '-' || cv2.ordering else '' end) as multilink, true as is_bibleref, 1 as section_order, " . $sBibleStr . " as priority_order, cb.url as book_url, trans.url as trans_url, cc1.ordering as chapter_order1, cc2.ordering as chapter_order2, cv1.ordering as verse_order1, cv2.ordering as verse_order2, trans.url as div_url, 0 as passage_order"
." FROM refbible rb, expositiontextunit t, expositiontranslationtext tt, expositiondisplayitem di, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbiblebook cb, canonicalbibleverse cv2, canonicalbiblechapter cc2, biblebook b, bibletranslation trans "
." WHERE t.passage_id = " . $passage_id . " and t.id = tt.placement_id and rb.container_id = di.ref_id and di.container_id = tt.id and di.ref_id is not null and cv1.id = rb.startverse_id and cv1.chapter_id = cc1.id and cc1.book_id = cb.id and cv2.id = rb.endverse_id and cv2.chapter_id = cc2.id and b.canonicalization_id = cb.id and b.translation_id = trans.id and trans.enabled order by di.ref_id, priority_order asc, trans.languagedefault desc) as b"
." union"
." select * from (SELECT distinct on (di.ref_id) di.ref_id, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || trans.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as multilink, false as is_bibleref, t2.ordering as section_order, (case when trans.id = " . $translation_id . " then 1 else case when trans.language_id in (select language_id from expositiontranslation where id = " . $translation_id . ") then 2 else case when trans.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 else 4 end end end) as priority_order, trans.url as book_url, trans.url as trans_url, 0 as chapter_order1, 0 as chapter_order2, 0 as verse_order1, 0 as verse_order2, d.url as div_url, p.ordering as passage_order"
." FROM refexposition re, expositiontextunit t, expositiontextunit t2, expositionpassage p, expositiondivision d, expositiontranslationtext tt, expositiondisplayitem di, expositionwork w, expositiontranslation trans"
." WHERE t.passage_id = " . $passage_id . " and t2.id = re.startlocation_id and p.id = t2.passage_id and d.id = p.division_id and re.container_id = di.ref_id and t.id = tt.placement_id and di.container_id = tt.id and di.ref_id is not null and d.work_id = w.id and w.id = trans.work_id and trans.url is not null and trans.ispublic and w.ispublic order by di.ref_id, priority_order asc, trans.islanguagedefault desc, trans.isoriginal desc) as e";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
$nLen = count($aTemp);
for($i=0;$i<$nLen;$i++)
{
$aRef = array();
$aRef["multilink"] = $aTemp[$i]["multilink"];
$aRef["is_bibleref"] = $aTemp[$i]["is_bibleref"];
$aRef["singlelink"] = '';
if($aRef["is_bibleref"])
{
if($aTemp[$i]["chapter_order1"] != $aTemp[$i]["chapter_order2"])
{
$aRef["singlelink"] = $this->get('router')->generate('bible_chapter_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterStartIndex' => $aTemp[$i]["chapter_order1"],
'chapterEndIndex' => $aTemp[$i]["chapter_order2"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else
{
if($aTemp[$i]["verse_order1"] != $aTemp[$i]["verse_order2"])
{
$aRef["singlelink"] = $this->get('router')->generate('bible_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else
{
$aRef["singlelink"] = $this->get('router')->generate('bible_verse', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseIndex' => $aTemp[$i]["verse_order1"]
));
}
}
}
else
{
$aRef["singlelink"] = $this->get('router')->generate('exposition_translation_division_passage', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'divisionUrls' => $aTemp[$i]["div_url"],
'passageNumber' => $aTemp[$i]["passage_order"]
));
}
$aRef["section_order"] = $aTemp[$i]["section_order"];
$aRet[$aTemp[$i]["ref_id"]] = $aRef;
}
return $aRet;
}
function getBibleTranslationsByLanguage($canonical_book_id=null, $language_id=null)
{
$sGroupOrder = $this->getGroupOrderForBibleTranslation($language_id);
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, bk.url as first_book_url, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, " . $sGroupOrder . " as group_order, true as has_book, true as has_chapter"
." FROM language l, bibletranslation t"
." join ("
." select distinct on (t2.id) t2.id as translation_id, cb.url"
." from bibletranslation t2, biblebook b, canonicalbiblebook cb where b.translation_id = t2.id and cb.id = b.canonicalization_id"
." order by t2.id, b.ordering"
.") as bk on t.id = bk.translation_id";
$sSql .= " WHERE t.enabled and t.language_id = l.id";
if($canonical_book_id != null)
{
$sSql .= " and t.id in (select distinct translation_id from biblebook where canonicalization_id = " . $canonical_book_id . ")";
}
$sSql .= " order by group_order, language_name, t.name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleRefPassage($aVerseInfo, $bChapter, $bCorePassage=false, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$canon = array();
$nulls = array();
$verseLanguage = null;
$sVerseIDs = $aVerseInfo['CVerseIDs'];
$verseLanguage = $aVerseInfo['LanguageID'];
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
$sCChapterIDs = '';
if($bChapter)
$sCChapterIDs = $aVerseInfo['CChapterIDs'];
if($sVerseIDs == '' && $sCChapterIDs == '') {
return null;
} else {
if($sVerseIDs != '')
{
$tempChapterIDs = $this->checkWholeChapters($sVerseIDs);
if($tempChapterIDs != null)
{
if($sCChapterIDs != '')
$sCChapterIDs .= ',';
$sCChapterIDs .= $tempChapterIDs;
}
}
return $this->getBibleRefPassageInfo($verseLanguage, $sVerseIDs, $sCChapterIDs, $bCorePassage, $bCheck);
}
}
function checkWholeChapters($sVerseIDs)
{
if($sVerseIDs == '')
return '';
$sAllChapterIDs = '';
$sSql = "select array_to_string(array_agg(distinct chapter_id), ',') from canonicalbibleverse where id in (" . $sVerseIDs . ")";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$sAllChapterIDs = implode(",", $aTemp[0]);
}
$sSql = "select array_to_string(array_agg(id), ',') from canonicalbiblechapter where id in (" . $sAllChapterIDs . ") and id not in (select distinct chapter_id from canonicalbibleverse where chapter_id in (" . $sAllChapterIDs . ") and id not in (" . $sVerseIDs . "))";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
$sRet = '';
if(count($aTemp) > 0)
{
$sRet = implode(",", $aTemp[0]);
}
return $sRet;
}
function getBibleRefPassageInfo($language_id, $sVerseIDs, $sChapterIDs, $bCorePassage, $bCheck)
{
if($sVerseIDs == '' && $sChapterIDs == '')
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = " p.id";
if(!$bCorePassage)
{
$sFilter .= " in (";
if ($sVerseIDs <> '')
{
$sFilter .= 'SELECT DISTINCT passage_id FROM verse_passage'
.' WHERE verse_id in (' . $sVerseIDs . ')';
}
$sFilter .= ") and p.id not ";
}
$sFilter .= " in (";
if ($sChapterIDs <> '')
{
$sFilter .= 'SELECT DISTINCT passage_id FROM chapter_passage'
.' WHERE chapter_id in (' . $sChapterIDs . ')';
}
if ($sVerseIDs <> '')
{
if ($sChapterIDs <> '')
$sFilter .= " union ";
$sFilter .= 'SELECT DISTINCT passage_id FROM verse_passage'
.' WHERE verse_id in (' . $sVerseIDs . ')';
$sFilter .= ' and iscorepassage';
}
$sFilter .= ")";
$sSql = "select * from (SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, p.swedenborgsection as num, '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as ref_column_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and w.swedenborgtype is not null and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, t.islanguagedefault desc";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") order by p.id, lang_order";
$sSql .= ") as t2 order by work_order, title, ordering";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getVerseConcept($verses, $language_id)
{
$aRet = array();
$aTemp = array();
if($verses != null && !empty($verses))
{
// $language_id = null;
$sVerseIDs = '';
foreach($verses as $verse)
{
if($sVerseIDs != '')
$sVerseIDs .= ',';
$sVerseIDs .= $verse->getId();
/* if($language_id == null)
$language_id = $verse->findMainVerse()->getChapter()->getBook()->getTranslation()->getLanguage()->getId(); */
}
if($sVerseIDs != '' && $language_id != null)
{
$sBorderStr = $this->getBoundaryStrForSql($language_id);
$sSql = "select * from (SELECT distinct on (verse_id, term_text) * from (select cbb.bibleverse_id as verse_id, t.id as term_id, lower(t.text) as term_text, c.url as concept_url, char_length(t.text) as text_len, ct.translatedtitle as title, ct.explanation_id, c.id as concept_id"
." FROM verse_concept vc, term_concept tc, term t, concept c, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cbb, bibleverse bv,";
if($language_id == $this->LANGUAGE_ID_ENGLISH)
{
$sSql .= " (select distinct on (cl.concept_id) cl.concept_id, trans.translatedtitle, trans.id as explanation_id from concept_work cl, expositiontranslation trans where trans.language_id = " . $language_id . " and cl.work_id = trans.work_id) as ct ";
}
else
{
$sSql .= " (select distinct on (cl.concept_id) cl.concept_id, trans.translatedtitle, trans.id as explanation_id, case when trans.language_id = " . $language_id . " then 1 else 2 end as ordering from concept_work cl, expositiontranslation trans where trans.language_id in (" . $language_id . ", " . $this->LANGUAGE_ID_ENGLISH . ") and cl.work_id = trans.work_id order by cl.concept_id, ordering) as ct ";
}
$sSql .= " WHERE cbb.canonicalbibleverse_id = vc.verse_id and tc.concept_id = vc.concept_id and t.id = tc.term_id and c.id = tc.concept_id and t.language_id = " . $language_id . " and ct.concept_id = c.id";
$sSql .= " and cbb.bibleverse_id = bv.id and case when t.text is not null and (bv.content ~* ('" . $sBorderStr . "' || " . $this->EscapeTextForRegex("t.text") . " || '" . $sBorderStr . "')) then true else false end order by cbb.bibleverse_id, lower(t.text), term_id) as t1) as t order by verse_id, char_length(term_text) desc";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
}
}
foreach($aTemp as $temp)
{
$aRet[$temp['verse_id']][] = $temp;
}
return $aRet;
}
/*
* Finds concept references in the text, and insert concept links to the text.
* Returns the verse text with concept links.
*/
public function getVerseContentWithConceptLinks($request, $verses, $bibleColumnSpec, $column_number=0)
{
$bOnMobile = $this->isOnMobile();
$sConceptMark = "qmrgyx";
$sHyphenMark = "xhfvkw";
$sSpaceMark = "zszkrx";
$sConceptLinkMark = "2";
$language_code = null;
$language_id = null;
foreach($verses as $verse)
{
if($language_code == null)
{
/* $lang = $verse->findMainVerse()->getChapter()->getBook()->getTranslation()->getLanguage();
$language_code = $lang->getBibliographicCode();
$language_id = $lang->getId(); */
$aLang = $this->getVerseData($verse->getId());
$language_code = $aLang['bibliographiccode'];
$language_id = $aLang['language_id'];
break;
}
}
// For Malayalam and Tagalog, page loads tend to be slow, perhaps because of bandwidth.
// We could try turning off commentary and word highlighting
// We can set it on the language list on the admin site
if(!$this->showBibleHighlights($language_id))
{
return array();
}
$bShowBibleLinks = true;
$nShowBibleLinks = $request->getSession()->get('show_bible_links');
if($nShowBibleLinks == 2)
$bShowBibleLinks = false;
if(!$bShowBibleLinks)
{
return array();
}
$conceptPopupLink = $this->get('router')->generate('popover_concept', array(
'explanationID' => $sConceptLinkMark,
'conceptID' => $sConceptLinkMark
));
$conceptPopupLink = rtrim($conceptPopupLink, $sConceptLinkMark."/".$sConceptLinkMark)."/";
/* $conceptLink = $this->get('router')->generate('concept', array(
'conceptUrl' => $sConceptLinkMark
)); */
$conceptOverlayLink = $this->get('router')->generate('overlay_concept', array(
'conceptUrl' => $sConceptLinkMark
));
// $conceptLink = trim($conceptLink, $sConceptLinkMark);
$conceptOverlayLink = rtrim($conceptOverlayLink, $sConceptLinkMark);
// The user requests that we will show modals on mobile devices now (10/19/2020)
// if(!$bOnMobile)
if(true)
{
$sLanguageStr = '';
if($language_code != 'eng')
$sLanguageStr = '/'.$language_code;
$sBibleStr = '';
if(!empty($bibleColumnSpec))
$sBibleStr = '?b='.$bibleColumnSpec;
}
// Some languages have no space between words
$sBorderStr = $this->getBoundaryStrForPhp($language_id);
if($bOnMobile)
{
// don't show popover on mobiles
$sLink1a = "<a ";
// if(!$bShowBibleLinks)
// $sLink1a .= " style='display:none' ";
$sLinkMark1a = "lll1a";
$sLink1 = " href='javascript:sm(\"";
$sLinkMark1 = "lll1";
// $sLink2 = "','" . $conceptLink;
// $sLinkMark2 = "lll2";
$sLink3 = "\",\"" . $conceptOverlayLink;
$sLinkMark3 = "lll3";
$sLink4 = "\")' >";
}
else
{
$sLink1a = "<a ";
// if(!$bShowBibleLinks)
// $sLink1a .= " style='display:none' ";
$sLink1a .= " data-poload='" . $conceptPopupLink;
$sLinkMark1a = "lll1a";
$sLink1 = "' href='javascript:sm(\"";
$sLinkMark1 = "lll1";
// $sLink2 = "','" . $conceptLink;
// $sLinkMark2 = "lll2";
$sLink3 = "\",\"" . $conceptOverlayLink;
$sLinkMark3 = "lll3";
$sLink4 = "\")' data-placement='auto' >";
}
$sLinkMark4 = "lll4";
$sLink5 = "</a>";
/* $sLink5 = "</a><span ";
if($bShowBibleLinks)
$sLink5 .= " style='display:none'";
$sLink5 .= " >"; */
$sLinkMark5 = "lll5";
// $sLink6 = "</span>";
// $sLinkMark6 = "lll6";
$sTermIDMark = "tt";
$sPopupStartMark = "~ppss~";
$sPopupEndMark = "~ppee~";
$aRet = array();
$aTerm = $this->getVerseConcept($verses, $language_id);
$nLen = count($aTerm);
foreach($verses as $verse)
{
$aTermFromText = array();
$aTermTitle = array();
$text = $verse->getContent();
// replace popup code
preg_match_all('/'.$sPopupStartMark.'[^~]+'.$sPopupEndMark.'/ui', $text, $matches1);
// print_r($matches1[0]);
$aPopup = $matches1[0];
if(!empty($aPopup))
{
$nPopupCount = 1;
foreach($aPopup as $popup)
{
$text = str_replace($popup, $sPopupStartMark.$nPopupCount.$sPopupEndMark, $text);
$nPopupCount++;
}
}
$verse_id = $verse->getId();
if($nLen > 0 && array_key_exists($verse_id, $aTerm) && !empty($aTerm[$verse_id]))
{
$aTermForV = $aTerm[$verse_id];
foreach ($aTermForV as $term)
{
$sTempText = $term['term_text'];
preg_match_all('/'.$sBorderStr.''.preg_quote($sTempText, '/').$sBorderStr.'/ui', $text, $matches2);
$aText2 = array_unique($matches2[0]);
// print_r($aText2);
$nCount2=0;
foreach ($aText2 as &$sSearch)
{
$sTempKey = $term['term_id'].'@'.$nCount2;
$aTermFromText[$sTempKey] = $sSearch;
$aTermTitle[$sTempKey] = $term['title'];
// str_replace($sSearch,$sTermIDMark.$sTempKey.$sTermIDMark, $text);
$pattern = '/'.$sBorderStr.''.preg_quote($sSearch, '/').$sBorderStr.'/ui';
$text = preg_replace($pattern, $sTermIDMark.$sTempKey.$sTermIDMark, $text);
$nCount2++;
}
}
foreach($aTermForV as $term)
{
//if($bOnMobile)
if(false)
{
$sTermID = $sConceptMark.$term['term_id'].$sConceptMark;
$sNewText = $sLinkMark1 . $sTermID . "," . $column_number . $sLinkMark4 . "$1" . $sLinkMark5;
$text = preg_replace('/'.$sBorderStr.'('.$sTermIDMark.$term['term_id']."@\d+".$sTermIDMark.')'.$sBorderStr.'/ui',$sNewText, $text);
}
else
{
$sConceptUrl = $sConceptMark.$term['concept_url'].$sConceptMark;
$sConceptUrl = str_replace('-', $sHyphenMark, $sConceptUrl);
$nExplanationID = $term['explanation_id'];
$conceptID = $term['concept_id'];
if($bOnMobile)
$sNewText = $sLinkMark1a . $sLinkMark1 . "$1" . $sLinkMark3 . $sConceptUrl . $sLanguageStr . $sBibleStr . $sLinkMark4 . "$1" . $sLinkMark5;
else
$sNewText = $sLinkMark1a . $nExplanationID . "/" . $conceptID . $sLinkMark1 . "$1" . $sLinkMark3 . $sConceptUrl . $sLanguageStr . $sBibleStr . $sLinkMark4 . "$1" . $sLinkMark5;
$text = preg_replace('/'.'('.$sTermIDMark.$term['term_id']."@\d+".$sTermIDMark.')'.'/i',$sNewText, $text);
}
}
$text = str_replace($sConceptMark,'', $text);
$text = str_replace($sHyphenMark,'-', $text);
$text = str_replace($sSpaceMark,' ', $text);
// if(!$bOnMobile)
$text = str_replace($sLinkMark1a, $sLink1a, $text);
$text = str_replace($sLinkMark1, $sLink1, $text);
// $text = str_replace($sLinkMark2, $sLink2, $text);
// if(!$bOnMobile)
// {
foreach ($aTermTitle as $key3 =>$term3)
{
$term3 = str_replace("'", "~q~",$term3);
$text = str_replace($sTermIDMark.$key3.$sTermIDMark.$sLinkMark3,$term3.$sLinkMark3, $text);
}
// }
foreach ($aTermFromText as $key3 =>$term3)
{
//$term3 = str_replace("'", "\'",$term3);
$text = str_replace($sTermIDMark.$key3.$sTermIDMark,$term3, $text);
}
// if(!$bOnMobile)
$text = str_replace($sLinkMark3, $sLink3, $text);
$text = str_replace($sLinkMark4, $sLink4, $text);
$text = str_replace($sLinkMark5, $sLink5, $text);
// $text = str_replace($sLinkMark6, $sLink6, $text);
}
if(!empty($aPopup))
{
$nPopupCount = 1;
foreach($aPopup as $popup)
{
$text = str_replace($sPopupStartMark.$nPopupCount.$sPopupEndMark, $popup, $text);
$nPopupCount++;
}
}
// $text = str_replace('"', """, $text);
$text = str_replace($sPopupStartMark, '<sup><a data-toggle="popover" style="cursor: pointer; color:#007bff;"data-content="', $text);
$text = str_replace($sPopupEndMark, '">*</a></sup>', $text);
$aRet[$verse_id] = $text;
}
return $aRet;
}
function getBibleTranslationInfo($translation_url, $book_url)
{
$sSql = "SELECT max(c.ordering) as last_chapter_order, t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $translation_url)) . "' and t.enabled and t.language_id = l.id and b.translation_id = t.id and cb.url = '" . trim(str_replace("'", "''", $book_url)) . "' "
." and c.book_id = b.id and b.canonicalization_id = cb.id group by t.id, b.id, cb.id, l.id";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getBibleBookList($request, $translation_id1, $translation_id2=null, $translation_id3=null)
{
$sOldTestament = $request->getSession()->get('ui_str')["bible.testament.oldtestament"];
$sNewTestament = $request->getSession()->get('ui_str')["bible.testament.newtestament"];
$sApocrypha = $request->getSession()->get('ui_str')["bible.testament.apocrypha"];
$sSql = "SELECT b.id, b.name, cb.id as canonical_id, cb.url as canonical_book_url, case when cb.testament_id = " . $this->TESTAMENT_OLDTESTAMENT . " then '" . str_replace("'", "''", $sOldTestament) . "' else case when cb.testament_id = " . $this->TESTAMENT_NEWTESTAMENT . " then '" . str_replace("'", "''", $sNewTestament) . "' else case when cb.testament_id = " . $this->TESTAMENT_APOCRYPHA . " then '" . str_replace("'", "''", $sApocrypha) . "' end end end as testament_name, t.url as translation_url, min(c.ordering) as first_chapter_order, cb.testament_id"
." FROM biblebook b, biblechapter c, canonicalbiblebook cb, bibletranslation t"
." WHERE b.translation_id = " . $translation_id1
." and b.canonicalization_id = cb.id"
." and t.id = b.translation_id and c.book_id = b.id";
if($translation_id2 != null)
{
$sSql .= " and cb.id in (SELECT cb.id FROM biblebook b, canonicalbiblebook cb"
." WHERE b.translation_id = " . $translation_id2 . " and b.canonicalization_id = cb.id)";
}
if($translation_id3 != null)
{
$sSql .= " and cb.id in (SELECT cb.id FROM biblebook b, canonicalbiblebook cb"
." WHERE b.translation_id = " . $translation_id3 . " and b.canonicalization_id = cb.id)";
}
$sSql .= " group by b.id, b.name, cb.id, cb.url, testament_name, t.url";
$sSql .= " order by cb.testament_id, cb.id";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getCanonicalBook($bookUrl)
{
$sSql = "SELECT cb.id, cb.url, array_to_string(array_agg(cc.ordering),',') as chapters"
." FROM canonicalbiblebook cb, canonicalbiblechapter cc"
." WHERE cb.url = '" . trim(str_replace("'", "''", $book_url)) . "' limit 1";
// echo $sSql;
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getStoriesForVerses($aVerseInfo, $request, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$aRet = array();
$sVerseIDs = $aVerseInfo['VerseIDs'];
if($sVerseIDs != '')
{
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nBookLanguageID = $aVerseInfo['LanguageID'];
$sLangOrder = "case when t.language_id = " . $nBookLanguageID . " then 1";
$sLangFilter = " and t.language_id in (" . $nBookLanguageID;
$sEndStr = "";
if($nBookLanguageID != $nUILanguageID)
{
$sLangOrder .= " else case when t.language_id = " . $nUILanguageID . " then 2";
$sEndStr .= " end ";
$sLangFilter .= "," . $nUILanguageID;
}
if(!empty($nPreferredLanguageID) && $nBookLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangOrder .= ' else case when t.language_id = ' . $nPreferredLanguageID . ' then 3';
$sEndStr .= " end ";
$sLangFilter .= "," . $nPreferredLanguageID;
}
if($nBookLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangOrder .= " else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 4";
$sEndStr .= " end ";
$sLangFilter .= "," . $this->LANGUAGE_ID_ENGLISH;
}
$sLangOrder .= $sEndStr . " end as lang_order";
$sLangFilter .= ")";
$sSql = "select * from (SELECT distinct on (bs.id) bs.id, bs.url, case when bsl.name is null then bs.name else bsl.name end as name, cv.ordering, t.url as explanation_url, bt.url as bible_translation_url, " . $sLangOrder
." FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs, canonicalbibleverse cv, bibleverse v, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cvv,"
." canonicalbiblechapter scc1, canonicalbiblechapter scc2, canonicalbiblechapter vcc,"
." canonicalbibleverse scv1, canonicalbibleverse scv2, expositiontranslation t, expositionwork w, bibletranslation bt, biblechapter c, biblebook b, story_work sw "
." left join (select * from biblestory_language where language_id = " . $nBookLanguageID . ") as bsl on sw.story_id = bsl.biblestory_id"
." WHERE t.ispublic and w.ispublic and sw.work_id = w.id". $sLangFilter
." and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id"
." and scv1.id = bs.startverse_id and scv2.id = bs.endverse_id"
." and scv1.chapter_id = scc1.id and scv2.chapter_id = scc2.id"
." and vcc.id = cv.chapter_id and v.chapter_id = c.id and c.book_id = b.id and b.translation_id = bt.id"
." and (bs.startverse_id = cv.id or bs.endverse_id = cv.id"
." or (scc1.id = scc2.id and scc1.id = vcc.id and cv.ordering > scv1.ordering and cv.ordering < scv2.ordering)"
." or (scc1.book_id = scc2.book_id and scc1.book_id = vcc.book_id and scc1.ordering < scc2.ordering and vcc.ordering < scc1.ordering and vcc.ordering > scc2.ordering)"
." or (scc1.book_id = scc2.book_id and scc1.book_id = vcc.book_id and scc1.ordering < scc2.ordering and vcc.ordering = scc1.ordering and cv.ordering > scv1.ordering)"
." or (scc1.book_id = scc2.book_id and scc1.book_id = vcc.book_id and scc1.ordering < scc2.ordering and vcc.ordering = scc2.ordering and cv.ordering < scv2.ordering)"
.") and bs.id = sw.story_id and sw.work_id = t.work_id order by bs.id, lang_order) as t1"
." order by ordering, name";
if($bCheck)
$sSql .= " limit 1";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getFirstExplainedVerse($aVerseInfo, $request)
{
if(empty($aVerseInfo))
return array();
$aRet = array();
$sVerseIDs = $aVerseInfo['VerseIDs'];
if($sVerseIDs != '')
{
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nBookLanguageID = $aVerseInfo['LanguageID'];
$sLangOrder = "case when t.language_id = " . $nBookLanguageID . " then 1";
$sLangFilter = " and t.language_id in (" . $nBookLanguageID;
$sEndStr = "";
if($nBookLanguageID != $nUILanguageID)
{
$sLangOrder .= " else case when t.language_id = " . $nUILanguageID . " then 2";
$sEndStr .= " end ";
$sLangFilter .= "," . $nUILanguageID;
}
if(!empty($nPreferredLanguageID) && $nBookLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangOrder .= ' else case when t.language_id = ' . $nPreferredLanguageID . ' then 3';
$sEndStr .= " end ";
$sLangFilter .= "," . $nPreferredLanguageID;
}
if($nBookLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangOrder .= " else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 4";
$sEndStr .= " end ";
$sLangFilter .= "," . $this->LANGUAGE_ID_ENGLISH;
}
$sLangOrder .= $sEndStr . " end as lang_order";
$sLangFilter .= ")";
$sSql = "SELECT v.id, cv.ordering as verse_order, scc.ordering as chapter_order, t.id as translation_id, scc.id as cchapter_id, v.chapter_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || bt.url || '_' || cb.url || '_' || c.ordering || '_' || v.indexdisplay as bible_spec, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, bt.url as bible_trans_url, cb.url as book_url, v.indexdisplay as verse_index, " . $sLangOrder . ", bt.language_id"
." FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_VERSE . ") as bs, canonicalbibleverse cv, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cvv, "
." canonicalbiblechapter scc, expositionwork w, expositiontranslation t, expositiontranslationtext tt, story_work sw,"
." biblechapter c, biblebook b, bibletranslation bt, canonicalbiblebook cb, bibleverse v"
." WHERE w.ispublic and t.ispublic". $sLangFilter
." and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id"
." and bs.startverse_id = cv.id"
." and cv.chapter_id = scc.id and v.verse_id is null"
." and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and v.chapter_id = c.id and b.id = c.book_id and b.translation_id=bt.id and scc.book_id = cb.id and t.id = tt.translation_id and tt.text is not null and tt.text <> ''"
." order by cv.ordering, lang_order, t.is_modern desc, w.ordering, t.islanguagedefault desc limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
}
return $aRet;
}
function getBibleTranslationByChapter($nChapterId, $isCanonical)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, cc.id as chapter_id, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb, canonicalbiblechapter cc";
if($isCanonical)
$sSql .= " where cc.id = " . $nChapterId;
else
$sSql .= " where c.id = " . $nChapterId;
$sSql .= " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id and cc.book_id = cb.id and cc.ordering = c.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getBibleTranslationByUrl($translationUrl, $nLanguageID=null)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, case when t.languagedefault then 1 else 2 end as trans_ordering, l.id as language_id, t.abbreviation"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $translationUrl)) . "'";
$sSql .= " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id";
if($nLanguageID != null)
$sSql .= " and l.id = " . $nLanguageID;
$sSql .= " order by lang_ordering, trans_ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getBibleTranslationByID($nTranslationID)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE t.id = " . $nTranslationID;
$sSql .= " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getDefaultBibleTranslationByLocale($locale)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.shortcode = '" . $locale . "' then 1 else case when l.shortcode = 'en' then 2 else 3 end end as lang_ordering, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id order by lang_ordering, languagedefault desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getBibleTranslationByBookID($nBookID, $nLanguageID)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, case when t.languagedefault then 1 else 2 end as trans_ordering, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, canonicalbiblebook cb"
." WHERE b.id = ". $nBookID;
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id";
if($nLanguageID != null)
$sSql .= " and l.id = " . $nLanguageID;
$sSql .= " order by lang_ordering, trans_ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getFirstCanonicalBibleChapterID($bookUrl)
{
$sSql = "SELECT cc.id"
." FROM canonicalbiblebook cb, canonicalbiblechapter cc"
." WHERE cb.url = '" . trim(str_replace("'", "''", $bookUrl)) . "'";
$sSql .= " and cc.book_id = cb.id";
$sSql .= " order by cc.ordering limit 1";
// echo $sSql;
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = $aTemp[0]['id'];
return $nRet;
}
function getAllBibleTranslationsByLanguage($canonical_book_id, $chapter_order)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, bk.url as first_book_url, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, (case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) as group_order, (case when tr.translation_id is null then false else true end) as has_book, (case when tc.translation_id is null then false else true end) as has_chapter"
." FROM language l, bibletranslation t"
." join ("
." select distinct on (t2.id) t2.id as translation_id, cb.url"
." from bibletranslation t2, biblebook b, canonicalbiblebook cb where b.translation_id = t2.id and cb.id = b.canonicalization_id"
." order by t2.id, b.ordering"
.") as bk on t.id = bk.translation_id"
." left join (select translation_id from biblebook where canonicalization_id = " . $canonical_book_id . ") as tr on t.id = tr.translation_id"
." left join (select distinct translation_id from biblebook b3, biblechapter c3 where b3.canonicalization_id = " . $canonical_book_id . " and c3.book_id = b3.id and c3.ordering = " . $chapter_order . ") as tc on t.id = tc.translation_id"
." WHERE t.enabled and t.language_id = l.id";
$sSql .= " order by group_order, language_name, t.name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleTranslationsByLanguageForVerses($canonical_book_id, $first_chapter_order, $first_verse_display, $last_chapter_order, $last_verse_display, $language_id=null)
{
$sGroupOrder = $this->getGroupOrderForBibleTranslation($language_id);
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, bk.url as first_book_url, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, " . $sGroupOrder . " as group_order, true as has_book, true as has_chapter, l.righttoleft"
." FROM language l, bibletranslation t"
." join ("
." select distinct on (t2.id) t2.id as translation_id, cb.url"
." from bibletranslation t2, biblebook b, canonicalbiblebook cb where b.translation_id = t2.id and cb.id = b.canonicalization_id"
." order by t2.id, b.ordering"
.") as bk on t.id = bk.translation_id"
." WHERE t.enabled and t.language_id = l.id"
." and t.id in (select distinct translation_id from biblebook b3, biblechapter c3, bibleverse v3 where b3.canonicalization_id = " . $canonical_book_id . " and c3.book_id = b3.id and c3.ordering = " . $first_chapter_order . " and v3.chapter_id = c3.id and v3.indexdisplay = '" . $first_verse_display . "')";
if($first_chapter_order != $last_chapter_order || $first_verse_display != $last_verse_display)
$sSql .= " and t.id in (select distinct translation_id from biblebook b3, biblechapter c3, bibleverse v3 where b3.canonicalization_id = " . $canonical_book_id . " and c3.book_id = b3.id and c3.ordering = " . $last_chapter_order . " and v3.chapter_id = c3.id and v3.indexdisplay = '" . $last_verse_display . "')";
$sSql .= " order by group_order, language_name, t.name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleTranslation($nTranslationID, $bookID)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, canonicalbiblebook cb"
." WHERE t.id = " . $nTranslationID . " and cb.id = " . $bookID;
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getBibleTranslationByLanguage($nLanguageID)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, case when t.languagedefault then 1 else 2 end as trans_ordering, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE l.id = " . $nLanguageID . " and t.language_id = l.id";
$sSql .= " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled ";
$sSql .= " order by lang_ordering, trans_ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getStoriesForTranslation($translation_id, $story_num)
{
$sSql = "SELECT distinct bs.id, bs.url, bs.name, si.file as image_file, cb1.name as book_name, cc1.ordering as start_chapter_order, cv1.ordering as start_verse_order, cc2.ordering as end_chapter_order, cv2.ordering as end_verse_order, ci1.first_order as start_chapter_min_order, ci1.last_order as start_chapter_max_order, ci2.first_order as end_chapter_min_order, ci2.last_order as end_chapter_max_order, t.url as translation_url"
." FROM story_work sw, expositiontranslation t, expositionwork w, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbiblebook cb1, canonicalbibleverse cv2, canonicalbiblechapter cc2, "
." (select id, min(ordering) as first_order, max(ordering) as last_order from canonicalbiblechapter group by id) as ci1, "
." (select id, min(ordering) as first_order, max(ordering) as last_order from canonicalbiblechapter group by id) as ci2, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs"
." left join (select distinct on (si1.story_id) si1.story_id, i.file from storyillustration si1, image i where i.id = si1.image_id and i.mediatype_id = " . $this->MEDIA_TYPE_ID_IMAGE . ") as si on bs.id = si.story_id"
." WHERE cc1.book_id in (select canonicalization_id from biblebook where translation_id = " . $translation_id . ")"
." and w.ispublic and t.ispublic and sw.work_id = w.id"
." and cv1.chapter_id = cc1.id and cv1.id = bs.startverse_id"
." and bs.id = sw.story_id and t.work_id = sw.work_id"
." and cb1.id = cc1.book_id"
." and cv2.chapter_id = cc2.id and cv2.id = bs.endverse_id"
." and ci1.id = cc1.id"
." and ci2.id = cc2.id";
if($story_num != null and $story_num > 0)
$sSql .= " limit " . $story_num;
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$startChapterNum = $aRet[$i]["start_chapter_order"];
$endChapterNum = $aRet[$i]["end_chapter_order"];
$startVerseNum = $aRet[$i]["start_verse_order"];
$endVerseNum = $aRet[$i]["end_verse_order"];
$bookName = $aRet[$i]["book_name"];
if($bookName == 'Psalms') {
$bookName = 'Psalm';
} else {
$bookName = str_replace('_', ' ', $bookName);
}
$fullChapter = ($startVerseNum == $aRet[$i]["start_chapter_min_order"] && $endVerseNum == $aRet[$i]["end_chapter_max_order"]);
if($fullChapter) {
if($startChapterNum == $endChapterNum) {
$bookName .= ' '.$startChapterNum;
} else {
$bookName .= ' '.$startChapterNum.'-'.$endChapterNum;
}
} else {
if($startChapterNum != $endChapterNum) {
$bookName .= ' '.$startChapterNum.':'.$startVerseNum.'-'.$endChapterNum.':'.$endVerseNum;
} elseif($startVerseNum != $endVerseNum) {
$bookName .= ' '.$startChapterNum.':'.$startVerseNum.'-'.$endVerseNum;
} else {
$bookName .= ' '.$startChapterNum.':'.$startVerseNum;
}
}
$aRet[$i]["verse_range_text"] = $bookName;
}
return $aRet;
}
function getBibleTranslationByCanonBookID($nCBookID, $nLanguageID)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name, t.url, l.righttoleft, b.id as book_id, b.name as book_name, cb.url as canonical_book_url, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_ordering, case when t.languagedefault then 1 else 2 end as trans_ordering, l.id as language_id"
." FROM language l, bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE cb.id = ". $nCBookID;
$sSql .= " and b.canonicalization_id = cb.id and c.book_id = b.id and b.translation_id = t.id and t.enabled and t.language_id = l.id";
if($nLanguageID != null)
$sSql .= " and l.id = " . $nLanguageID;
$sSql .= " order by lang_ordering, trans_ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getLanguageByBibliographicCode($language_code)
{
$sSql = "SELECT id, (case when nativename is not null and nativename <> '' then nativename else name end) as name FROM language"
." WHERE bibliographiccode = '" . trim(str_replace("'", "''", $language_code)) . "' limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getFirstBibleChapterOrder($translation_url, $book_url)
{
$sSql = "SELECT min(c.ordering) as first_chapter_order"
." FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $translation_url)) . "' and t.enabled and b.translation_id = t.id and cb.url = '" . trim(str_replace("'", "''", $book_url)) . "' "
." and c.book_id = b.id and b.canonicalization_id = cb.id";
// echo $sSql."<br>";
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = current($aTemp[0]);
return $nRet;
}
function getLanguageByID($language_id)
{
$sSql = "SELECT id, case when searchlibrary is null then 'simple' else searchlibrary end as searchlibrary FROM language"
." WHERE id = " . $language_id;
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getLanguageByShortCode($language_code)
{
$sSql = "SELECT id, case when searchlibrary is null then 'simple' else searchlibrary end as searchlibrary FROM language"
." WHERE shortcode = '" . trim(str_replace("'", "''", $language_code)) . "' limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getLanguageIDByShortCode($language_code)
{
$sSql = "SELECT id FROM language"
." WHERE shortcode = '" . trim(str_replace("'", "''", $language_code)) . "' limit 1";
// echo $sSql."<br>";
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = $aTemp[0]['id'];
return $nRet;
}
// $book should be biblebook, not canonical book
function getChapterExplanations($book, $work_translation, $request)
{
$sSql = '';
$nCurrTranslationID = $work_translation->getId();
// $nLanguageID = $work_translation->getLanguage()->getId();
$nLanguageID = $book->getTranslation()->getLanguage()->getId();
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nCountOtherCase = 0;
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $nUILanguageID)
{
$sLangStr .= ' case when t.language_id = ' . $nUILanguageID . ' then 2 else';
$nCountOtherCase++;
}
if(!empty($nPreferredLanguageID) && $nLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangStr .= ' case when t.language_id = ' . $nPreferredLanguageID . ' then 3 else';
$nCountOtherCase++;
}
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 4 else';
$nCountOtherCase++;
}
$sLangStr .= ' 5 end';
for($i=0;$i<$nCountOtherCase;$i++)
$sLangStr .= ' end';
$sLangStr .= ' as lang_order';
if($book != null)
{
$sSql = "select distinct on (chapter_order) * from (SELECT distinct t.id as translation_id, bs.name as explanationtitle, bs.chapter_id as cchapter_id, bible.chapter_id, cbc.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || '" . $book->getTranslation()->getUrl() . "' || '_' || '" . $book->getCanonicalization()->getUrl() . "' || '_' || bible.chapter_order as bible_spec, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, " . $sLangStr . ", case when t.id = " . $nCurrTranslationID . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, t.is_modern, w.id as work_id, bs.id as story_id"
." FROM expositionwork w, expositiontranslation t, story_work sw, canonicalbiblechapter cbc, biblestory bs"
." left join (SELECT distinct bc.id as chapter_id, bc.ordering as chapter_order, cbc.id as cchapter_id FROM bibleverse bv, biblechapter bc, canonicalbibleverse_bibleverse cbv_bv, canonicalbiblechapter cbc, canonicalbibleverse cbv where bc.book_id = " . $book->getId() . " and bv.chapter_id = bc.id and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.chapter_id = cbc.id) as bible on bs.chapter_id = bible.cchapter_id"
." WHERE bs.chapter_id in (select id from canonicalbiblechapter where book_id in (select book_id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $work_translation->getWork()->getId() . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id)) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by cbc.ordering, lang_order, t.is_modern desc, trans_order) as t";
}
else
{
$sSql = "select distinct on (chapter_order,work_id) * from (SELECT t.id as translation_id, bs.name as explanationtitle, bs.chapter_id as cchapter_id, null as chapter_id, cbc.ordering as chapter_order, t.url as translation_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, " . $sLangStr . ", case when t.id = " . $nCurrTranslationID . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, t.is_modern, w.id as work_id, bs.id as story_id"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc"
." WHERE bs.chapter_id in (select id from canonicalbiblechapter where book_id in (select book_id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $work_translation->getWork()->getId() . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id)) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic) as t order by chapter_order, lang_order, is_modern desc, trans_order desc";
}
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if($book != null && !empty($aTemp))
{
foreach($aTemp as $t)
{
$aRet[$t["chapter_id"]] = $t;
}
}
else
$aRet = $aTemp;
return $aRet;
}
// $book should be biblebook, not canonical book
function getChapterExplanationsForCombo($book, $work_translation)
{
$sSql = '';
$nCurrTranslationID = $work_translation->getId();
//$nLanguageID = $work_translation->getLanguage()->getId();
$nLanguageID = $book->getTranslation()->getLanguage()->getId();
$nCurrWorkID = $work_translation->getWork()->getId();
if($book != null)
{
$sSql = "SELECT distinct t.id as translation_id, t.translatedtitle as explanationtitle, bs.chapter_id as cchapter_id, bible.chapter_id, cbc.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || '" . $book->getTranslation()->getUrl() . "' || '_' || '" . $book->getCanonicalization()->getUrl() . "' || '_' || bible.chapter_order as bible_spec, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $nCurrTranslationID . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, w.id as work_id, t.is_modern, bs.id as story_id, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name "
." FROM expositionwork w, expositiontranslation t, story_work sw, canonicalbiblechapter cbc, language l, biblestory bs"
." left join (SELECT distinct bc.id as chapter_id, bc.ordering as chapter_order, cbc.id as cchapter_id FROM bibleverse bv, biblechapter bc, canonicalbibleverse_bibleverse cbv_bv, canonicalbiblechapter cbc, canonicalbibleverse cbv where bc.book_id = " . $book->getId() . " and bv.chapter_id = bc.id and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.chapter_id = cbc.id) as bible on bs.chapter_id = bible.cchapter_id"
." WHERE bs.chapter_id in (select cbc2.id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $nCurrWorkID . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.language_id = l.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by lang_order, t.is_modern desc, trans_order";
}
else
{
$sSql = "SELECT t.id as translation_id, w.title as explanationtitle, bs.chapter_id as cchapter_id, null as chapter_id, cbc.ordering as chapter_order, t.url as translation_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $nCurrTranslationID . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, w.id as work_id, t.is_modern, bs.id as story_id, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc, language l"
." WHERE bs.chapter_id in (select cbc2.id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $nCurrWorkID . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.language_id = l.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by lang_order, t.is_modern desc, trans_order";
}
// echo $sSql."<br>";
/* $aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if($book != null && !empty($aTemp))
{
foreach($aTemp as $t)
{
$aRet[$t["chapter_id"]] = $t;
}
}
else
$aRet = $aTemp;
return $aRet; */
return $this->getNativeQueryResults($sSql);
}
// $chapter_id: verse's chapter id
function getVerseExplanations($chapter_id, $nLanguageID, $request)
{
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nCountOtherCase = 0;
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $nUILanguageID)
{
$sLangStr .= ' case when t.language_id = ' . $nUILanguageID . ' then 2 else';
$nCountOtherCase++;
}
if(!empty($nPreferredLanguageID) && $nLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangStr .= ' case when t.language_id = ' . $nPreferredLanguageID . ' then 3 else';
$nCountOtherCase++;
}
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 4 else';
$nCountOtherCase++;
}
$sLangStr .= ' 5 end';
for($i=0;$i<$nCountOtherCase;$i++)
$sLangStr .= ' end';
$sLangStr .= ' as lang_order';
$sSql = "select distinct on (verse_order) * from (SELECT t.id as translation_id, bs.name as explanationtitle, v.id as verse_id, t.url as translation_url, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || bt.url || '_' || cb.url || '_' || c.ordering || '_' || v.indexdisplay as bible_spec, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, sw.story_id, cv.ordering as verse_order, " . $sLangStr
.", t.work_id FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_VERSE . ") as bs, canonicalbibleverse cv, bibleverse v, canonicalbibleverse_bibleverse cvv,"
." canonicalbiblechapter scc, expositionwork w, expositiontranslation t, story_work sw,"
." biblechapter c, biblebook b, bibletranslation bt, canonicalbiblebook cb"
." WHERE v.chapter_id = " . $chapter_id
." and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id"
." and bs.startverse_id = cv.id"
." and cv.chapter_id = scc.id"
." and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and w.ispublic and t.ispublic"
." and v.chapter_id = c.id and b.id = c.book_id and b.translation_id=bt.id and scc.book_id = cb.id and v.verse_id is null"
." order by cv.ordering, lang_order) as t order by verse_order";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(!empty($aTemp))
{
foreach($aTemp as $t)
{
$aRet[$t["verse_id"]] = $t;
}
}
else
$aRet = $aTemp;
return $aRet;
}
function getFeaturedStoryList($locale, $vLimit, $vUseDefaultLang=false)
{
if($vUseDefaultLang)
$nLanguageID = $this->LANGUAGE_ID_ENGLISH;
else
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (SELECT distinct on (t.id) t.translatedtitle AS title, t.description, t.url, bsw.story_id, bsw.story_url, t.url as translation_url, wo.feature_ordering, ";
$sSql .= "case when fi.image_file is null then case when fi2.image_file is null then ti.image_file else fi2.image_file end else fi.image_file end as image_file, case when fi.image_file is null then ti.image_title else fi.image_title end as image_title";
$sSql .= " FROM expositionwork w, expositiontranslation t";
// featured image from the current translation
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
// featured image from the translation with image
$sSql .= " left join (select distinct on (t1.work_id) t1.work_id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id order by t1.work_id, case when t1.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) fi2 on t.work_id = fi2.work_id";
// textunit image
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1) ti on t.id = ti.translation_id";
$sSql .= " join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") as wo on t.work_id = wo.work_id"
." join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs where bs.id = sw.story_id) bsw on t.work_id = bsw.work_id"
." WHERE t.language_id = " . $nLanguageID . " AND t.ispublic AND w.ispublic and t.work_id=w.id"
." and story_url is not null";
$sSql .= ") as t1"
." ORDER BY t1.feature_ordering";
if($vLimit != null && $vLimit > 0)
$sSql .= " limit " . $vLimit;
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getFeaturedListByCategory($vCategoryId, $locale, $vLimit, $vUseDefaultLang=false)
{
if($vUseDefaultLang)
$nLanguageID = $this->LANGUAGE_ID_ENGLISH;
else
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$bConcept = ($vCategoryId == $this->CATEGORY_ID_CONCEPT);
$sSql = "select * from (SELECT distinct on (t.id) t.translatedtitle AS title, t.description, t.url, wo.feature_ordering, ";
if($bConcept)
{
$sSql .= "case when fi.image_file is not null then fi.image_file else case when fi2.image_file is not null then fi2.image_file else case when ci.image_file is not null then ci.image_file else ti.image_file end end end as image_file, case when ci.image_file is not null then ci.image_title else case when fi.image_file is not null then fi.image_title else case when fi2.image_file is not null then fi2.image_title else ti.image_title end end end as image_title";
}
else
{
$sSql .= "case when fi.image_file is not null then fi.image_file else case when fi2.image_file is not null then fi2.image_file else ti.image_file end end as image_file, case when fi.image_file is not null then fi.image_title else case when fi2.image_file is not null then fi2.image_title else ti.image_title end end as image_title";
}
$sSql .= " FROM expositionwork w, expositiontranslation t ";
// featured image from the current translation
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
// featured image from the translation with image
$sSql .= " left join (select distinct on (t1.work_id) t1.work_id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id order by t1.work_id, case when t1.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) fi2 on t.work_id = fi2.work_id";
// textunit image
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1) ti on t.id = ti.translation_id";
if($bConcept)
{
$sSql .= " left join (select distinct on (cl.work_id) cl.work_id as id, i.file as image_file, i.title as image_title from image i, conceptillustration ci1, concept_work cl where cl.concept_id = ci1.concept_id and ci1.image_id = i.id) ci on t.work_id = ci.id";
}
$sSql .= " join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") as wo on t.work_id = wo.work_id"
." WHERE (t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $vCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $vCategoryId . ")) and t.language_id = " . $nLanguageID . " AND t.ispublic and w.ispublic and t.work_id=w.id";
$sSql .= ") as t1 ORDER BY t1.feature_ordering";
if($vLimit != null && $vLimit > 0)
$sSql .= " limit " . $vLimit;
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getConsiderList($locale, $sSearchText=null)
{
$sSql = "SELECT t.translatedtitle AS title, t.url as translation_url, d.url as division_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' || t.url as ref_column_spec, w.id as work_id, t.id as translation_id"
." FROM expositionwork w, expositiontranslation t, (select distinct on (work_id) work_id, url, min(ordering) as ordering from expositiondivision group by work_id, url) as d"
." WHERE t.work_id=w.id and w.id in (select work_id from work_category where category_id = " . $this->CATEGORY_ID_SPIRITUAL_TOPIC . ")"
." and d.work_id = w.id AND w.ispublic AND t.ispublic"
." AND w.swedenborgtype IS NULL"
." AND t.translatedtitle IS NOT NULL and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, language l WHERE ";
$sSql .= " t.work_id=w.id and t.language_id = l.id order by w.id, (case when l.shortcode = '" . $locale . "' then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end), t.islanguagedefault desc)"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
if(!empty($sSearchText))
$sSql .= " and lower(t.translatedtitle) like '%" . str_replace("'", "''", strtolower($sSearchText)) . "%'";
$sSql .= " order by w.ordering, t.translatedtitle";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleStudyList($locale)
{
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.translatedtitle AS title, t.url as translation_url, d.url as division_url"
.", (case when b.datecreated is null then null else to_date(b.datecreated, 'MM/DD/YYYY') end) as datecreated, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' || t.url as ref_column_spec, t.id as translation_id"
." FROM expositionwork w, expositiontranslation t, expositiondivision d, bibliographicdata b, translation_bibliographicdata tb, language l"
." WHERE t.work_id=w.id and w.id in (select work_id from work_category where category_id = " . $this->CATEGORY_ID_BIBLE_STUDY . ")"
." and d.work_id = w.id AND w.ispublic AND t.ispublic"
." AND w.swedenborgtype IS NULL"
." AND t.translatedtitle IS NOT NULL"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT
." and t.id = tb.translation_id and t.language_id = l.id and b.id = tb.bibliographicdata_id ORDER BY w.id, (case when l.shortcode = '" . $locale . "' then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end), t.islanguagedefault desc) as t order by datecreated";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getOtherBibleRefPassage($aVerseInfo, $category_id)
{
$canon = array();
$nulls = array();
$verseLanguage = null;
$sVerseIDs = $aVerseInfo['CVerseIDs'];
$verseLanguage = $aVerseInfo['LanguageID'];
/* foreach($verses as $verse) {
if($verse === null) {
continue;
}
foreach($verse->getCanonicalization() as $canonicalVerse) {
if($canonicalVerse != null)
{
if($sVerseIDs != '')
$sVerseIDs .= ',';
$sVerseIDs .= $canonicalVerse->getId();
}
}
if($verseLanguage == null)
$verseLanguage = $verse->getLanguage()->getId();
} */
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
if($sVerseIDs == '') {
return null;
} else {
return $this->getOtherBibleRefPassageInfo($verseLanguage, $sVerseIDs, $category_id);
}
}
function getOtherBibleRefPassageInfo($language_id, $sVerseIDs, $category_id)
{
if($sVerseIDs == '')
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = " p.id in (SELECT DISTINCT passage_id FROM verse_passage"
." WHERE verse_id in (" . $sVerseIDs . "))";
$sFilter .= " and w.swedenborgtype is null and w.id in (select work_id from work_category where category_id = " . $category_id . ")";
$sSql = "select * from (SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, p.swedenborgsection as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as ref_column_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, t.islanguagedefault desc) order by p.id, lang_order";
/* if($language_id != $nDefaultLanguageID)
{
$sSql .= " union SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, p.swedenborgsection as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as ref_column_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id = ".$nDefaultLanguageID. " and t.ispublic and w.ispublic";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, t.islanguagedefault desc)";
$sSql .= " and p.id not in (";
$sSql .= "SELECT distinct p.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id = ".$language_id. " and t.ispublic and w.ispublic";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, t.islanguagedefault desc)";
$sSql .= ")";
} */
$sSql .= ") as t2 order by work_order, title, ordering";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleChapterID($sTranslationUrl, $sBookUrl, $nChapterNum=null)
{
$sSql = "SELECT c.id"
." FROM biblebook b, biblechapter c, bibletranslation t, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $sTranslationUrl)) . "'"
." and cb.url = '" . trim(str_replace("'", "''", $sBookUrl)) . "'";
if($nChapterNum != null)
$sSql .= " and c.ordering = " . $nChapterNum;
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and c.book_id = b.id limit 1";
// echo $sSql."<br>";
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = $aTemp[0]['id'];
return $nRet;
}
function getBibleVerseID($sTranslationUrl, $sBookUrl, $nChapterNum=null, $sVerseIndex=null)
{
$sSql = "SELECT v.id"
." FROM biblebook b, biblechapter c, bibletranslation t, bibleverse v, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $sTranslationUrl)) . "'"
." and cb.url = '" . trim(str_replace("'", "''", $sBookUrl)) . "'";
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and c.book_id = b.id and v.chapter_id = c.id";
if($nChapterNum != null)
$sSql .= " and c.ordering = " . $nChapterNum;
if($sVerseIndex != null)
{
if(!is_numeric($sVerseIndex))
{
$filteredNumbers = array_filter(preg_split("/\D+/", $sVerseIndex));
$verseNum = reset($filteredNumbers);
$subverse = trim($sVerseIndex, $verseNum);
$sSql .= " and v.indexdisplay = '" . trim(str_replace("'", "''", $subverse)) . "'";
$sSql .= " and v.verse_id in (";
$sSql .= "SELECT v.id"
." FROM biblebook b, biblechapter c, bibletranslation t, bibleverse v, canonicalbiblebook cb"
." WHERE t.url = '" . trim(str_replace("'", "''", $sTranslationUrl)) . "'"
." and cb.url = '" . trim(str_replace("'", "''", $sBookUrl)) . "'";
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and c.book_id = b.id and v.chapter_id = c.id";
if($nChapterNum != null)
$sSql .= " and c.ordering = " . $nChapterNum;
$sSql .= " and v.indexdisplay = '" . $verseNum . "')";
}
else
$sSql .= " and v.indexdisplay = '" . trim(str_replace("'", "''", $sVerseIndex)) . "'";
}
$sSql .= " order by c.ordering, v.indexdisplay limit 1";
// echo $sSql;
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = $aTemp[0]['id'];
return $nRet;
}
function getNextChapter($chapter_id, $nextbook_id=null)
{
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering as chapter_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE c.book_id in (select book_id from biblechapter where id = " . $chapter_id . ") and c.ordering > (select ordering from biblechapter where id = " . $chapter_id . ") and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id order by c.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
else
$aRet = $this->getNextChapterInNextBook($chapter_id, $nextbook_id);
return $aRet;
}
function getNextChapterInNextBook($chapter_id, $nextbook_id)
{
$aRet = array();
if($nextbook_id != '')
{
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering as chapter_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE c.book_id = " . $nextbook_id . " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id order by c.ordering limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
}
return $aRet;
}
function getPreviousChapter($chapter_id, $prevbook_id=null)
{
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering as chapter_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE c.book_id in (select book_id from biblechapter where id = " . $chapter_id . ") and c.ordering < (select ordering from biblechapter where id = " . $chapter_id . ") and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id order by c.ordering desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
else
$aRet = $this->getPreviousChapterInPreviousBook($chapter_id, $prevbook_id);
return $aRet;
}
function getPreviousChapterInPreviousBook($chapter_id, $prevbook_id)
{
$aRet = array();
if($prevbook_id != '')
{
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering as chapter_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb"
." WHERE c.book_id = " . $prevbook_id . " and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id order by c.ordering desc limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
}
return $aRet;
}
function getNextVerse($verse_id)
{
$sSql = "SELECT id FROM bibleverse"
." WHERE verse_id = " . $verse_id . " and chapter_id in (select chapter_id from bibleverse where id = " . $verse_id . ") and indexdisplay like '%-%' limit 1";
// echo $sSql."<br>";
$aTemp1 = $this->getNativeQueryResults($sSql);
if(count($aTemp1) > 0)
$verse_id = $aTemp1[0]['id'];
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, (case when v2.indexdisplay is not null and v.indexdisplay not like '%-%' then v.indexdisplay else null end) as subverse_index, (case when v2.indexdisplay is not null then case when v.indexdisplay like '%-%' then v2.indexdisplay else v2.indexdisplay || v.indexdisplay end else v.indexdisplay end) as verse_label, (case when v2.indexdisplay is not null then v2.indexdisplay else v.indexdisplay end) as verse_index, v.id as verse_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering || '_' || (case when v2.indexdisplay is not null then v2.indexdisplay else '' end) || v.indexdisplay as verse_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb, bibleverse v"
." left join bibleverse v2 on v.verse_id = v2.id"
." WHERE c.book_id in (select book_id from biblechapter where id in (select chapter_id from bibleverse where id = " . $verse_id . ")) and v.chapter_id = c.id and v.ordering > (select ordering from bibleverse where id = " . $verse_id . ") and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and (v.verse_id is null or v.indexdisplay like '%-%') order by v.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getPreviousVerse($verse_id)
{
$sSql = "SELECT id FROM bibleverse"
." WHERE verse_id = " . $verse_id . " and chapter_id in (select chapter_id from bibleverse where id = " . $verse_id . ") and indexdisplay like '%-%' limit 1";
// echo $sSql."<br>";
$aTemp1 = $this->getNativeQueryResults($sSql);
if(count($aTemp1) > 0)
$verse_id = $aTemp1[0]['id'];
$sSql = "SELECT c.id, t.url as translation_url, cb.url as book_url, c.ordering as chapter_order, (case when v2.indexdisplay is not null and v.indexdisplay not like '%-%' then v.indexdisplay else null end) as subverse_index, (case when v2.indexdisplay is not null then case when v.indexdisplay like '%-%' then v2.indexdisplay else v2.indexdisplay || v.indexdisplay end else v.indexdisplay end) as verse_label, (case when v2.indexdisplay is not null then v2.indexdisplay else v.indexdisplay end) as verse_index, v.id as verse_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "' || '_' || t.url || '_' || cb.url || '_' || c.ordering || '_' || (case when v2.indexdisplay is not null then v2.indexdisplay else '' end) || v.indexdisplay as verse_multi_spec, b.name as book_name";
$sSql .= " FROM bibletranslation t, biblebook b, biblechapter c, canonicalbiblebook cb, bibleverse v"
." left join bibleverse v2 on v.verse_id = v2.id"
." WHERE c.book_id in (select book_id from biblechapter where id in (select chapter_id from bibleverse where id = " . $verse_id . ")) and v.chapter_id = c.id and v.ordering < (select ordering from bibleverse where id = " . $verse_id . ") and c.book_id = b.id and b.canonicalization_id = cb.id and b.translation_id = t.id and (v.verse_id is null or v.indexdisplay like '%-%') order by v.ordering desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getTextToSpeech($vLanguageId)
{
$sSql = "SELECT t.name"
." FROM texttospeech t, texttospeechlanguage tl"
." WHERE tl.language_id = " . $vLanguageId
." and t.id = tl.tts_id";
$sSql .= " order by tl.is_standard desc limit 1";
// echo $sSql;
$sRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = $aTemp[0]['name'];
return $sRet;
}
function checkBookInfo($book_id, $chapterStartIndex, $chapterEndIndex, $verseStartIndex, $verseEndIndex)
{
$aRet = array();
// check start chapter
$sSql = "SELECT ordering FROM biblechapter"
." WHERE book_id = " . $book_id . " and ordering = " . $chapterStartIndex . " limit 1";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['chapterStartIndex'] = current($aTemp[0]);
else
{
$sSql = "SELECT min(ordering) FROM biblechapter"
." WHERE book_id = " . $book_id;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['chapterStartIndex'] = current($aTemp[0]);
}
// check end chapter
if($chapterEndIndex != null)
{
$sSql = "SELECT ordering FROM biblechapter"
." WHERE book_id = " . $book_id . " and ordering = " . $chapterEndIndex . " limit 1";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['chapterEndIndex'] = current($aTemp[0]);
else
{
$aRet['chapterEndIndex'] = $aRet['chapterStartIndex'];
}
}
// check start verse
$sSql = "SELECT v.indexdisplay FROM bibleverse v, biblechapter c"
." WHERE c.book_id = " . $book_id . " and c.ordering = " . $aRet['chapterStartIndex']
." and v.chapter_id = c.id and v.indexdisplay = '" . $verseStartIndex . "' limit 1";
// echo $sSql;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['verseStartIndex'] = current($aTemp[0]);
else
{
$sSql = "SELECT v.indexdisplay FROM bibleverse v, biblechapter c"
." WHERE book_id = " . $book_id . " and c.ordering = " . $aRet['chapterStartIndex']
." and v.chapter_id = c.id order by v.ordering limit 1";
// echo $sSql;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet['verseStartIndex'] = current($aTemp[0]);
}
}
// check end verse
if($verseEndIndex != null && isset($aRet['chapterEndIndex']) && $aRet['chapterEndIndex'] != null)
{
$sSql = "SELECT v.indexdisplay FROM bibleverse v, biblechapter c"
." WHERE c.book_id = " . $book_id . " and c.ordering = " . $aRet['chapterEndIndex']
." and v.chapter_id = c.id and v.indexdisplay = '" . $verseEndIndex . "' limit 1";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['verseEndIndex'] = current($aTemp[0]);
else
{
$sSql = "SELECT v.indexdisplay FROM bibleverse v, biblechapter c"
." WHERE book_id = " . $book_id . " and c.ordering = " . $aRet['chapterEndIndex']
." and v.chapter_id = c.id order by v.ordering limit 1";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet['verseEndIndex'] = current($aTemp[0]);
}
}
return $aRet;
}
function getChapterExplanationsForText($translation)
{
$nCurrTranslationID = $translation->getId();
$nLanguageID = $translation->getLanguage()->getId();
$sSql = "select distinct on (chapter_order) * from (SELECT t.id as translation_id, t.url as translation_url, bs.name as explanationtitle, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, cbc.ordering as chapter_order, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $nCurrTranslationID . " then 1 else case when t.language_id = " . $nLanguageID . " then 2 else case when t.islanguagedefault then 3 else 4 end end end as trans_order, w.id as work_id, bs.id as story_id, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc, language l"
." WHERE bs.chapter_id in (select id from canonicalbiblechapter where book_id in (select book_id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $translation->getWork()->getId() . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id)) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and cbc.id = bs.chapter_id and t.language_id = l.id and w.ispublic AND t.ispublic order by cbc.ordering, lang_order, trans_order, t.is_modern desc, sw.ordering) as t";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getChapterExplanationsForNav($translation)
{
$nTranslationID = $translation->getId();
$nLanguageID = $translation->getLanguage()->getId();
$sSql = "select distinct on (chapter_order) * from (SELECT t.id as translation_id, t.url as translation_url, bs.name as explanationtitle, cbc.ordering as chapter_order, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $nTranslationID . " then 1 else case when t.language_id = " . $nLanguageID . " then 2 else case when t.islanguagedefault then 3 else 4 end end end as trans_order, cb.id as book_id"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc, canonicalbiblebook cb"
." WHERE bs.chapter_id in (select id from canonicalbiblechapter where book_id in (select book_id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $translation->getWork()->getId() . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id)) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic and cbc.book_id = cb.id order by cbc.ordering, lang_order, trans_order, t.is_modern desc, sw.ordering) as t";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getChapterExplanationsForTextForCombo($translation)
{
$nCurrTranslationID = $translation->getId();
$nLanguageID = $translation->getLanguage()->getId();
$nCurrWorkID = $translation->getWork()->getId();
$sSql = "SELECT t.id as translation_id, t.url as translation_url, w.title as explanationtitle, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $nCurrTranslationID . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, w.id as work_id, t.is_modern"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc"
." WHERE bs.chapter_id in (select cbc2.id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $nCurrWorkID . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by lang_order, t.is_modern desc, trans_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getFirstCBook($nTranslationID)
{
$sSql = "SELECT cb.id"
." FROM language l, bibletranslation t, biblebook b, canonicalbiblebook cb"
." WHERE t.id = " . $nTranslationID;
$sSql .= " and b.canonicalization_id = cb.id and b.translation_id = t.id and t.enabled and t.language_id = l.id order by cb.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getConceptUrl($vWorkId)
{
$sSql = "SELECT c.url"
." FROM expositionwork w, work_category wc, concept_work cl, expositiontranslation t, concept c"
." WHERE w.id = " . $vWorkId ." and wc.work_id = w.id"
." and t.work_id = w.id and cl.work_id = w.id and c.id = cl.concept_id"
." and wc.category_id = " . $this->CATEGORY_ID_CONCEPT;
// echo $sSql;
$sRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = $aTemp[0]['url'];
return $sRet;
}
function getTopPathList($locale, $vLimit=null)
{
return $this->getTopPathListFromDB($locale, true, $vLimit);
}
function getTopPathListFromDB($locale, $vIsFeatured, $vLimit)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT t.id, (case when tl.title is not null then tl.title else t.default_title end) AS title, (case when tl.description is not null then tl.description else t.default_description end) as description, t.url, i.file as image_file, i.title as image_title, (case when title_color is not null then title_color else '" . $this->DEFAULT_PATH_TITLE_COLOR . "' end) as title_color, (case when i.height > i.width then ' portrait' else '' end) as image_class"
." FROM image i, expositiontopicgroup t";
// if($this->showEnglishTrans($nLanguageID))
$sSql .= " left join ";
// else
// $sSql .= " join ";
$sSql .= " (select tl1.expositiontopicgroup_id, tl1.title, tl1.description from expositiontopicgroup_language tl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = tl1.language_id) as tl on t.id = tl.expositiontopicgroup_id"
." WHERE t.image_id = i.id and t.url is not null and t.url <> '' and t.id in (select expositiontopicgroup_id from expositiontopic_group)";
if($vIsFeatured)
$sSql .= " and t.is_featured";
else
$sSql .= " and not t.is_featured";
$sSql .= " ORDER BY t.display_order";
if($vLimit > 0)
$sSql .= " limit " . $vLimit;
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getSubPathList($toppathurl, $locale)
{
return $this->getSubPathListFromDB($toppathurl, $locale, true);
}
function getSubPathListFromDB($TopPathUrl, $locale, $vIsFeatured)
{
$sSql = "SELECT (case when tl.title is not null then tl.title else t.name end) AS title, (case when tl.description is not null then tl.description else t.default_description end) as description, t.url, i.file as image_file, i.title as image_title, (case when i.height > i.width then ' portrait' else '' end) as image_class"
." FROM image i, expositiontopicgroup tg, expositiontopic_group t_g, expositiontopic t"
." left join (select tl1.expositiontopic_id, tl1.title, tl1.description from expositiontopic_language tl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = tl1.language_id) as tl on t.id = tl.expositiontopic_id"
." WHERE t.image_id = i.id and t.url is not null and t.url <> '' and t.id = t_g.expositiontopic_id "
." and tg.id = t_g.expositiontopicgroup_id and tg.url = '" . trim(str_replace("'", "''", $TopPathUrl)) . "'"
." and t.id in (select topic_id from work_topic)";
if($vIsFeatured)
$sSql .= " and t_g.is_featured";
else
$sSql .= " and not t_g.is_featured";
$sSql .= " ORDER BY t_g.display_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getTopPathInfo($TopPathUrl, $locale)
{
$sSql = "SELECT (case when tl.title is not null then tl.title else t.default_title end) AS title, (case when tl.description is not null then tl.description else t.default_description end) as description, (case when title_color is not null then title_color else '" . $this->DEFAULT_PATH_TITLE_COLOR . "' end) as title_color"
." FROM expositiontopicgroup t"
." left join (select tl1.expositiontopicgroup_id, tl1.title, tl1.description from expositiontopicgroup_language tl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = tl1.language_id) as tl on t.id = tl.expositiontopicgroup_id"
." WHERE url = '" . trim(str_replace("'", "''", $TopPathUrl)) . "'";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getExplanationListForTopic($TopicUrl,$locale)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (select distinct on (work_id) * from (SELECT w.id as work_id,"
."t.translatedtitle AS title, t.description, t.url, wc.category_id, bsw.story_id, bsw.story_url,"
." ti.image_file, ti.image_title, ti.image_class, wt.display_order, " . $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
." FROM work_topic wt, expositiontopic et, expositiontranslation t left join vexpositiontranslationimage ti on t.id = ti.translation_id join expositionwork w on t.work_id = w.id left join work_category wc on w.id = wc.work_id"
." left join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs where bs.id = sw.story_id) bsw on w.id = bsw.work_id"
." WHERE et.url = '" . trim(str_replace("'", "''", $TopicUrl)) . "'"
." and wt.topic_id = et.id and wt.work_id = t.work_id"
." AND t.ispublic AND w.ispublic and t.url is not null and t.url <> ''"
." ORDER BY language_order, lang_default_order) as t1) as t where language_order < 3"
." ORDER BY display_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getSubPathInfo($SubPathUrl, $locale)
{
$sSql = "SELECT (case when tl.title is not null then tl.title else t.name end) AS title, (case when tl.description is not null then tl.description else t.default_description end) as description"
." FROM expositiontopic t"
." left join (select tl1.expositiontopic_id, tl1.title, tl1.description from expositiontopic_language tl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = tl1.language_id) as tl on t.id = tl.expositiontopic_id"
." WHERE t.url = '" . trim(str_replace("'", "''", $SubPathUrl)) . "'";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getExplanationListForTopAndSubPath($TopPathUrl, $SubPathUrl, $locale)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id,"
." t.translatedtitle AS title, t.description, t.url, wc.category_id, bsw.story_id,"
." bsw.story_url, ti.image_file, ti.image_title, ti.image_class, wt.display_order, " . $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
." FROM work_topic wt, expositiontopic et, work_topicgroup wtg, expositiontopicgroup tg, expositiontranslation t left join vexpositiontranslationimage ti on t.id = ti.translation_id join expositionwork w on t.work_id = w.id left join work_category wc on w.id = wc.work_id"
." left join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs where bs.id = sw.story_id) bsw on w.id = bsw.work_id"
." WHERE et.url = '" . trim(str_replace("'", "''", $SubPathUrl)) . "'"
." and tg.url = '" . trim(str_replace("'", "''", $TopPathUrl)) . "'"
." and wt.topic_id = et.id and wt.work_id = t.work_id"
." and wtg.topicgroup_id = tg.id and wtg.work_id = t.work_id"
." AND t.ispublic AND w.ispublic and t.url is not null and t.url <> ''"
." ORDER BY w.id, language_order, lang_default_order) as t where language_order < 3"
." ORDER BY display_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getTopicList($locale)
{
$sSql = "SELECT (case when tl.title is not null then tl.title else t.name end) AS title, (case when tl.description is not null then tl.description else t.default_description end) as description, t.url, i.file as image_file, i.title as image_title, (case when i.height > i.width then ' portrait' else '' end) as image_class"
." FROM image i, expositiontopic t"
." left join (select tl1.expositiontopic_id, tl1.title, tl1.description from expositiontopic_language tl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = tl1.language_id) as tl on t.id = tl.expositiontopic_id"
." WHERE t.image_id = i.id and t.url is not null and t.url <> ''"
." ORDER BY t.display_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function HasBibleBookForTrans($story_id, $bible_translation_url)
{
$bRet = false;
$sSql = "SELECT bt.id "
." FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs, canonicalbibleverse cv, canonicalbibleverse_bibleverse cvv, "
." canonicalbiblechapter scc, expositionwork w, expositiontranslation t, story_work sw,"
." biblechapter c, biblebook b, bibletranslation bt, canonicalbiblebook cb, bibleverse v"
." WHERE bs.id = " . $story_id . " and bt.url = '" . trim(str_replace("'", "''", $bible_translation_url)) . "' and w.ispublic and t.ispublic"
." and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id"
." and bs.startverse_id = cv.id"
." and cv.chapter_id = scc.id and v.verse_id is null"
." and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and v.chapter_id = c.id and b.id = c.book_id and b.translation_id=bt.id and scc.book_id = cb.id"
." limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$bRet = true;
return $bRet;
}
// get footnote popup text for footnotes that repeat after the first footnote
// (e.g. footnote '1' after the first one)
// The repeated footnotes have no hyperlinks, show them as plain text
function getPassageFootnotes($passage_id,$translation_id)
{
$sSql = "select di1.text as footnote, array_to_string(array_agg(di3.text order by di3.ordering),' ') as footnote_text from expositiondisplayitem di1, (select di2.* from expositiondisplayitem di2, expositiontranslationtext tt, expositiontextunit tu where tu.passage_id = " . $passage_id . " and tt.translation_id = " . $translation_id;
$sSql .= " and tu.id = tt.placement_id and di2.container_id = tt.id";
$sSql .= " order by di2.ordering) as di3 where di3.containingitem_id = di1.id group by di1.text";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$nLen = count($aTemp);
for($i=0;$i<$nLen;$i++)
{
$sTempText = $aTemp[$i]["footnote_text"];
$nPosDot = strpos($sTempText, '.');
$sCheckNum = substr($sTempText, 0, $nPosDot);
if(is_numeric($sCheckNum))
$sTempText = trim(substr($sTempText, $nPosDot+1));
$sTempText1 = "<i>". nl2br($sTempText) . "</i>";
$aRet[$aTemp[$i]["footnote"]] = $sTempText1;
// For quick view page
$sTempText2 = str_replace('\n', ' ', $sTempText);
$aRet[$aTemp[$i]["footnote"]."q"] = $sTempText2;
}
return $aRet;
}
function getReadingPlanList($user_id, $locale)
{
return $this->getReadingPlanListFromDB($user_id, $locale, true);
}
function getReadingPlanListFromDB($user_id, $locale, $vIsFeatured)
{
$bUser=!empty($user_id);
$sSql = "SELECT (case when rl.name is not null then rl.name else r.name end) AS title, (case when rl.intro is not null then rl.intro else r.intro end) as description, r.url, i.file as image_file, i.title as image_title, (case when i.height > i.width then ' portrait' else '' end) as image_class,";
if($bUser)
{
$sSql .= " ur.status_id";
}
else
{
$sSql .= " null as status_id";
}
$sSql .= " FROM image i, readingplan r"
." left join (select rl1.readingplan_id, rl1.name, rl1.intro, rl1.description from readingplan_language rl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = rl1.language_id) as rl on r.id = rl.readingplan_id";
if($bUser)
{
$sSql .= " left join (select * from user_readingplan where user_id = " . $user_id . ") ur on r.id = ur.readingplan_id";
}
$sSql .= " WHERE r.image_id = i.id and r.url is not null and r.id in (select readingplan_id from readingplanstep)";
if($vIsFeatured)
$sSql .= " and r.is_featured";
else
$sSql .= " and not r.is_featured";
if($bUser)
{
$sSql .= " ORDER BY (case when ur.status_id = 2 then 1 else case when ur.status_id is null or ur.status_id <> 4 then 2 else 3 end end), ordering";
}
else
{
$sSql .= " ORDER BY ordering";
}
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getReadingPlanInfo($readingPlanUrl, $locale)
{
$sSql = "SELECT r.id, count(rs.id) as step_count, (case when rl.name is not null then rl.name else r.name end) AS title, (case when rl.description is not null then rl.description else r.description end) as description, url, avg_daily_read_time, i.file as image_file, i.title as image_title, (case when i.height > i.width then ' portrait' else '' end) as image_class"
." FROM image i, readingplan r"
." left join readingplanstep rs on r.id = rs.readingplan_id"
." left join (select rl1.readingplan_id, rl1.name, rl1.description from readingplan_language rl1, language l where l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' and l.id = rl1.language_id) as rl on r.id = rl.readingplan_id"
." WHERE url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.image_id = i.id group by r.id, rl.name, r.name, rl.description, r.description, url, i.file, i.title, i.height, i.width" ;
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(empty($aTemp))
{
$sSql = "SELECT r.id, count(rs.id) as step_count, r.name AS title, r.description, url, avg_daily_read_time, i.file as image_file, i.title as image_title, (case when i.height > i.width then ' portrait' else '' end) as image_class"
." FROM image i, readingplan r"
." left join readingplanstep rs on r.id = rs.readingplan_id"
." WHERE url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.image_id = i.id group by r.id, r.name, r.description, url, i.file, i.title, i.height, i.width" ;
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
}
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
$aRet['first_order'] = $this->getReadingPlanFirstStepOrder($readingPlanUrl);
}
return $aRet;
}
function getUserStepInfo($readingPlanUrl, $UserID)
{
$sSql = "SELECT ur.completed_step_id, rps.name as current_status, ur.reminder_option_id FROM readingplan r, readingplanstatus rps, user_readingplan ur"
." where ur.user_id = " . $UserID
." and r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = ur.readingplan_id"
." and rps.id = ur.status_id";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
$nStepOrder = null;
$nCompletedStepID = $aRet['completed_step_id'];
if(!empty($nCompletedStepID))
{
$sSql = "SELECT ordering FROM readingplanstep"
." where id = " . $nCompletedStepID;
$nStepOrder = $this->getSingleData($sSql);
}
$aRet['completed_step_order'] = $nStepOrder;
}
return $aRet;
}
function getUserReadingPlanInfo($readingPlanUrl, $UserID, $request)
{
$aRet = array();
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$bHasUserStep = false;
if($UserID != '')
{
$aCompletedStepInfo = $this->getUserStepInfo($readingPlanUrl, $UserID);
// print_r($aCompletedStepInfo);
if(!empty($aCompletedStepInfo))
{
$sSql = "SELECT rs.name as step_name, rs.ordering as step_order"
." FROM readingplan r, (select rsa.id, case when rsl.name is null then rsa.name else rsl.name end as name, rsa.readingplan_id, rsa.startverse_id, rsa.endverse_id, rsa.ordering, case when rsl.text is null then rsa.text else rsl.text end as text from readingplanstep rsa left join (select * from readingplanstep_language where language_id = " .$nLanguageID . ") rsl on rsa.id = rsl.readingplanstep_id) rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." and rs.id not in (select readingplanstep_id from user_readingplanstep urs, user_readingplan ur where ur.user_id = " . $UserID . " and ur.id = urs.user_readingplan_id)"
." order by rs.ordering limit 1";
// echo $sSql . "<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
$aRet['step_complete'] = false;
$bHasUserStep = true;
}
else
{
$aRet['step_complete'] = true;
$bHasUserStep = true;
$sSql = "SELECT min(rs.ordering) as step_order"
." FROM readingplan r, readingplanstep rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id";
$nStepOrder = $this->getSingleData($sSql);
$aRet['step_order'] = $nStepOrder;
}
$aRet['current_status'] = $aCompletedStepInfo['current_status'];
$aRet['reminder_option_id'] = $aCompletedStepInfo['reminder_option_id'];
}
}
if(!$bHasUserStep)
{
$sSql = "SELECT min(rs.ordering) as step_order"
." FROM readingplan r, readingplanstep rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id";
// echo $sSql . "<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
}
}
return $aRet;
}
function getReadingPlanStepInfoForBible($readingPlanUrl, $stepOrder, $request)
{
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT rs.id, rs.name, rs.text, rs.closing_text, rs.image_file, rs.image_title, rs.video_link, rs.youtube_id, rs.video_notice, ";
$sSql .= "case when rl.name is null then r.name else rl.name end";
$sSql .= " as plan_title, rs.ordering as step_order, cb1.id as book_id, cc1.ordering as start_chapter_order, cv1.ordering as start_verse_order, cc2.ordering as end_chapter_order, cv2.ordering as end_verse_order FROM canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbiblebook cb1, canonicalbibleverse cv2, canonicalbiblechapter cc2, ";
$sSql .= " (select rsa.id, case when rsl.name is null then rsa.name else rsl.name end as name, rsa.readingplan_id, rsa.startverse_id, rsa.endverse_id, rsa.ordering, case when rsl.text is null then rsa.text else rsl.text end as text, case when rsl.closing_text is null then rsa.closing_text else rsl.closing_text end as closing_text, i.file as image_file, i.title as image_title, e.location as video_link, e.youtube_id, e.notice as video_notice from readingplanstep rsa left join (select * from readingplanstep_language where language_id = " .$nLanguageID . ") rsl on rsa.id = rsl.readingplanstep_id left join image i on rsa.image_id = i.id left join (select * from embed where visibilitydefault) e on rsa.embed_id = e.id) rs, ";
$sSql .= " readingplan r left join (select * from readingplan_language where language_id = " .$nLanguageID . ") rl on r.id = rl.readingplan_id ";
$sSql .= " where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." and rs.ordering = " . $stepOrder
." and cv1.id = rs.startverse_id and cv1.chapter_id = cc1.id"
." and cb1.id = cc1.book_id"
." and cv2.id = rs.endverse_id and cv2.chapter_id = cc2.id";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getDefaultBibleTranslation($cbook_id, $request)
{
$sBibleStr = '';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
{
$sBibleStr = '(case when t.id = ' . $nBibleTransID . ' then 1 else case when t.id = ' . $this->DEFAULT_BIBLE_TRANSLATION_ID . ' then 2 else case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 3 else 4 end end end)';
}
else
{
$locale = $request->getLocale();
$sBibleStr = "(case when t.language_id in (select id from language where shortcode = '" . trim(str_replace("'", "''", $locale)) . "') then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end)";
}
$sSql = "select t.url, " . $sBibleStr . " as priority_order from bibletranslation t, biblebook b where b.canonicalization_id = " . $cbook_id . " and b.translation_id = t.id order by priority_order, t.languagedefault desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getNextReadingPlanStep($readingPlanUrl, $stepOrder, $request)
{
$nUserID = $request->getSession()->get("user_id");
$sSql = "select id from readingplan where url = " . $this->dbstr($readingPlanUrl);
// echo $sSql."<br>";
$nReadingPlanID = $this->getSingleData($sSql);
$sSql = "SELECT r.url as plan_url, rs.ordering as step_order"
." FROM readingplan r, readingplanstep rs"
." where r.id = " . $nReadingPlanID
." and r.id = rs.readingplan_id"
." and rs.ordering > " . $stepOrder
." order by rs.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
else
{
if(!empty($nUserID))
{
// On the last step: check if there are any uncompleted steps
$sSql = "select " . $this->dbstr($readingPlanUrl) . " as plan_url, ordering as step_order from readingplanstep where readingplan_id = " . $nReadingPlanID . " and id not in (SELECT readingplanstep_id FROM user_readingplanstep urs, user_readingplan ur"
." where ur.user_id = " . $nUserID
." and readingplan_id = " . $nReadingPlanID . " and ur.id = urs.user_readingplan_id) order by ordering";
// echo $sSql . "<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
}
}
return $aRet;
}
function getPreviousReadingPlanStep($readingPlanUrl, $stepOrder)
{
$sSql = "SELECT r.url as plan_url, rs.ordering as step_order"
." FROM readingplan r, readingplanstep rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." and rs.ordering < " . $stepOrder
." order by rs.ordering desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getReadingPlanStepList($readingPlanUrl, $bibleTranslationUrl)
{
$sSql = "SELECT rs.ordering as step_order, b.name || ' ' || cc1.ordering || ':' || cv1.ordering || case when cc1.ordering <> cc2.ordering or cv1.ordering <> cv2.ordering then '-' else '' end || case when cc1.ordering <> cc2.ordering then cc2.ordering || ':' else '' end || case when cc1.ordering <> cc2.ordering or cv1.ordering <> cv2.ordering then cv2.ordering || '' else '' end as book_str FROM readingplan r, readingplanstep rs, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbiblebook cb1, canonicalbibleverse cv2, canonicalbiblechapter cc2, biblebook b, bibletranslation bt"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and bt.url = '" . trim(str_replace("'", "''", $bibleTranslationUrl)) . "'"
." and r.id = rs.readingplan_id"
." and cv1.id = rs.startverse_id and cv1.chapter_id = cc1.id"
." and cb1.id = cc1.book_id"
." and cv2.id = rs.endverse_id and cv2.chapter_id = cc2.id"
." and b.canonicalization_id = cb1.id and b.translation_id = bt.id"
." order by rs.ordering";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getStepCompletedPercentage($readingPlanUrl, $request)
{
$nRet = 0;
$nUserID = $request->getSession()->get("user_id");
if(empty($nUserID))
{
return $nRet;
}
$sSql = "SELECT CAST(count(distinct us.id) AS FLOAT) / CAST(count(rs.id) AS FLOAT) * 100 as completed_percentage"
." FROM readingplan r, readingplanstep rs left join (select urs.* from user_readingplanstep urs, user_readingplan ur where ur.user_id = " . $nUserID . " and ur.id = urs.user_readingplan_id) us on rs.id = us.readingplanstep_id"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id";
// echo $sSql . "<br>";
$nRet = 0;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = round(current($aTemp[0]));
return $nRet;
}
function getReadingPlanFirstStepOrder($readingPlanUrl)
{
$sSql = "SELECT min(rs.ordering) as first_order"
." FROM readingplan r, readingplanstep rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id";
// echo $sSql . "<br>";
$nRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$nRet = current($aTemp[0]);
return $nRet;
}
function getReadingPlanOrderInfo($readingPlanUrl)
{
$sSql = "SELECT min(rs.ordering) as first_order, max(rs.ordering) as last_order"
." FROM readingplan r, readingplanstep rs"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
}
return $aRet;
}
function IsReadingPlanStepCompleted($vStepID, $request)
{
$nUserID = $this->getCurrUserID($request);
if(empty($nUserID))
{
return false;
}
$sSql = "SELECT urs.id FROM user_readingplanstep urs, user_readingplan ur"
." where ur.user_id = " . $nUserID
." and urs.readingplanstep_id = " . $vStepID
." and ur.id = urs.user_readingplan_id";
// echo $sSql . "<br>";
$nID = $this->getSingleData($sSql);
return (!empty($nID));
}
function IsLastReadingPlanStepUncompleted($vStepID, $request)
{
$bRet = false;
$nUserID = $this->getCurrUserID($request);
if(empty($nUserID))
{
return $bRet;
}
$sSql = "select readingplan_id from readingplanstep where id = " . $vStepID;
// echo $sSql."<br>";
$nReadingPlanID = $this->getSingleData($sSql);
$sSql = "select id from readingplanstep where readingplan_id = " . $nReadingPlanID . " and id not in (SELECT urs.readingplanstep_id FROM user_readingplanstep urs, user_readingplan ur"
." where ur.user_id = " . $nUserID
." and ur.readingplan_id = " . $nReadingPlanID . " and ur.id = urs.user_readingplan_id)";
// echo $sSql . "<br>";
$aData = $this->getListData($sSql);
$aID = array();
foreach($aData as $d)
{
$aID[] = $d['id'];
}
if(count($aID) === 1 && in_array($vStepID, $aID))
{
$bRet = true;
}
return $bRet;
}
function getTranslationInfo($workUrl,$locale,$passageId)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT t.url as translation_url, d.url as division_url, d.ordering as division_order, p.ordering as passage_order,"
. " case when t.islanguagedefault then 1 else 2 end as lang_default_order"
." FROM expositiontranslation t join expositionwork w on t.work_id = w.id"
." join expositiondivision d on d.work_id = w.id"
." join expositionpassage p on p.division_id = d.id"
." join expositiontranslationtext tt on tt.translation_id = t.id"
." join expositiontextunit tu on tt.placement_id = tu.id and tu.passage_id = p.id"
." WHERE w.url = '" . trim(str_replace("'", "''", $workUrl)) . "'";
if($passageId != null)
$sSql .= " and p.id = " . $passageId;
$sSql .= " AND t.language_id = " . $nLanguageID
." AND t.ispublic AND w.ispublic and t.url is not null and t.url <> ''"
." ORDER BY division_order, passage_order, lang_default_order limit 1";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
}
if(empty($aRet))
{
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 1 else';
$sLangStr .= ' 2 end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "SELECT t.url as translation_url, d.url as division_url, d.ordering as division_order, p.ordering as passage_order," . $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
." FROM expositiontranslation t join expositionwork w on t.work_id = w.id"
." join expositiondivision d on d.work_id = w.id"
." join expositionpassage p on p.division_id = d.id"
." join expositiontranslationtext tt on tt.translation_id = t.id"
." join expositiontextunit tu on tt.placement_id = tu.id and tu.passage_id = p.id"
." WHERE w.url = '" . trim(str_replace("'", "''", $workUrl)) . "'"
." AND t.ispublic AND w.ispublic and t.url is not null and t.url <> ''"
." ORDER BY division_order, passage_order, language_order, lang_default_order limit 1";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aRet = $aTemp[0];
}
}
return $aRet;
}
function getNextBookID($book_id, $booklist)
{
$nRet = null;
$bHasBook = false;
foreach($booklist as $book) {
if($bHasBook)
{
$nRet = $book['id'];
break;
}
if($book['id'] == $book_id)
$bHasBook = true;
}
return $nRet;
}
function getPreviousBookID($book_id, $booklist)
{
$nRet = null;
foreach($booklist as $book) {
if($book['id'] == $book_id)
{
break;
}
$nRet = $book['id'];
}
return $nRet;
}
function setCurrentUrl($request)
{
$sUrl = $_SERVER['REQUEST_URI'];
$request->getSession()->set("current_page_url", $sUrl);
$request->getSession()->set("from_login", "");
}
function getCurrentUrl($request)
{
$sUrl = $request->getSession()->get('current_page_url');
if(strpos($sUrl, "exposition/translation") !== false)
{
//$sUrl = trim($sUrl, "/");
}
// Fix the cache issue on bible chapter pages
if(strpos($sUrl, "bible/") !== false && strpos($sUrl, "/stories/") === false && strpos($sUrl, "/story/") === false)
{
if(strpos($sUrl, "?/") !== false)
{
$sUrl .= "&";
}
else
{
$sUrl .= "?";
}
$sUrl .= date("YmdHis");
}
return $sUrl;
}
function goToLoginPage($request)
{
$request->getSession()->set("from_login", '1');
return $this->renderView('login', array());
}
function getCurrUserID($request)
{
return $request->getSession()->get('user_id');
}
function getWorkPassageScanImageFileName($passage_id)
{
$aRet = array();
$sSql = "select distinct image_filename from passage_scanimage where passage_id=".$passage_id." order by image_filename";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getPreviousPassageWithScan($passage_id)
{
$sSql = "SELECT p.id"
." FROM expositionpassage p, expositiondivision d, passage_scanimage ps"
." WHERE d.work_id in (select d1.work_id from expositionpassage p1, expositiondivision d1 where p1.id = " . $passage_id . " and p1.division_id = d1.id) and p.division_id = d.id and p.ordering < (select p1.ordering from expositionpassage p1 where p1.id = " . $passage_id . ") and ps.passage_id = p.id and ps.image_filename not in (select image_filename from passage_scanimage where passage_id = " . $passage_id . ") and ps.section_num < 2 order by p.ordering desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getNextPassageWithScan($passage_id)
{
$sSql = "SELECT p.id"
." FROM expositionpassage p, expositiondivision d, passage_scanimage ps"
." WHERE d.work_id in (select d1.work_id from expositionpassage p1, expositiondivision d1 where p1.id = " . $passage_id . " and p1.division_id = d1.id) and p.division_id = d.id and p.ordering > (select p1.ordering from expositionpassage p1 where p1.id = " . $passage_id . ") and ps.passage_id = p.id and ps.image_filename not in (select image_filename from passage_scanimage where passage_id = " . $passage_id . ") and ps.section_num < 2 order by p.ordering limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getCurrentPassageForLatin($passage_id)
{
$sSql = "SELECT t.translatedtitle || ' ' || p.swedenborgsection as title"
." FROM expositionpassage p, expositiondivision d, expositiontranslation t"
." WHERE p.id = " . $passage_id . " and p.division_id = d.id and t.work_id = d.work_id and t.language_id = " . $this->LANGUAGE_ID_LATIN . " order by islanguagedefault desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getCommentaryUrl($storyId, $bibleTranslationUrl)
{
$sSql = "SELECT t.url, case when l.language_id is not null then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order"
." FROM story_work sw, expositionwork w, expositiontranslation t"
." left join (select language_id from bibletranslation where url='". trim(str_replace("'", "''", $bibleTranslationUrl)) . "') as l on t.language_id = l.language_id"
." WHERE sw.story_id = " . $storyId . " and t.work_id = sw.work_id and t.work_id = w.id and w.ispublic AND t.ispublic order by lang_order, sw.ordering, t.islanguagedefault desc limit 1";
// echo $sSql."<br>";
$sRet = '';
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = current($aTemp[0]);
return $sRet;
}
function getPublicCommentaryUrl($storyId, $commentaryUrl)
{
$sSql = "SELECT t.url"
." FROM story_work sw, expositiontranslation t, expositionwork w"
." WHERE sw.story_id = " . $storyId
." and t.url = '" . trim(str_replace("'", "''", $commentaryUrl)) . "'"
." and t.work_id = sw.work_id and t.work_id = w.id and w.ispublic AND t.ispublic";
// echo $sSql."<br>";
$sRet = '';
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = current($aTemp[0]);
return $sRet;
}
function getFirstBibleTranslation($request)
{
$locale = $request->getLocale();
$cbook_id = 1;
$sBibleStr = '';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($locale == 'en')
{
if($nBibleTransID != '')
{
$sBibleStr = "(case when t.id = " . $nBibleTransID . " then 1 else case when t.id = " . $this->DEFAULT_BIBLE_TRANSLATION_ID . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 else 4 end end end)";
}
else
{
$sBibleStr = "(case when t.id = " . $this->DEFAULT_BIBLE_TRANSLATION_ID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end)";
}
$sSql = "select t.id, t.url, t.name, cb.url as book_url, " . $sBibleStr . " as priority_order from bibletranslation t, biblebook b, canonicalbiblebook cb where b.canonicalization_id = " . $cbook_id . " and cb.id = b.canonicalization_id and b.translation_id = t.id order by priority_order, t.languagedefault desc limit 1";
}
else
{
if($nBibleTransID != '')
{
$sBibleStr = "(case when t.id = " . $nBibleTransID . " then 1 else case when l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' then 2 else case when t.id = " . $this->DEFAULT_BIBLE_TRANSLATION_ID . " then 3 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 4 else 5 end end end end)";
}
else
{
$sBibleStr = "(case when l.shortcode = '" . trim(str_replace("'", "''", $locale)) . "' then 1 else case when t.id = " . $this->DEFAULT_BIBLE_TRANSLATION_ID . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 else 4 end end end)";
}
$sSql = "select t.id, t.url, t.name, cb.url as book_url, " . $sBibleStr . " as priority_order from bibletranslation t, biblebook b, canonicalbiblebook cb, language l where b.canonicalization_id = " . $cbook_id . " and cb.id = b.canonicalization_id and b.translation_id = t.id and t.language_id = l.id order by priority_order, t.languagedefault desc limit 1";
}
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getWorkRefForWork($passage_id, $translation_id, $language_id, $bCheck=false)
{
$aRet = array();
if(empty($passage_id))
return $aRet;
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = ' p.id in (';
$sFilter .= 'select ps.id
FROM refexposition re, textunit_combinedreference tc, expositiontextunit ts, expositiontextunit t1, expositiontextunit t2, expositionpassage ps, expositiondivision d1, expositiondivision da, expositionpassage p1, expositionpassage p2, expositionpassage pa'
.' WHERE ts.passage_id = ps.id and ts.id = tc.textunit_id and re.container_id = tc.combinedreference_id and t1.id = re.startlocation_id and t2.id = re.endlocation_id and t1.passage_id = p1.id and t2.passage_id = p2.id and p1.division_id = d1.id and pa.id = ' . $passage_id . ' and pa.division_id = da.id and da.work_id = d1.work_id and pa.ordering >= p1.ordering and pa.ordering <= p2.ordering) and w.swedenborgtype ';
$sFilter .= ' is not null';
$sSql1 = "select array_to_string(array_agg(DISTINCT id), ',') from (SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql1 .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql1 .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql1 .= " order by w.id, ";
$sSql1 .= " (case when t.id = " . $translation_id . " then 1 else 2 end),";
if($language_id != $nDefaultLanguageID)
{
$sSql1 .= " (case when t.language_id = " . $language_id . " then 1 else 2 end), ";
}
$sSql1 .= " t.islanguagedefault desc";
if($bCheck)
$sSql1 .= " limit 1";
$sSql1 .= " ) as t";
// echo $sSql1."<br>";
$aTrans = $this->getNativeQueryResults($sSql1);
$sTransIDs = '';
if(count($aTrans) > 0)
$sTransIDs = implode('', $aTrans[0]);
$bHasTrans = (!empty($sTransIDs));
$sSql = "";
if($bHasTrans)
{
$sSql = "SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as link_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id";
$sSql .= " and t.id in (" . $sTransIDs . ")";
$sSql .= " order by p.id, lang_order";
$sSql = "select * from (" . $sSql . ") as t2 order by work_order, title, ordering";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getGCEDRefForWork($passage_id, $bCheck=false)
{
if(empty($passage_id))
return array();
$sSql = $this->getGCEDSqlSelectStr();
$sSql .= " FROM resource_workref rw, resource r left join resourcetype rt on r.resourcetype_id = rt.id";
$sSql .= " WHERE rw.passage_id = " . $passage_id . " and r.id = rw.resource_id and r.publicfile is not null";
$sSql .= " and r.publish order by r.title";
if($bCheck)
$sSql .= " limit 1";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getGCEDRefForBible($aVerseInfo, $bChapter, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$aChapter = array();
$sVerseCChapterIDs = $aVerseInfo['CChapterIDs'];
/* foreach($verses as $verse) {
if($verse === null) {
continue;
}
foreach($verse->getCanonicalization() as $canonicalVerse) {
if($canonicalVerse != null)
{
$aChapter[] = $canonicalVerse->getChapter()->getId();
}
}
}
foreach($chapters as $chapter) {
if($chapter === null) {
continue;
}
$canonicalChapter = $chapter->getCanonicalization();
if($canonicalChapter != null)
{
$aChapter[] = $canonicalChapter->getId();
}
}*/
$sChapters = '';
if($bChapter)
$sChapters = $aVerseInfo['CChapterIDs'];
$aRet = array();
// $aChapter = array_unique($aChapter);
// $sChapters = implode (',', $aChapter);
if($sChapters != '' && $sVerseCChapterIDs != '')
$sChapters .= ','.$sVerseCChapterIDs;
else if($sVerseCChapterIDs != '')
$sChapters = $sVerseCChapterIDs;
if(!empty($sChapters))
{
$sSql = $this->getGCEDSqlSelectStr();
$sSql .= " FROM resource_bibleref rb, resource r left join resourcetype rt on r.resourcetype_id = rt.id";
$sSql .= " WHERE rb.chapter_id in (" . $sChapters . ") and r.id = rb.resource_id and r.publicfile is not null";
$sSql .= " and r.publish order by r.title";
if($bCheck)
$sSql .= " limit 1";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getGCEDSqlSelectStr()
{
$sSql = "SELECT distinct r.publicfile, r.title, r.searchsummary,";
$sSql .= " case when rt.description is null then '' else rt.description || ' | ' end";
$sSql .= " || case when r.minaudienceage is null and r.maxaudienceage is null then 'All Ages ' else";
$sSql .= " case when r.minaudienceage is not null or r.maxaudienceage is not null then 'Ages ' else '' end end";
$sSql .= " || case when r.minaudienceage is null and r.maxaudienceage is not null then 'up to ' else";
$sSql .= " case when r.minaudienceage is not null and r.maxaudienceage is null then 'over ' else '' end end";
$sSql .= " || case when r.minaudienceage is null then '' else r.minaudienceage || '' end";
$sSql .= " || case when r.minaudienceage is not null and r.maxaudienceage is not null then ' - ' else '' end";
$sSql .= " || case when r.maxaudienceage is null then '' else '' || r.maxaudienceage end";
$sSql .= " as otherinfo";
return $sSql;
}
function getRelatedPassages($passage_id, $language_id, $bCheck=false)
{
if(empty($passage_id))
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = ' p.id in (';
$sFilter .= 'select distinct passage_id2 from relatedpassage WHERE passage_id1 = ' . $passage_id
.' union select distinct passage_id1 from relatedpassage WHERE passage_id2 = ' . $passage_id . ')';
$sSql = "select * from (SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as link_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, t.islanguagedefault desc)) order by p.id, lang_order";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") as t2 order by work_order, title, ordering";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getVerseCrossRefs($aVerseInfo, $request, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$language_id = $aVerseInfo['LanguageID'];
$translation_id = $aVerseInfo['TranslationID'];
$sCVerseIDs = $aVerseInfo['CVerseIDs'];
if($sCVerseIDs == '')
return array();
if($language_id == '')
$language_id = $this->LANGUAGE_ID_ENGLISH;
$sFilter1 = '(';
$sFilter1 .= 'select distinct canonicalbibleverse_id1 as cvid from bibleversecross where canonicalbibleverse_id2 in (' . $sCVerseIDs . ')';
$sFilter1 .= ')';
$sFilter2 = '(';
$sFilter2 .= 'select distinct canonicalbibleverse_id2 as cvid from bibleversecross where canonicalbibleverse_id1 in (' . $sCVerseIDs . ')';
$sFilter2 .= ')';
$sBibleStr = '(';
/* $nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
$sBibleStr .= 'case when trans.id = ' . $nBibleTransID . ' then 1 else '; */
if($translation_id != '')
$sBibleStr .= 'case when trans.id = ' . $translation_id . ' then 2 else ';
if($language_id != '')
$sBibleStr .= 'case when trans.language_id = ' . $language_id . ' then 3 else ';
if($language_id != $this->LANGUAGE_ID_ENGLISH)
$sBibleStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 4 else ';
$sBibleStr .= ' 5 ';
if($language_id != $this->LANGUAGE_ID_ENGLISH)
$sBibleStr .= ' end ';
// if($nBibleTransID != '')
// $sBibleStr .= ' end';
if($translation_id != '')
$sBibleStr .= ' end';
if($language_id != '')
$sBibleStr .= ' end';
$sBibleStr .= ')';
$sSql = "select t.*, v2.id as ref_vid, b2.name as ref_book, c2.ordering as ref_chapternum, v2.indexdisplay as ref_versenum from (";
$sSql .= "select * from (SELECT distinct on (cv.id, cf.canonicalbibleverse_id2) b.name as book_name, cc.ordering as chapter_num, cv.ordering as verse_num, cc.book_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc.ordering || '_' || cv.ordering as ref_column_spec, " . $sBibleStr . " as priority_order, trans.url as translation_url, cb.url as book_url, v.content as verse_text, cf.canonicalbibleverse_id2 as refcvid"
." FROM canonicalbibleverse cv, canonicalbiblechapter cc, canonicalbiblebook cb, (select * from canonicalbibleverse_bibleverse where canonicalbibleverse_id in (" . $sFilter1 . ")) as cbb, (select canonicalbibleverse_id1, canonicalbibleverse_id2 from bibleversecross where canonicalbibleverse_id2 in (" . $sCVerseIDs . ")) as cf, bibleverse v, biblechapter c, biblebook b, bibletranslation trans"
." WHERE cv.id = cbb.canonicalbibleverse_id and cv.chapter_id = cc.id and cc.book_id = cb.id and v.id = cbb.bibleverse_id and v.chapter_id = c.id and c.book_id = b.id and cc.ordering=c.ordering and b.translation_id = trans.id and trans.enabled and cf.canonicalbibleverse_id1 = cv.id order by cv.id, cf.canonicalbibleverse_id2, priority_order asc, trans.languagedefault desc) as t1";
$sSql .= " union ";
$sSql .= "select * from (SELECT distinct on (cv.id, cf.canonicalbibleverse_id1) b.name as book_name, cc.ordering as chapter_num, cv.ordering as verse_num, cc.book_id, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc.ordering || '_' || cv.ordering as ref_column_spec, " . $sBibleStr . " as priority_order, trans.url as translation_url, cb.url as book_url, v.content as verse_text, cf.canonicalbibleverse_id1 as refcvid"
." FROM canonicalbibleverse cv, canonicalbiblechapter cc, canonicalbiblebook cb, (select * from canonicalbibleverse_bibleverse where canonicalbibleverse_id in (" . $sFilter2 . ")) as cbb, (select canonicalbibleverse_id1, canonicalbibleverse_id2 from bibleversecross where canonicalbibleverse_id1 in (" . $sCVerseIDs . ")) as cf, bibleverse v, biblechapter c, biblebook b, bibletranslation trans"
." WHERE cv.id = cbb.canonicalbibleverse_id and cv.chapter_id = cc.id and cc.book_id = cb.id and v.id = cbb.bibleverse_id and v.chapter_id = c.id and c.book_id = b.id and cc.ordering=c.ordering and b.translation_id = trans.id and trans.enabled and cf.canonicalbibleverse_id2 = cv.id order by cv.id, cf.canonicalbibleverse_id1, priority_order asc, trans.languagedefault desc) as t2";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") as t, (select * from canonicalbibleverse_bibleverse where canonicalbibleverse_id in (" . $sCVerseIDs . ")) as cbb2, bibleverse v2, biblechapter c2, biblebook b2 where t.refcvid = cbb2.canonicalbibleverse_id and v2.id = cbb2.bibleverse_id and v2.chapter_id = c2.id and c2.book_id = b2.id and b2.translation_id = " . $translation_id
." order by b2.id, c2.ordering, v2.ordering, book_id, chapter_num, verse_num";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleWordExplanations($aVerseInfo, $request, $row_num = 0, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$canon = array();
$nulls = array();
$verseLanguage = $aVerseInfo['LanguageID'];
$sCVerseIDs = $aVerseInfo['CVerseIDs'];
$sVerseIDs = $aVerseInfo['VerseIDs'];
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
if($sCVerseIDs == '') {
return null;
}
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH;
$sTransIDs = $this->getExplanationTransIDs($sVerseIDs, $verseLanguage, $nDefaultLanguageID);
if(empty($sTransIDs))
return array();
$sBorderStr = $this->getBoundaryStrForSql($verseLanguage);
$sTermTable = "(select * from (SELECT distinct on (lower(t.text)) cl.explanation_id, t.id as term_id, t.text as term_text, bv.ordering, POSITION(lower(t.text) in lower(bv.content)) as term_order, vc.concept_id"
." FROM verse_concept vc, term_concept tc, term t, (select cl1.*, trans1.id as explanation_id from concept_work cl1, expositiontranslation trans1 where trans1.id in (" . $sTransIDs . ") and cl1.work_id = trans1.work_id) as cl, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cbb, bibleverse bv"
." WHERE cbb.canonicalbibleverse_id = vc.verse_id and tc.concept_id = vc.concept_id and t.id = tc.term_id and cl.concept_id = tc.concept_id and t.language_id = " . $verseLanguage;
$sTermTable .= " and cbb.bibleverse_id = bv.id and case when t.text is not null and (bv.content ~* ('" . $sBorderStr . "' || " . $this->EscapeTextForRegex("t.text") . " || '" . $sBorderStr . "')) then true else false end order by lower(t.text), bv.ordering, t.id, cbb.bibleverse_id desc) as t1 order by ordering) as t2";
// $sFilter = " t.id in (" . $sTransIDs . ")";
$sTransTable = " (SELECT tt.translation_id, string_agg(tt.text, ' ' order by tu.ordering) as text FROM (select * from expositiontranslationtext where translation_id in (" . $sTransIDs . ")) as tt, expositiontextunit tu where tt.placement_id = tu.id group by tt.translation_id) as trans";
$sSql = "select * from (SELECT distinct on (p.id, t2.term_id) p.id, t2.term_text, trans.text as trans_text, t2.term_order, array_to_string((regexp_split_to_array(trans.text, '\s+'))[1:25], ' ') as trans_excerpt, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as ref_column_spec, t.url as translation_url, d.url as division_url, t2.ordering as verse_order, p.ordering, t2.concept_id, t2.term_id, t.work_id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d, " . $sTermTable . ", " . $sTransTable
." WHERE trans.translation_id = t.id and t.id = t2.explanation_id and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and w.ispublic AND t.ispublic";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") as t3 order by verse_order, term_order";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$this->setWordExplanationSession($aRet, $request, $row_num);
return $aRet;
}
function getBiblioForTranslation($translation_id)
{
$sSql = "SELECT credit, sourcelink from bibliographicdata b, translation_bibliographicdata tb"
." WHERE tb.translation_id = ".$translation_id . " and tb.bibliographicdata_id = b.id and sourcelink is not null and credit is not null";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$aRet = $aRet[0];
return $aRet;
}
function isOnMobile()
{
$bRet = false;
$useragent=@$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/mobile|android|iPad|(bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent))
{
$bRet = true;
}
return $bRet;
}
function isIPad()
{
$bRet = false;
$useragent=@$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/iPad/i',$useragent))
{
$bRet = true;
}
return $bRet;
}
function checkIfOnMobile($request)
{
$request->getSession()->set('is_on_mobile', $this->isOnMobile());
}
function OnMobile($request)
{
return $request->getSession()->get('is_on_mobile');
}
function setWordExplanationSession($aWordExplanation, $request, $row_num)
{
$aPrev = array();
$aNext = array();
$sPrev = "";
$nPrevWorkID = "";
// print_r($aWordExplanation);
$aTempWork = array();
foreach ($aWordExplanation as $wep)
{
$sTransUrl = $wep['translation_url'];
$nWorkID = $wep['work_id'];
if(!in_array($nWorkID, $aTempWork))
{
$aTempWork[] = $nWorkID;
if($sPrev != '')
{
$aPrev[$nWorkID] = $this->MULTI_URL_INDICATOR_EXPLANATION . '_' . $sPrev;
$aNext[$nPrevWorkID] = $this->MULTI_URL_INDICATOR_EXPLANATION . '_' . $sTransUrl;
}
$sPrev = $sTransUrl;
$nPrevWorkID = $nWorkID;
}
}
if($row_num > 1)
{
$request->getSession()->set("prev_word_explanation".$row_num, $aPrev);
$request->getSession()->set("next_word_explanation".$row_num, $aNext);
}
else
{
$request->getSession()->set("prev_word_explanation", $aPrev);
$request->getSession()->set("next_word_explanation", $aNext);
}
}
function getVerseInfo($verses)
{
$aRet = array();
$sVerseIDs = '';
$sCVerseIDs = '';
$verseLanguage = '';
$translation_id = '';
$book_id = '';
$nChapterID = '';
$sCChapterIDs = '';
// $aChapter = array();
$nChapterIndex = '';
foreach($verses as $verse) {
if($verse === null) {
continue;
}
$nVerseID = $verse->getId();
if($sVerseIDs != '')
$sVerseIDs .= ',';
$sVerseIDs .= $nVerseID;
/* foreach($verse->getCanonicalization() as $canonicalVerse) {
if($canonicalVerse != null)
{
if($sCVerseIDs != '')
$sCVerseIDs .= ',';
$sCVerseIDs .= $canonicalVerse->getId();
$chapter = $canonicalVerse->getChapter();
$aChapter[] = $chapter->getId();
if($nChapterIndex == '')
$nChapterIndex = $chapter->getOrdering();
}
} */
if($verseLanguage == '')
{
$aVerseData = $this->getVerseData($nVerseID);
$verseLanguage = $aVerseData['language_id'];
$translation_id = $aVerseData['translation_id'];
$book_id = $aVerseData['book_id'];
$nChapterID = $aVerseData['chapter_id'];
}
}
if(empty($sVerseIDs))
return $aRet;
$aCVerse = $this->getCVerseInfo($sVerseIDs);
if(!empty($aCVerse))
{
$sCVerseIDs = $aCVerse['cverse_ids'];
$sCChapterIDs = $aCVerse['cchapter_ids'];
$nChapterIndex = $aCVerse['first_chapter_index'];
}
$aRet['VerseIDs'] = $sVerseIDs;
$aRet['CVerseIDs'] = $sCVerseIDs;
$aRet['LanguageID'] = $verseLanguage;
// $aChapter = array_unique($aChapter);
// $sCChapterIDs = implode (',', $aChapter);
$aRet['CChapterIDs'] = $sCChapterIDs;
$aRet['TranslationID'] = $translation_id;
$aRet['BookID'] = $book_id;
$aRet['FirstChapterIndex'] = $nChapterIndex;
$aRet['FirstChapterID'] = $nChapterID;
return $aRet;
}
function getExplainChapterTranslation($vCChapterID, $vUserID, $vTranslationUrl)
{
$nRet = -1;
if($vUserID != '')
{
$sSql = "select \"UserLevelID\" from \"user\" where \"UserID\" = " . $vUserID . " and \"UserLevelID\" in (-1, 1, 2, 3, 5, 6)";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) <= 0)
return -1;
else
$nUserLevelID = current($aTemp[0]);
$sFilter = "";
if($nUserLevelID == $this->USER_LEVEL_ID_EXPLANATION_AUTHOR)
$sFilter .= " and (user_id = " . $vUserID . " or user_id is null)";
$sSql = "select translation_id, case when language_id in (select language_id from bibletranslation where url = '" . $vTranslationUrl . "') then 1 else case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order from vchapterexplanationforchecking where id = " . $vCChapterID . $sFilter . " order by lang_order limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$nRet = $aTemp[0]['translation_id'];
}
}
return $nRet;
}
function getExplanationTransIDs($sVerseIDs, $verseLanguage, $nDefaultLanguageID)
{
$sRet = '';
$sBorderStr = $this->getBoundaryStrForSql($verseLanguage);
$sSql = "SELECT array_to_string(array_agg(distinct trans.id), ',') as trans_ids, array_to_string(array_agg(distinct t.id), ',') as term_ids "
." FROM verse_concept vc, term_concept tc, term t, concept_work cl, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cbb, bibleverse bv, expositiontranslation trans"
." WHERE cbb.canonicalbibleverse_id = vc.verse_id and tc.concept_id = vc.concept_id and t.id = tc.term_id and cl.concept_id = tc.concept_id and t.language_id = " . $verseLanguage;
$sSql .= " and cl.work_id = trans.work_id and trans.language_id = t.language_id and cbb.bibleverse_id = bv.id and case when t.text is not null and (bv.content ~* ('" . $sBorderStr . "' || " . $this->EscapeTextForRegex("t.text") . " || '" . $sBorderStr . "')) then true else false end";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
$sTransIDs1 = '';
$sTermIDs1 = '';
if(count($aTemp) > 0)
{
$sTransIDs1 = $aTemp[0]['trans_ids'];
$sTermIDs1 = $aTemp[0]['term_ids'];
}
if($verseLanguage == $nDefaultLanguageID)
{
$sRet = $sTransIDs1;
}
else
{
$sSql = "SELECT array_to_string(array_agg(distinct trans.id), ',')"
." FROM verse_concept vc, term_concept tc, term t, concept_work cl, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $sVerseIDs . ")) as cbb, bibleverse bv, expositiontranslation trans"
." WHERE cbb.canonicalbibleverse_id = vc.verse_id and tc.concept_id = vc.concept_id and t.id = tc.term_id and cl.concept_id = tc.concept_id and t.language_id = " . $verseLanguage;
$sSql .= " and cl.work_id = trans.work_id and cbb.bibleverse_id = bv.id and case when t.text is not null and (bv.content ~* ('" . $sBorderStr . "' || " . $this->EscapeTextForRegex("t.text") . " || '" . $sBorderStr . "')) then true else false end";
if($sTermIDs1 != '')
$sSql .= " and t.id not in (" . $sTermIDs1 . ")";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
$sTransIDs2 = '';
if(count($aTemp) > 0)
{
$sTransIDs2 = implode(",", $aTemp[0]);
}
if($sTransIDs1 != '' && $sTransIDs2 != '')
$sRet = $sTransIDs1 . ',' . $sTransIDs2;
else
{
if($sTransIDs1 != '')
$sRet = $sTransIDs1;
else if($sTransIDs2 != '')
$sRet = $sTransIDs2;
}
}
return $sRet;
}
function getVerseContentWithConceptLinksForAllTrans($CVerseIDs)
{
$aRet = array();
$aTranslation = $this->getTranslationsForVerse($CVerseIDs);
foreach($aTranslation as $trans)
{
$nTransID = $trans['id'];
$nVerseID = $trans['verse_id'];
$verses = $this->bibleData('Verse')->findBy(array('id' => $nVerseID));
$aRet[$nTransID]['verse'] = current($verses);
// $aRet[$nTransID]['verse_concept'] = $verse_concept;
}
return $aRet;
}
function getTranslationsForVerse($CVerseIDs)
{
$aRet = array();
$sSql = "SELECT bt.id, v.id as verse_id from bibleverse v, canonicalbibleverse cv, (select * from canonicalbibleverse_bibleverse where canonicalbibleverse_id in (" . $CVerseIDs . ")) as cbb, bibletranslation bt, biblechapter c, biblebook b"
." WHERE cv.id = cbb.canonicalbibleverse_id and v.id = cbb.bibleverse_id and v.chapter_id = c.id and b.id = c.book_id and b.translation_id=bt.id";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getAllBibleTranslationsForVerse($canonical_book_id, $chapter_index, $verse_index)
{
$sSql = "SELECT t.id, t.name as translation_name, t.url, v.content as verse_content, (case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) as group_order, l.id as language_id, l.righttoleft, l.text_style_override"
." FROM bibletranslation t, biblebook b, biblechapter c, bibleverse v, language l"
." WHERE b.canonicalization_id = " . $canonical_book_id . " and t.id = b.translation_id and t.enabled and c.book_id = b.id and c.ordering = " . $chapter_index . " and v.chapter_id = c.id and v.indexdisplay = '" . $verse_index . "' and t.language_id = l.id";
$sSql .= " order by group_order, t.name";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
$nTempLen = count($aTemp);
$sPopupStartMark = "~ppss~";
$sPopupEndMark = "~ppee~";
for($i=0;$i<$nTempLen;$i++)
{
$text = $aTemp[$i]['verse_content'];
if(strpos($text,$sPopupStartMark) !== false)
{
$text = str_replace($sPopupStartMark, '<sup><a data-toggle="popover" style="cursor: pointer; color:#007bff;"data-content="', $text);
$text = str_replace($sPopupEndMark, '">*</a></sup>', $text);
$aTemp[$i]['verse_content'] = $text;
}
}
return $aTemp;
}
function checkConcept($translation_id)
{
$sSql = "select c.url from concept_work cl, concept c, expositionwork w, expositiontranslation t where t.id=".$translation_id . " and t.work_id = w.id and cl.work_id = w.id and c.id = cl.concept_id and w.ispublic and t.ispublic limit 1";
$sRet = '';
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
function checkUserBibleTrans($bookUrl, $chapterIndex, $verseIndex, $nUserTranslationID)
{
$sSql = "SELECT t.url FROM bibletranslation t, biblebook b, biblechapter c, bibleverse v, canonicalbiblebook cb"
." WHERE t.id = " . $nUserTranslationID . " and cb.url = '" . $bookUrl . "' and b.canonicalization_id = cb.id and t.id = b.translation_id and t.enabled and c.book_id = b.id and c.ordering = " . $chapterIndex . " and v.chapter_id = c.id and v.indexdisplay = '" . $verseIndex . "' limit 1";
// echo $sSql;
$sRet = '';
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
// We will add a Bible icon to the right of the speaker icon, if the current Swedenborg passage is
// a "main explanation" for a Bible chapter/verse. If the user clicks the Bible icon, it will open
// that "main-related" Bible chapter in a new column (desktop), or a new page (mobile UI).
function getRelatedBibleRefForWork($passage_id,$translation_id, $request)
{
$sBibleStr = '(';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
$sBibleStr .= 'case when trans.id = ' . $nBibleTransID . ' then 1 else ';
$sBibleStr .= 'case when trans.language_id in (select language_id from expositiontranslation where id = ' . $translation_id . ') then 2 else case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 3 else 4 end end';
if($nBibleTransID != '')
$sBibleStr .= ' end';
$sBibleStr .= ')';
$sSql = "SELECT '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc.ordering as link_spec, " . $sBibleStr . " as priority_order, cb.url as book_url, trans.url as trans_url, cc.ordering as chapter_order"
." FROM expositiontextunit t, canonicalbibleverse cv, canonicalbiblechapter cc, canonicalbiblebook cb, biblebook b, bibletranslation trans"
." WHERE t.passage_id = " . $passage_id . " and (cv.id in (select verse_id from verse_passage where passage_id = " . $passage_id . " and iscorepassage) or cc.id in (select chapter_id from chapter_passage where passage_id = " . $passage_id . ") ) and cv.chapter_id = cc.id and cc.book_id = cb.id and b.canonicalization_id = cb.id and b.translation_id = trans.id and trans.enabled order by priority_order asc, trans.languagedefault desc limit 1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getNextPrevConcept($concept_id)
{
$sSql = "SELECT c.id, c.url FROM concept c, concept_work cl where c.id = cl.concept_id"
." order by c.url";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$sPrev = "";
$sNext = "";
if(count($aTemp) > 0)
{
$bPass = false;
foreach ($aTemp as $c)
{
$nCurrID = $c['id'];
if($bPass)
{
$sNext = $c['url'];
break;
}
if($nCurrID == $concept_id)
$bPass = true;
if(!$bPass)
{
$sPrev = $c['url'];
}
}
}
$aRet['prev_concept_url'] = $sPrev;
$aRet['next_concept_url'] = $sNext;
return $aRet;
}
// Get PDF resources for bible chapter
function getCommentaryWithDocForBible($book, $chapter_order, $isCanonical=false)
{
if($isCanonical)
$cbook = $book;
else
$cbook = $book->getCanonicalization();
$nCBookId = $cbook->getId();
// get commentary from gced
$sLinkStart = '/bundles/ncbsw/resource/publicfile/';
$sSql = "SELECT r.title as name, ('" . $sLinkStart . "' || r.publicfile) as public_file, 1 as ordering from resource r, resource_bibleref rb, canonicalbiblechapter cc"
." WHERE rb.book_id = " . $nCBookId . " and r.publicfile like 'dbsn%' and r.id = rb.resource_id and cc.id = rb.chapter_id and cc.ordering = " . $chapter_order . " and r.publish";
// get commentary from ncbsw translations
$sLinkStart = '/bundles/ncbsw/translation/';
$sSql .= " union SELECT name, ('" . $sLinkStart . "' || public_file) as public_file, 2 as ordering from expositiontranslation"
." WHERE id in (select translation_id from expositiontranslationref where book_id = " . $nCBookId . ") and type_id = " . $this->TRANSLATION_TYPE_ID_FILE . " and ispublic and public_file is not null order by ordering, name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getExplainChapterID($vTranslationID, $vUserID)
{
$nRet = null;
if($vUserID != '')
{
$nUserLevelID = null;
$sSql = "select \"UserLevelID\" from \"user\" where \"UserID\" = " . $vUserID . " and \"UserLevelID\" in (-1, 1, 2, 3, 5, 6)";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) <= 0)
return null;
else
$nUserLevelID = current($aTemp[0]);
$sSql = "select id from vchapterexplanationforchecking where translation_id = " . $vTranslationID;
if($nUserLevelID == $this->USER_LEVEL_ID_EXPLANATION_AUTHOR)
$sSql .= " and (user_id = " . $vUserID . " or user_id is null)";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$nRet = $aTemp[0]['id'];
}
}
return $nRet;
}
function EscapeTextForRegex($vText)
{
$sRet = "replace(replace(replace(replace(" . $vText . ", ')', '\)'), '(', '\('), ']', '\]'), '[', '\[')";
return $sRet;
}
// Convert time like 00:12:22 to seconds
function convertTimeToSeconds($vTime)
{
sscanf($vTime, "%d:%d:%d", $hours, $minutes, $seconds);
$sRet = $hours * 3600 + $minutes * 60 + $seconds;
return $sRet;
}
function getOTLERefForBible($aVerseInfo, $bChapter, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
// 1/5/2022: Change to show all videos inc. non-OTLE.
$aChapter = array();
$sVerseCChapterIDs = $aVerseInfo['CChapterIDs'];
$sCVerseIDs = $aVerseInfo['CVerseIDs'];
/* foreach($chapters as $chapter) {
if($chapter === null) {
continue;
}
$canonicalChapter = $chapter->getCanonicalization();
if($canonicalChapter != null)
{
$aChapter[] = $canonicalChapter->getId();
}
}*/
// $sChapters = '';
// if($bChapter)
$sChapters = $aVerseInfo['CChapterIDs'];
$aRet = array();
$aRet['otle_nonmusic'] = null;
$aRet['otle_music'] = null;
/* if($sChapters != '' && $sVerseCChapterIDs != '')
$sChapters .= ','.$sVerseCChapterIDs;
else if($sVerseCChapterIDs != '')
$sChapters = $sVerseCChapterIDs; */
if(!empty($sChapters))
{
$aVerseChapter = $this->getVerseChapterInfo($sCVerseIDs);
$sSql = "select * from (" . $this->getOTLESqlSelectStr() . ", cc.ordering as chapter_order, cb1.ordering as start_verse, cb2.ordering as end_verse, case when e.youtube_id is null then 2 else 1 end as video_order1";
$sSql .= " FROM show s, embed e, show_bibleref sr left join canonicalbibleverse as cb1 on from_verse_id = cb1.id ";
$sSql .= " left join canonicalbibleverse as cb2 on to_verse_id = cb2.id ";
$sSql .= " left join canonicalbiblechapter as cc on sr.chapter_id = cc.id ";
$sSql .= " WHERE ( ";
if($bChapter)
$sSql .= " (sr.chapter_id in (" . $sChapters . "))";
else
{
$sSql .= " (sr.chapter_id in (" . $sChapters . ") and from_verse_id is null and to_verse_id is null)";
$sSql .= " or ( to_verse_id is null and sr.from_verse_id in (" . $sCVerseIDs . ") )";
if(!empty($aVerseChapter))
{
foreach($aVerseChapter as $vc)
{
$sSql .= " or ( to_verse_id is not null and sr.chapter_id = " . $vc['chapter_id'] . " and cb1.ordering <= " . $vc['min_order'] . " and cb2.ordering >= " . $vc['max_order'] . " )";
}
}
}
$sSql .= ") and s.id = sr.show_id and e.visibilitydefault";
// $sSql .= " and s.is_otle";
$sSql .= " and s.embed_id = e.id";
if($bCheck)
$sSql .= " limit 1";
// else
// $sSql .= " limit 5";
$sSql .= ") as t order by video_order1";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$aOTLE = array();
$aOTLE_Music = array();
foreach($aTemp as $temp)
{
if($temp['showtype_id'] == $this->OTLE_SHOW_TYPE_ID_MUSIC)
$aOTLE_Music[] = $temp;
else
$aOTLE[] = $temp;
}
$aRet['otle_nonmusic'] = $this->checkOTLELinks($aOTLE);
$aRet['otle_music'] = $this->checkOTLELinks($aOTLE_Music);
}
return $aRet;
}
function getOTLESqlSelectStr()
{
$sSql = "SELECT distinct on (s.id) s.id, s.title, e.description, e.youtube_id, sr.time, s.showtype_id, e.location, case when e.youtube_id is null then 2 else 1 end as video_order";
return $sSql;
}
function getVerseChapterInfo($CVerseIDs)
{
$sSql = "SELECT chapter_id, min(ordering) as min_order, max(ordering) as max_order from canonicalbibleverse"
." WHERE id in (".$CVerseIDs . ") group by chapter_id";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getOTLERefForWork($passage_id, $bCheck=false)
{
// 1/5/2022: Change to show all videos inc. non-OTLE.
if(empty($passage_id))
return array();
$sSql = $this->getOTLESqlSelectStr() . ", null as chapter_order, null as start_verse, null as end_verse";
$sSql .= " FROM show s, embed e, show_workref sr";
$sSql .= " WHERE sr.passage_id = " . $passage_id . " and s.id = sr.show_id and e.visibilitydefault";
// $sSql .= " and s.is_otle";
$sSql .= " and s.embed_id = e.id";
$sSql .= " and s.showtype_id <> " . $this->OTLE_SHOW_TYPE_ID_MUSIC;
$sSql .= " order by s.id desc, sr.time";
if($bCheck)
$sSql .= " limit 1";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$aRet = $this->checkOTLELinks($aRet);
return $aRet;
}
function checkOTLELinks($vRet)
{
if(empty($vRet))
return $vRet;
$nDelaySeconds = 5;
$nLen = count($vRet);
for($i=0;$i<$nLen;$i++)
{
$sLink = '';
$sYoutubeID = $vRet[$i]["youtube_id"];
$sSameLink = '';
$vRet[$i]["show_time_in_seconds"] = 0;
if(empty($sYoutubeID))
{
$sLink = $vRet[$i]["location"];
}
else
{
$sLink = "https://www.youtube.com/embed/" . $sYoutubeID;
$sTime = $vRet[$i]["time"];
if(!empty($sTime))
{
$sSeconds = $this->convertTimeToSeconds($sTime);
if(!empty($sSeconds))
{
$nShowTimeInSeconds = $sSeconds - $nDelaySeconds;
$sLink .= "?start=" . $nShowTimeInSeconds . "&autoplay=1";
$vRet[$i]["show_time_in_seconds"] = $nShowTimeInSeconds;
}
else
$sLink .= "?autoplay=1";
if(strpos($sTime, "00:") === 0)
$vRet[$i]["time"] = substr($sTime, 3);
}
else
{
$vRet[$i]["time"] = "00:00";
$sLink .= "?autoplay=1";
}
if($i > 0 && $sYoutubeID == $vRet[$i-1]["youtube_id"])
{
$vRet[$i-1]["same_youtube_id"] = $sYoutubeID;
$sSameLink = $sYoutubeID;
}
// if(empty($vRet[$i]["end_verse"]))
// $vRet[$i]["end_verse"] = $vRet[$i]["start_verse"];
if($i > 0 && $vRet[$i]["start_verse"] == $vRet[$i-1]["start_verse"] && $vRet[$i]["end_verse"] == $vRet[$i-1]["end_verse"])
{
$vRet[$i]["same_verse_range"] = true;
}
else
$vRet[$i]["same_verse_range"] = false;
if($i == 0 || ($i > 0 && $sYoutubeID != $vRet[$i-1]["youtube_id"]))
{
$vRet[$i]["diff_prev_youtube_id"] = true;
}
else
$vRet[$i]["diff_prev_youtube_id"] = false;
if($i == $nLen-1 || !($vRet[$i]["start_verse"] == $vRet[$i+1]["start_verse"] && $vRet[$i]["end_verse"] == $vRet[$i+1]["end_verse"]))
{
$vRet[$i]["diff_next_verse_range"] = true;
}
else
$vRet[$i]["diff_next_verse_range"] = false;
if($i == $nLen-1 || $vRet[$i]["youtube_id"] != $vRet[$i+1]["youtube_id"])
{
$vRet[$i]["diff_next_youtube_id"] = true;
}
else
$vRet[$i]["diff_next_youtube_id"] = false;
}
if(!empty($sLink))
$vRet[$i]["video_link"] = $sLink;
$vRet[$i]["same_youtube_id"] = $sSameLink;
}
return $vRet;
}
function getExplanationRefForWork($passage_id, $language_id, $bCheck=false)
{
$aRet = array();
if(empty($passage_id))
return $aRet;
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = ' p.id in (';
$sFilter .= 'select ps.id
FROM refexposition re, textunit_combinedreference tc, expositiontextunit ts, expositiontextunit t1, expositiontextunit t2, expositionpassage ps, expositiondivision d1, expositiondivision da, expositionpassage p1, expositionpassage p2, expositionpassage pa'
.' WHERE ts.passage_id = ps.id and ts.id = tc.textunit_id and re.container_id = tc.combinedreference_id and t1.id = re.startlocation_id and t2.id = re.endlocation_id and t1.passage_id = p1.id and t2.passage_id = p2.id and p1.division_id = d1.id and pa.id = ' . $passage_id . ' and pa.division_id = da.id and da.work_id = d1.work_id and pa.ordering >= p1.ordering and pa.ordering <= p2.ordering) and w.swedenborgtype ';
$sFilter .= ' is null';
$sSql1 = "select array_to_string(array_agg(DISTINCT id), ',') from (SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql1 .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql1 .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql1 .= " order by w.id, t.islanguagedefault desc";
if($bCheck)
$sSql1 .= " limit 1";
$sSql1 .= ") as t";
$aTrans = $this->getNativeQueryResults($sSql1);
$sTransIDs = '';
if(count($aTrans) > 0)
$sTransIDs = implode('', $aTrans[0]);
$bHasTrans = (!empty($sTransIDs));
$sSql = "";
if($bHasTrans)
{
$sSql = "SELECT distinct on (p.id) p.id, ec.category_id, t.translatedtitle AS title, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as link_spec, t.url as translation_url, d.url as division_url, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositiontranslation t, expositionpassage p, expositiondivision d, expositionwork w"
." join (select distinct on (wc.work_id) wc.work_id, c.id as category_id, c.name as category_name from work_category wc, expositioncategory c where wc.category_id = c.id and c.id in (12,13,14,41,42,46)) as ec on w.id = ec.work_id"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (";
$sSql .= $sTransIDs . ") order by p.id, lang_order";
}
/* if($language_id != $nDefaultLanguageID)
{
$sSql2 = "select array_to_string(array_agg(DISTINCT id), ',') from (SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql2 .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id = ".$nDefaultLanguageID. " and t.ispublic and w.ispublic";
$sSql2 .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql2 .= " order by w.id, t.islanguagedefault desc) as t";
$aTrans2 = $this->getNativeQueryResults($sSql2);
$sTransIDs2 = '';
if(count($aTrans2) > 0)
$sTransIDs2 = implode('', $aTrans2[0]);
$bHasTrans2 = (!empty($sTransIDs2));
if($bHasTrans2)
{
if($bHasTrans)
$sSql .= " union ";
$sSql .= " SELECT distinct on (p.id) p.id, ec.category_id, t.translatedtitle AS title, (case when p.swedenborgsection is not null then p.swedenborgsection else cast(p.ordering as text) end) as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as link_spec, t.url as translation_url, d.url as division_url"
." FROM expositiontranslation t, expositionpassage p, expositiondivision d, expositionwork w"
." join (select distinct on (wc.work_id) wc.work_id, c.id as category_id, c.name as category_name from work_category wc, expositioncategory c where wc.category_id = c.id and c.id in (12,13,14,41,42,46)) as ec on w.id = ec.work_id"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (" . $sTransIDs2 . ")";
if($bHasTrans)
{
$sSql .= " and p.id not in (";
$sSql .= "SELECT distinct p.id"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.id in (" . $sTransIDs . ")";
$sSql .= ")";
}
}
} */
if($sSql != "")
{
$sSql = "select t2.*, case when category_id = 46 then 1 else case when category_id = 13 then 2 else case when category_id = 14 then 3 else case when category_id = 42 then 4 else 5 end end end end as ordering from (" . $sSql . ") as t2 order by ordering, title";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getLangNoWordBoundary()
{
$aRet = array($this->LANGUAGE_ID_CHINESE, $this->LANGUAGE_ID_JAPANESE, $this->LANGUAGE_ID_THAI, $this->LANGUAGE_ID_BURMESE, $this->LANGUAGE_ID_MALAYALAM, $this->LANGUAGE_ID_GEORGIAN, $this->LANGUAGE_ID_AMHARIC, $this->LANGUAGE_ID_KOREAN);
return $aRet;
}
// Need to change the similar function on phpMaker site
function getBoundaryStrForSql($vLanguageID)
{
$aLangNoBoundary = $this->getLangNoWordBoundary();
$sBorderStr = "\y";
if(in_array($vLanguageID, $aLangNoBoundary))
$sBorderStr = "";
return $sBorderStr;
}
function getBoundaryStrForPhp($vLanguageID)
{
$aLangNoBoundary = $this->getLangNoWordBoundary();
$sBorderStr = '\b';
if(in_array($vLanguageID, $aLangNoBoundary))
$sBorderStr = '';
return $sBorderStr;
}
function getAvailableTranslantion($vBookUrl, $vDefaultTranslationID)
{
$sSql = "SELECT t.id, t.url FROM bibletranslation t, biblebook b, canonicalbiblebook cb"
." WHERE cb.url = '" . str_replace("'", "''", $vBookUrl) . "' and cb.id = b.canonicalization_id and t.id = b.translation_id order by (case when t.id = " . $vDefaultTranslationID . " then 1 else 2 end) limit 1";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet[0];
}
function getBibleTranslationCredit($translation_url)
{
$sSql = "SELECT credit from bibletranslation"
." WHERE url = '". str_replace("'", "''", $translation_url) . "'";
// echo $sSql."<br>";
$sRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = $aTemp[0]['credit'];
return $sRet;
}
function getExplanationRefForBible($aVerseInfo, $request, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$canon = array();
$nulls = array();
$verseLanguage = null;
$sVerseIDs = $aVerseInfo['CVerseIDs'];
$verseLanguage = $aVerseInfo['LanguageID'];
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
if($sVerseIDs == '') {
return null;
} else {
return $this->getExplanationRefForBibleInfo($verseLanguage, $sVerseIDs, $request, $bCheck);
}
}
function getExplanationRefForBibleInfo($language_id, $sVerseIDs, $request, $bCheck=false)
{
if($sVerseIDs == '')
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sFilter = " p.id in (SELECT DISTINCT passage_id FROM verse_passage"
." WHERE verse_id in (" . $sVerseIDs . "))";
$sFilter .= " and w.swedenborgtype is null and wc.category_id in (13,14,42)";
$nUILanguageID = $this->getUILanguageID($request);
$nPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$nBookLanguageID = $language_id;
$sLangOrder = "case when t.language_id = " . $nBookLanguageID . " then 1";
$sLangFilter = " and t.language_id in (" . $nBookLanguageID;
$sEndStr = "";
if($nBookLanguageID != $nUILanguageID)
{
$sLangOrder .= " else case when t.language_id = " . $nUILanguageID . " then 2";
$sEndStr .= " end ";
$sLangFilter .= "," . $nUILanguageID;
}
if(!empty($nPreferredLanguageID) && $nBookLanguageID != $nPreferredLanguageID && $nUILanguageID != $nPreferredLanguageID)
{
$sLangOrder .= ' else case when t.language_id = ' . $nPreferredLanguageID . ' then 3';
$sEndStr .= " end ";
$sLangFilter .= "," . $nPreferredLanguageID;
}
if($nBookLanguageID != $this->LANGUAGE_ID_ENGLISH && $nUILanguageID != $this->LANGUAGE_ID_ENGLISH && $nPreferredLanguageID != $this->LANGUAGE_ID_ENGLISH)
{
$sLangOrder .= " else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH ." then 4";
$sEndStr .= " end ";
$sLangFilter .= "," . $this->LANGUAGE_ID_ENGLISH;
}
$sLangOrder .= $sEndStr . " end";
$sLangFilter .= ")";
$sSql = "SELECT distinct on (p.id) p.id, wc.category_id, w.swedenborgtype, t.translatedtitle AS title, p.swedenborgsection as num, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as ref_column_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d, work_category wc"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and w.id = wc.work_id and p.division_id = d.id and wc.category_id <> " . $this->CATEGORY_ID_BIBLE_STUDY . " and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t, expositiontranslationtext tt, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id " . $sLangFilter. " and t.ispublic and w.ispublic and t.id = tt.translation_id and tt.text is not null and tt.text <> ''";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.id, " . $sLangOrder . ", t.islanguagedefault desc)";
if($bCheck)
$sSql .= " limit 1";
$sSql = "select t2.*, case when category_id = 46 then 1 else case when category_id = 13 then 2 else case when category_id = 14 then 3 else case when category_id = 42 then 4 else 5 end end end end as category_order from (" . $sSql . ") as t2 order by category_order, work_order, title, ordering";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getBibleStoryName($story_id, $locale)
{
$sSql = "SELECT bsl.name "
." FROM biblestory_language bsl, language l"
." WHERE bsl.biblestory_id = " . $story_id . " and l.shortcode = '" . $locale . "' and l.id = bsl.language_id";
$sRet = '';
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
function getBibleBookName($vLinkText, $vBookID, $vLanguageID)
{
// check Bible name
$sRet = $vLinkText;
$nPosLastSpace = strrpos($vLinkText, ' ');
if ($nPosLastSpace !== FALSE)
{
// $sBookName = trim(substr($vLinkText, 0, $nPosLastSpace));
$sNum = trim(substr($vLinkText, $nPosLastSpace+1));
}
$sSql2 = "SELECT name from canonicalbiblebooklanguage where language_id = " . $vLanguageID . " and book_id = " . $vBookID . " and not is_abbr order by is_standard desc";
//echo $sSql2."<br>";
$sExistingName = $this->getSingleData($sSql2);
if(!empty($sExistingName))
{
$sTempSpace = " ";
if($this->isLanguageNoBoundary($vLanguageID))
$sTempSpace = "";
$sRet = $sExistingName . $sTempSpace . $sNum;
}
return $sRet;
}
function getWorkBookName($vLinkText, $vBookID, $vLanguageID)
{
// check work name
$sRet = $vLinkText;
$sTempLink = str_replace(" [", "[", $vLinkText);
$nPosLastSpace = strrpos($sTempLink, ' ');
if ($nPosLastSpace !== FALSE)
{
// $sBookName = trim(substr($vLinkText, 0, $nPosLastSpace));
$sNum = trim(substr($sTempLink, $nPosLastSpace+1));
$sNum = str_replace("[", " [", $sNum);
}
$sSql2 = "SELECT name from expositionworklanguage where language_id = " . $vLanguageID . " and work_id = " . $vBookID . " and not is_abbr order by is_standard desc";
//echo $sSql2."<br>";
$sExistingName = $this->getSingleData($sSql2);
if(!empty($sExistingName))
{
$sTempSpace = " ";
if($this->isLanguageNoBoundary($vLanguageID))
$sTempSpace = "";
$sRet = $sExistingName . $sTempSpace . $sNum;
}
return $sRet;
}
function isLanguageNoBoundary($vLanguageID)
{
$aLangNoBoundary = $this->getLangNoWordBoundary();
return (in_array($vLanguageID, $aLangNoBoundary));
}
function getSingleData($vSql)
{
$sRet = '';
$aRet = $this->getNativeQueryResults($vSql);
if(count($aRet) > 0)
$sRet = implode('', $aRet[0]);
return $sRet;
}
function getBiblio($translation_id, $vOriginal)
{
$sSql1 = "select translationmethod_id from expositiontranslation where id = " . $translation_id;
$nTransMethodID = $this->getSingleData($sSql1);
$bMachineTranslated = ($nTransMethodID == $this->TRANSLATION_METHOD_ID_MACHINE);
$sSql = "SELECT b.*, bp.creators, l.name as license_name, l.terms as license_terms, cr.description as copyright from";
$sSql .= " translation_bibliographicdata tb, ";
$sSql .= " bibliographicdata b" . $this->getBiblioJoinSqlStr($vOriginal) . " where ";
$sSql .= " tb.translation_id = ".$translation_id . " and tb.bibliographicdata_id = b.id";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) < 1 && $bMachineTranslated)
{
$sSql = "SELECT b.*, bp.creators, l.name as license_name, l.terms as license_terms, cr.description as copyright from";
$sSql .= " bibliographicdata b" . $this->getBiblioJoinSqlStr($vOriginal) . " where ";
$sSql .= " b.id in (select machine_translation_bilio_id from parameter where id = ". $this->NC_PARAMETER_ID . ")";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
}
if(count($aRet) > 0)
{
$aRet = $aRet[0];
$nBiblioID = $aRet['id'];
$sSql = "SELECT * from publication where biblio_id = " . $nBiblioID;
$aRet['publications'] = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getBiblioOriginal($translation_id)
{
$sSql = "SELECT b.*, bp.creators, l.name as license_name, l.terms as license_terms, cr.description as copyright, t.url as translation_url from translation_bibliographicdata tb, expositiontranslation t, bibliographicdata b" . $this->getBiblioJoinSqlStr(true)
." WHERE tb.translation_id in (select id from expositiontranslation where work_id in (select work_id from expositiontranslation where id = ".$translation_id . ") and isoriginal) and tb.bibliographicdata_id = b.id and t.id = tb.translation_id";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
if(count($aRet) > 0)
{
$aRet = $aRet[0];
$nBiblioID = $aRet['id'];
$sSql = "SELECT * from publication where biblio_id = " . $nBiblioID;
$aRet['publications'] = $this->getNativeQueryResults($sSql);
}
return $aRet;
}
function getBiblioJoinSqlStr($vOriginal)
{
$sRet = " LEFT JOIN (SELECT bp1.bibliographicdata_id, string_agg(CASE WHEN p.name is not null then p.name else ((((p.firstname)::text || CASE WHEN (p.middlename IS NULL) THEN ''::text ELSE (' '::text || (p.middlename)::text) END) || ' '::text) || (p.lastname)::text) end, ', '::text) AS creators FROM bibliographicdata_person bp1, person p WHERE (bp1.person_id = p.id";
if($vOriginal)
$sRet .= " and (bp1.role_id = " . $this->PERSON_ROLE_ID_AUTHOR . " or bp1.role_id is null)";
else
$sRet .= " and bp1.role_id = " . $this->PERSON_ROLE_ID_TRANSLATOR;
$sRet .= ") GROUP BY bp1.bibliographicdata_id) bp ON (b.id = bp.bibliographicdata_id)";
$sRet .= " LEFT JOIN lnccopyrightholder cr on b.copyright_id = cr.id";
$sRet .= " LEFT JOIN license l on b.license_id = l.id";
return $sRet;
}
function getCurrUserLevel($request)
{
return $request->getSession()->get('user_level');
}
function getNextPrevConceptUrl($translation_id, $nLanguageID, $locale)
{
$nLocaleLangID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (SELECT distinct on (c.id) c.id, t.id as translation_id, t.url, case when t.language_id = " . $nLocaleLangID . " then 1 else case when t.language_id = " . $nLanguageID . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 else 4 end end end as lang_order FROM concept c, concept_work cl, expositiontranslation t where c.id = cl.concept_id and cl.work_id = t.work_id"
." order by c.id, lang_order) as t1 order by url";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$sPrev = "";
$sNext = "";
if(count($aTemp) > 0)
{
$nCurrConceptID = $this->getConceptID($translation_id);
$bPass = false;
foreach ($aTemp as $c)
{
$nCurrID = $c['id'];
if($bPass)
{
$sNext = $c['url'];
break;
}
if($nCurrID == $nCurrConceptID)
$bPass = true;
if(!$bPass)
{
$sPrev = $c['url'];
}
}
}
$aRet['prev_concept_url'] = $sPrev;
$aRet['next_concept_url'] = $sNext;
return $aRet;
}
function getConceptTranslationUrl($conceptUrl, $languageCode)
{
$sOrder = "";
if(!empty($languageCode))
{
$sSql = "SELECT t.url FROM concept c, concept_work cl, expositiontranslation t, language l where c.url = '" . str_replace("'", "''", $conceptUrl) . "' and c.id = cl.concept_id and cl.work_id = t.work_id and t.language_id = l.id"
." order by case when l.bibliographiccode = '" . $languageCode . "' then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end limit 1";
}
else
{
$sSql = "SELECT t.url FROM concept c, concept_work cl, expositiontranslation t where c.url = '" . str_replace("'", "''", $conceptUrl) . "' and c.id = cl.concept_id and cl.work_id = t.work_id"
." order by case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end limit 1";
}
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function IsSwedenborgWork($translationUrl)
{
$sSql = "SELECT t.id FROM expositionwork w, expositiontranslation t where t.url = '" . str_replace("'", "''", $translationUrl) . "' and w.id = t.work_id and w.swedenborgtype is not null";
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return (!empty($nRet));
}
function getBibleTranslationID($translationUrl)
{
$sSql = "SELECT id FROM bibletranslation"
." WHERE url = '" . trim(str_replace("'", "''", $translationUrl)) . "'";
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getRowData($vSql)
{
$aRet = array();
$aTemp = $this->getNativeQueryResults($vSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getListData($vSql)
{
$aRet = array();
$aTemp = $this->getNativeQueryResults($vSql);
if(count($aTemp) > 0)
$aRet = $aTemp;
return $aRet;
}
function getBibleChapterList($translationUrl, $translationUrl2, $translationUrl3, $bookUrl)
{
$sSql = "SELECT c.ordering FROM biblebook b, biblechapter c, bibletranslation t, canonicalbiblebook cb";
$sSql .= " WHERE t.url = '" . trim(str_replace("'", "''", $translationUrl)) . "'";
if(!empty($translation_id2) && $translation_id2 != '-none-')
{
$sSql .= " and cb.id in (SELECT cb.id FROM biblebook b, canonicalbiblebook cb, bibletranslation t"
." WHERE t.url = " . trim(str_replace("'", "''", $translationUrl2)) . " and b.canonicalization_id = cb.id and t.id = b.translation_id)";
}
if(!empty($translation_id3) && $translation_id3 != '-none-')
{
$sSql .= " and cb.id in (SELECT cb.id FROM biblebook b, canonicalbiblebook cb, bibletranslation t"
." WHERE t.url = " . trim(str_replace("'", "''", $translationUrl3)) . " and b.canonicalization_id = cb.id and t.id = b.translation_id)";
}
$sSql .= " and cb.url = '" . trim(str_replace("'", "''", $bookUrl)) . "'";
$sSql .= " and t.id = b.translation_id and b.id = c.book_id and cb.id = b.canonicalization_id";
$sSql .= " order by c.ordering";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return $aRet;
}
function getLanguageIDByTranslation($translation_id)
{
$sSql = "select language_id from expositiontranslation where id = " . $translation_id;
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getBibleMultiColumnSpec($translationUrl, $bookUrl, $chapterIndex, $verseStartIndex=null, $verseEndIndex = null)
{
$bible_multi_spec = $this->MULTI_URL_INDICATOR_BIBLE . '_' . $translationUrl . '_' . $bookUrl;
if($chapterIndex != null)
{
$bible_multi_spec .= '_' . $chapterIndex;
if($verseStartIndex != null)
{
$bible_multi_spec .= '_' . $verseStartIndex;
if($verseEndIndex != null)
$bible_multi_spec .= '-' . $verseEndIndex;
}
}
// echo $bible_multi_spec;
return $bible_multi_spec;
}
function getSwedenborgWorks($request, $nLocaleLanguageID, $nSelectedLangID)
{
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as translation_id, t.url as translation_url, t.translatedtitle AS title, t.description, case when t.language_id = ".$nLocaleLanguageID ." then 1 else 2 end as lang_order, case when t.islanguagedefault then 1 else 2 end as default_order, w.swedenborgtype, wt.ordering as type_order"
." FROM expositiontranslation t, expositionworktype wt, expositionwork w"
." WHERE w.swedenborgtype IS NOT NULL and t.work_id=w.id and w.swedenborgtype = wt.name";
if(!empty($nSelectedLangID))
$sSql .= " and t.language_id = ".$nSelectedLangID;
else if($nLocaleLanguageID == $this->LANGUAGE_ID_ENGLISH)
$sSql .= " and t.language_id = ".$nLocaleLanguageID;
else
$sSql .= " and t.language_id in (" . $this->LANGUAGE_ID_ENGLISH . ", ".$nLocaleLanguageID.")";
$sSql .= " and w.swedenborgtype not in ('scientific', 'transitional')";
$sSql .= " and t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " and w.ispublic AND t.ispublic and t.url is not null and t.url <> '' order by w.id, lang_order, default_order) as w2 order by w2.type_order, w2.title";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$nLen = count($aTemp);
foreach($aTemp as $temp)
{
$aRet[$temp['swedenborgtype']][] = $temp;
}
return $aRet;
}
// Show the transitional and/or scientific and works after the main list
function getSwedenborgWorks2($request, $nLocaleLanguageID, $nSelectedLangID)
{
$sSql = " select * from (SELECT distinct on (w.id) w.id as work_id, t.id as translation_id, t.url as translation_url, t.translatedtitle || case when wd.datecreated is null then '' else ' (' || wd.datecreated || ')' end AS title, t.description, case when t.language_id = ".$nLocaleLanguageID ." then 1 else 2 end as lang_order, case when t.islanguagedefault then 1 else 2 end as default_order, w.swedenborgtype, wt.ordering as type_order, wd.datecreated as order2, t.translatedtitle as order3"
." FROM expositiontranslation t, expositionworktype wt, expositionwork w"
." left join vworkdatecreated wd on w.id = wd.work_id"
." WHERE w.swedenborgtype IS NOT NULL and t.work_id=w.id and w.swedenborgtype = wt.name";
if(!empty($nSelectedLangID))
$sSql .= " and t.language_id = ".$nSelectedLangID;
else if($nLocaleLanguageID == $this->LANGUAGE_ID_ENGLISH)
$sSql .= " and t.language_id = ".$nLocaleLanguageID;
else
$sSql .= " and t.language_id in (" . $this->LANGUAGE_ID_ENGLISH . ", ".$nLocaleLanguageID.")";
$bShowScientific = $this->showScientific($request);
if(!$bShowScientific)
$sSql .= " and w.swedenborgtype in ('transitional')";
else
$sSql .= " and w.swedenborgtype in ('scientific', 'transitional')";
$sSql .= " and t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " and w.ispublic AND t.ispublic and t.url is not null and t.url <> '' order by w.id, lang_order, default_order) as w3 order by type_order, order2 desc NULLS LAST, order3";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
$nLen = count($aTemp);
foreach($aTemp as $temp)
{
$aRet[$temp['swedenborgtype']][] = $temp;
}
return $aRet;
}
function getSwedenborgWorkLanguages($request)
{
$sSql = "SELECT id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct language_id FROM expositiontranslation";
$sSql .= " WHERE type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " and ispublic and work_id in (select distinct id from expositionwork where ispublic and swedenborgtype is not null";
$bShowScientific = $this->showScientific($request);
if(!$bShowScientific)
$sSql .= " and swedenborgtype <> 'scientific'";
$sSql .= ")) order by name";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return $aRet;
}
function getConceptID($vTranslationId)
{
$sSql = "SELECT cl.concept_id"
." FROM work_category wc, concept_work cl, expositiontranslation t"
." WHERE t.id = " . $vTranslationId ." and wc.category_id = " . $this->CATEGORY_ID_CONCEPT . " and wc.work_id = t.work_id and cl.work_id = t.work_id";
// echo $sSql;
$sRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = $aTemp[0]['concept_id'];
return $sRet;
}
function showScientific($request)
{
$bRet = false;
// Let's show the Swedenborg Scientific works to users whose privileges are Administrator,
// Application Manager, OTLE Administrator, Writings Translator or Editor, Explanation Author,
// Editor, or Translator.
$ul = array(-1,1,2,3,5,6,7,8,9,11,12,13,14);
$nUserLevelID = $this->getCurrUserLevel($request);
if(in_array($nUserLevelID, $ul))
$bRet = true;
return $bRet;
}
function canEditExplanation($request)
{
$bRet = false;
$ul = array(-1,1,2,3,5,6,8,9,11,12,14);
$nUserLevelID = $this->getCurrUserLevel($request);
if(in_array($nUserLevelID, $ul))
$bRet = true;
return $bRet;
}
function canEditWork($request)
{
$bRet = false;
$ul = array(-1,3,6,14);
$nUserLevelID = $this->getCurrUserLevel($request);
if(in_array($nUserLevelID, $ul))
$bRet = true;
return $bRet;
}
function getChapterIDForTranslation($translationId, $bible_chapterId)
{
$sSql = "SELECT c.id"
." FROM biblechapter c, biblebook b, bibletranslation t,"
." (select b2.canonicalization_id, c2.ordering as chapter_order from biblechapter c2, biblebook b2 "
." where c2.id = " . $bible_chapterId . " and c2.book_id = b2.id) as b3 "
." WHERE t.id = " . $translationId ." and b.translation_id = t.id"
." and c.book_id = b.id and b.canonicalization_id = b3.canonicalization_id "
." and c.ordering = b3.chapter_order limit 1";
// echo $sSql;
$sRet = null;
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$sRet = $aTemp[0]['id'];
return $sRet;
}
function getBibleWordPopupText($explanationID)
{
$nTextLength = 300;
$sSql = "SELECT string_agg(tt.text, ' ' order by tu.ordering) as text FROM expositiontranslationtext tt , expositiontextunit tu where tt.translation_id = " . $explanationID . " and tt.placement_id = tu.id group by tt.translation_id";
// echo $sSql;
$sText = $this->getSingleData($sSql);
$sRet = $this->getTextWithinLength($sText, $nTextLength, "...");
return $sRet;
}
function getTextWithinLength($text, $length, $str_end='')
{
if(strlen($text) > $length) {
$nPosSpace = strpos($text, ' ', $length);
if($nPosSpace !== false)
$text = substr($text, 0, $nPosSpace).$str_end;
else
$text = substr($text, 0, $length).$str_end;
}
return $text;
}
function getBibleWordPopupConceptInfo($explanationID, $conceptID)
{
$sSql = "select ct.translatedtitle as title, c.url as concept_url"
." FROM concept c, ";
$sSql .= " (select cl.concept_id, trans.translatedtitle from concept_work cl, expositiontranslation trans where trans.id =" . $explanationID . " and cl.work_id = trans.work_id) as ct ";
$sSql .= " WHERE c.id = " . $conceptID . " and c.id = ct.concept_id";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getChapterSummaryList($explanationID, $bibleBookID)
{
$sSql1 = "select language_id from expositiontranslation where id = " . $explanationID;
$nLanguageID = $this->getSingleData($sSql1);
$sSql = "SELECT distinct on (cc.ordering) t.id, t.translatedtitle as title, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc.ordering as bible_spec, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, cc.ordering as chapter_order, case when t.id = " . $explanationID . " then 1 else case when t.language_id = " . $nLanguageID . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 end end end as lang_order";
$sSql .= " FROM expositionwork w, expositiontranslation t, biblestory bs, story_work sw, canonicalbiblechapter cc, canonicalbiblebook cb, biblebook b, bibletranslation trans";
$sSql .= " WHERE b.id = " . $bibleBookID . " and cc.book_id = b.canonicalization_id and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and w.ispublic and t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " and t.ispublic and bs.chapter_id = cc.id and cc.book_id = cb.id and b.translation_id = trans.id";
if($nLanguageID == $this->LANGUAGE_ID_ENGLISH)
$sSql .= " and t.language_id = " . $this->LANGUAGE_ID_ENGLISH;
else
$sSql .= " and t.language_id in (" . $nLanguageID . ", " . $this->LANGUAGE_ID_ENGLISH . ")";
$sSql .= " order by cc.ordering, lang_order, t.id desc";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return $aRet;
}
function getChapterSummaryListForSinglePage($explanationID, $cBibleBookID)
{
$sSql1 = "select language_id from expositiontranslation where id = " . $explanationID;
$nLanguageID = $this->getSingleData($sSql1);
$sSql = "SELECT distinct on (cc.ordering) t.id, t.translatedtitle as title, t.url, cc.ordering as chapter_order, case when t.id = " . $explanationID . " then 1 else case when t.language_id = " . $nLanguageID . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 end end end as lang_order";
$sSql .= " FROM expositionwork w, expositiontranslation t, biblestory bs, story_work sw, canonicalbiblechapter cc";
$sSql .= " WHERE cc.book_id = " . $cBibleBookID . " and bs.chapter_id = cc.id and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and w.ispublic and t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " and t.ispublic";
if($nLanguageID == $this->LANGUAGE_ID_ENGLISH)
$sSql .= " and t.language_id = " . $this->LANGUAGE_ID_ENGLISH;
else
$sSql .= " and t.language_id in (" . $nLanguageID . ", " . $this->LANGUAGE_ID_ENGLISH . ")";
$sSql .= " order by cc.ordering, lang_order, t.id desc";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return $aRet;
}
function getUILocale($language_id)
{
$sSql = "SELECT l.shortcode FROM language l, languageforui lu"
." WHERE l.id = " . $language_id . " and l.id = lu.language_id";
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getWorkTranslationLanguage($translationUrl)
{
$sSql = "SELECT language_id FROM expositiontranslation t where t.url = '" . str_replace("'", "''", $translationUrl) . "' limit 1";
// echo $sSql;
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getBibleTranslationLanguage($translationUrl)
{
$sSql = "SELECT language_id FROM bibletranslation where url = '" . str_replace("'", "''", $translationUrl) . "' limit 1";
// echo $sSql;
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getCVerseIDs($bookUrl, $chapterIndex, $verseIndex=null)
{
$sSql = "";
if($verseIndex != null)
{
$sSql = "select array_to_string(array_agg(cbv.id),',') as ids"
." FROM canonicalbiblechapter cbc, canonicalbibleverse cbv, canonicalbiblebook cbb, bibleverse v, canonicalbibleverse_bibleverse cvv";
$sSql .= " where cbb.url = '" . str_replace("'", "''", $bookUrl) . "' and cbc.ordering = " . $chapterIndex . " and v.indexdisplay = '" . str_replace("'", "''", $verseIndex) . "' and cbc.book_id = cbb.id and cbv.chapter_id = cbc.id and v.id = cvv.bibleverse_id and cbv.id = cvv.canonicalbibleverse_id";
}
else
{
$sSql = "select array_to_string(array_agg(cbv.id),',') as ids"
." FROM canonicalbiblechapter cbc, canonicalbibleverse cbv, canonicalbiblebook cbb";
$sSql .= " where cbb.url = '" . str_replace("'", "''", $bookUrl) . "' and cbc.ordering = " . $chapterIndex . " and cbc.book_id = cbb.id and cbv.chapter_id = cbc.id";
}
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getCChapterID($bookUrl, $chapterIndex)
{
$sSql = "select cbc.id"
." FROM canonicalbiblechapter cbc, canonicalbiblebook cbb";
$sSql .= " where cbb.url = '" . str_replace("'", "''", $bookUrl) . "' and cbc.ordering = " . $chapterIndex . " and cbc.book_id = cbb.id";
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getBibleBookInfo($translationUrl, $bookUrl)
{
$sSql = "select b.name FROM biblebook b, canonicalbiblebook cb, bibletranslation t";
$sSql .= " where cb.url = '" . str_replace("'", "''", $bookUrl) . "' and t.url = '" . str_replace("'", "''", $translationUrl) . "' and b.translation_id = t.id and cb.id = b.canonicalization_id";
// echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getVerseInfoForFrame($translationUrl, $bookUrl, $chapterIndex, $verseIndex=null)
{
$aRet = array();
$verseLanguage = $this->getBibleTranslationLanguage($translationUrl);
$sCVerseIDs = $this->getCVerseIDs($bookUrl, $chapterIndex, $verseIndex);
$sCChapterIDs = $this->getCChapterID($bookUrl, $chapterIndex);
$aRet['CVerseIDs'] = $sCVerseIDs;
$aRet['LanguageID'] = $verseLanguage;
$aRet['CChapterIDs'] = $sCChapterIDs;
return $aRet;
}
function isBadBot()
{
$bRet = false;
$useragent=@$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/petalbot|mj12bot/i',$useragent))
{
$bRet = true;
}
return $bRet;
}
function getStoryTranslation($sStoryUrl, $request)
{
$locale = $request->getLocale();
$sSql1 = "select id from language where shortcode = '" . trim(str_replace("'", "''", $locale)) . "'";
$nUILanguageID = $this->getSingleData($sSql1);
$sSql = "select t.url FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs"
." WHERE bs.url = '" . str_replace("'", "''", $sStoryUrl) . "' and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and w.ispublic AND t.ispublic order by case when t.language_id = " . $nUILanguageID . " then 1 else 2 end limit 1";
// echo $sSql . "<br>";
return $this->getSingleData($sSql);
}
function getUILanguageID($request)
{
$locale = $request->getLocale();
$sSql1 = "select id from language where shortcode = '" . trim(str_replace("'", "''", $locale)) . "'";
$nUILanguageID = $this->getSingleData($sSql1);
return $nUILanguageID;
}
function getUserPreferredLanguageID($request)
{
$nLanguageID = $request->getSession()->get('current_language_id');
return $nLanguageID;
}
function getVerseExplanationsForVerse($verse_id)
{
$sSql = "SELECT t.id as translation_id, t.translatedtitle as explanationtitle, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, (case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) as group_order"
." FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_VERSE . ") as bs, canonicalbibleverse cv, bibleverse v, canonicalbibleverse_bibleverse cvv,"
." canonicalbiblechapter scc, expositionwork w, expositiontranslation t, story_work sw,"
." biblechapter c, biblebook b, canonicalbiblebook cb, language l"
." WHERE v.id = " . $verse_id
." and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id"
." and bs.startverse_id = cv.id"
." and cv.chapter_id = scc.id"
." and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id"
." and w.ispublic and t.ispublic"
." and v.chapter_id = c.id and b.id = c.book_id and scc.book_id = cb.id and v.verse_id is null and t.language_id = l.id"
." order by group_order, language_name, t.name";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getChapterExplanationForCombo($nChapterID)
{
$sSql = "SELECT t.id as translation_id, t.is_modern, t.translatedtitle as explanationtitle, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, (case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) as group_order, case when t.islanguagedefault then 1 else 2 end as trans_order"
." FROM expositionwork w, expositiontranslation t, expositiontranslationtext tt, story_work sw, biblestory bs, language l"
." WHERE bs.chapter_id = " . $nChapterID . " and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.id = tt.translation_id and tt.text is not null and tt.text <> ''"
." and w.ispublic AND t.ispublic and t.language_id = l.id order by group_order, language_name, trans_order";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getVerseExplanationInText($request, $explanations, $nLanguageID)
{
$nUILanguageID = $this->getUILanguageID($request);
$nSelectedExplTransID = $request->query->get('et');
// $nUserPreferredLanguageID = $this->getUserPreferredLanguageID($request);
$verseexplanation = null;
$bGetID = false;
if(!empty($nSelectedExplTransID))
{
foreach($explanations as $expl) {
$aTrans = $expl->getTranslations();
foreach($aTrans as $trans) {
if($trans->getId() == $nSelectedExplTransID)
{
$verseexplanation = $trans;
$bGetID = true;
break;
}
}
}
}
if(!$bGetID)
{
foreach($explanations as $expl) {
$aTrans = $expl->getTranslations();
foreach($aTrans as $trans) {
if($trans->getLanguage()->getId() == $nLanguageID)
{
$verseexplanation = $trans;
$bGetID = true;
break;
}
}
}
}
if(!$bGetID)
{
foreach($explanations as $expl) {
$aTrans = $expl->getTranslations();
if($trans->getLanguage()->getId() == $nUILanguageID)
{
$verseexplanation = $trans;
$bGetID = true;
break;
}
}
}
if(!$bGetID)
{
foreach($explanations as $expl) {
$aTrans = $expl->getTranslations();
if($trans->getLanguage()->getId() == $this->LANGUAGE_ID_ENGLISH)
{
$verseexplanation = $trans;
$bGetID = true;
break;
}
}
}
return $verseexplanation;
}
// get table of contents of a work translation
function getWorkTranslationTOC($nTranslationID, $bCheck=false)
{
$sSql = "select tt.toc_text, wt.startpassage_id, p.swedenborgsection as startpassagenum, p2.swedenborgsection as endpassagenum, t.url as translation_url, d.url as division_url, p.ordering as startpassageorder, p2.ordering as endpassageorder, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' end) || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as link_spec";
$sSql .= " from expositiontranslationtoc tt, expositionwork w, expositiontranslation t, expositiondivision d, expositionpassage p, expositionworktoc wt left join expositionpassage p2 on wt.endpassage_id = p2.id";
$sSql .= " where tt.translation_id = " . $nTranslationID . " and tt.worktoc_id = wt.id and wt.startpassage_id = p.id and tt.translation_id = t.id and p.division_id = d.id and t.work_id = w.id order by p.ordering";
if($bCheck)
$sSql .= " limit 1";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$startpassagenum = $aRet[$i]["startpassagenum"];
$endpassagenum = $aRet[$i]["endpassagenum"];
$sPassageStr = $startpassagenum;
if($endpassagenum != '')
$sPassageStr .= '-'.$endpassagenum;
$aRet[$i]["link_text"] = $sPassageStr;
$nNextStartPassageOrder = '';
if($i != $nLen-1)
{
$nNextStartPassageOrder = $aRet[$i+1]["startpassageorder"];
}
$aRet[$i]["nextstartpassageorder"] = $nNextStartPassageOrder;
}
return $aRet;
}
function getBibleTransInfo($translation_url)
{
$sSql = "SELECT id, name"
." FROM bibletranslation"
." WHERE url = '" . trim(str_replace("'", "''", $translation_url)) . "'";
// echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function hasSliderItem($aVerseInfo, $bInframe, $request, $bChapter = false)
{
$bRet = false;
if(!$bInframe)
{
$ref = $this->getBibleRefPassage($aVerseInfo, $bChapter, false, true);
if(!empty($ref))
return true;
$ref = $this->getBibleRefPassage($aVerseInfo, $bChapter, true, true);
if(!empty($ref))
return true;
}
$ref = $this->getExplanationRefForBible($aVerseInfo, $request, true);
if(!empty($ref))
return true;
$ref = $this->getGCEDRefForBible($aVerseInfo, $bChapter, true);
if(!empty($ref))
return true;
$ref = $this->getOTLERefForBible($aVerseInfo, $bChapter, true);
if(!empty($ref))
return true;
$ref = $this->getVerseCrossRefs($aVerseInfo, $request, true);
if(!empty($ref))
return true;
$ref = $this->getBibleWordExplanations($aVerseInfo, $request, true);
if(!empty($ref))
return true;
$ref = $this->getStoriesForVerses($aVerseInfo, $request, true);
if(!empty($ref))
return true;
$ref = $this->getFirstExplainedVerse($aVerseInfo, $request);
if(!empty($ref))
return true;
return $bRet;
}
function getBibleRefPassageNew($aVerseInfo, $bChapter, $bCorePassage, $bCheck=false)
{
$sVerseIDs = $aVerseInfo['CVerseIDs'];
$verseLanguage = $aVerseInfo['LanguageID'];
$nBibleBookID = $aVerseInfo['BookID'];
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
$sCChapterIDs = '';
if($bChapter)
$sCChapterIDs = $aVerseInfo['CChapterIDs'];
if($sVerseIDs == '' && $sCChapterIDs == '') {
return null;
} else {
if($sVerseIDs != '')
{
$tempChapterIDs = $this->checkWholeChapters($sVerseIDs);
if($tempChapterIDs != null)
{
if($sCChapterIDs != '')
$sCChapterIDs .= ',';
$sCChapterIDs .= $tempChapterIDs;
}
}
return $this->getBibleRefPassageInfoNew($verseLanguage, $sVerseIDs, $sCChapterIDs, $nBibleBookID, $bCorePassage, $bCheck);
}
}
function getBibleRefPassageInfoNew($language_id, $sVerseIDs, $sChapterIDs, $nBibleBookID, $bCorePassage, $bCheck)
{
if($sVerseIDs == '' && $sChapterIDs == '')
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sSql1 = "select name from biblebook where id = ". $nBibleBookID;
$sBibleBookName = $this->getSingleData($sSql1);
$sFilter = " p.id";
if(!$bCorePassage)
{
$sFilter .= " in (";
if ($sVerseIDs <> '')
{
$sFilter .= 'SELECT DISTINCT passage_id FROM verse_passage'
.' WHERE verse_id in (' . $sVerseIDs . ')';
}
$sFilter .= ") and p.id not ";
}
$sFilter .= " in (";
if ($sChapterIDs <> '')
{
$sFilter .= 'SELECT DISTINCT passage_id FROM chapter_passage'
.' WHERE chapter_id in (' . $sChapterIDs . ')';
}
if ($sVerseIDs <> '')
{
if ($sChapterIDs <> '')
$sFilter .= " union ";
$sFilter .= 'SELECT DISTINCT passage_id FROM verse_passage'
.' WHERE verse_id in (' . $sVerseIDs . ')';
$sFilter .= ' and iscorepassage';
}
$sFilter .= ")";
$sSql = "select t2.*, b.book_name, b.chapter_num, b.verse_num, b.chapter_num || ':' || b.verse_num as chapter_verse from (SELECT distinct on (p.id) p.id, w.swedenborgtype, t.translatedtitle AS title, p.swedenborgsection as num, '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "' || '_' || t.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as ref_column_spec, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE " . $sFilter;
$sSql .= " and w.swedenborgtype is not null and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ") and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by p.id, lang_order, t.islanguagedefault desc";
if($bCheck)
$sSql .= " limit 1";
$sSql .= ") as t2,";
// check ref chapter/verse
if($bCorePassage)
{
$sSql .= "(";
if ($sChapterIDs <> '')
{
$sSql .= "SELECT cp.passage_id, '" . str_replace("'", "''", $sBibleBookName) . "' as book_name, cc.ordering as chapter_num, 0 as verse_num FROM chapter_passage cp, canonicalbiblechapter cc";
$sSql .= " WHERE cp.chapter_id in (" . $sChapterIDs . ") and cc.id = cp.chapter_id union ";
}
$sSql .= "SELECT vp.passage_id, '" . str_replace("'", "''", $sBibleBookName) . "' as book_name, cc.ordering as chapter_num, cv.ordering as verse_num FROM verse_passage vp, canonicalbibleverse cv, canonicalbiblechapter cc";
$sSql .= " WHERE verse_id in (" . $sVerseIDs . ") and vp.iscorepassage and cv.id = vp.verse_id and cc.id = cv.chapter_id";
$sSql .= ") as b";
}
else
{
$sSql .= "(SELECT vp.passage_id, '" . str_replace("'", "''", $sBibleBookName) . "' as book_name, cc.ordering as chapter_num, cv.ordering as verse_num FROM verse_passage vp, canonicalbibleverse cv, canonicalbiblechapter cc";
$sSql .= " WHERE verse_id in (" . $sVerseIDs . ") and not vp.iscorepassage and cv.id = vp.verse_id and cc.id = cv.chapter_id";
$sSql .= ") as b";
}
$sSql .= " where b.passage_id = t2.id order by b.chapter_num, b.verse_num, t2.work_order, t2.title, t2.ordering";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getAllListByCategory($vCategoryId, $locale, $request)
{
$bConcept = ($vCategoryId == $this->CATEGORY_ID_CONCEPT);
$bTopic = ($vCategoryId == $this->CATEGORY_ID_SPIRITUAL_TOPIC);
$nSelectedLang = null;
$searchOptions = $request->query->all();
if(array_key_exists('language', $searchOptions))
{
$nLanguageID = $searchOptions['language'];
$nSelectedLang = $nLanguageID;
}
else
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as tid,"
."t.translatedtitle AS title, t.description, t.url, wo.feature_ordering,"
. $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
. ", case when wo.is_featured then 1 else 2 end as is_featured,";
if($bConcept)
{
$sSql .= "case when fi.image_file is not null then fi.image_file else case when fi2.image_file is not null then fi2.image_file else case when ci.image_file is not null then ci.image_file else ti.image_file end end end as image_file, case when ci.image_file is not null then ci.image_title else case when fi.image_file is not null then fi.image_title else case when fi2.image_file is not null then fi2.image_title else ti.image_title end end end as image_title";
}
else
{
$sSql .= "case when fi.image_file is not null then fi.image_file else case when fi2.image_file is not null then fi2.image_file else ti.image_file end end as image_file, case when fi.image_file is not null then fi.image_title else case when fi2.image_file is not null then fi2.image_title else ti.image_title end end as image_title";
}
$sSql .= ", t.primaryformat_id, case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then te.youtube_id else null end as youtube_id"
." FROM expositiontranslation t join expositionwork w on t.work_id = w.id";
// featured image from the current translation
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
// featured image from the translation with image
$sSql .= " left join (select distinct on (t1.work_id) t1.work_id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id order by t1.work_id, case when t1.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) fi2 on t.work_id = fi2.work_id";
// textunit image
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1) ti on t.id = ti.translation_id";
if($bConcept)
{
$sSql .= " left join (select distinct on (cl.work_id) cl.work_id as id, i.file as image_file, i.title as image_title from image i, conceptillustration ci1, concept_work cl where cl.concept_id = ci1.concept_id and ci1.image_id = i.id) ci on t.work_id = ci.id";
}
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering, t2.is_featured from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") wo on w.id = wo.work_id";
$sSql .= " left join (select distinct on (d.work_id) d.work_id, e.youtube_id from passage_media pm, expositionpassage p, expositiondivision d, embed e";
$sSql .= " where e.youtube_id is not null and e.id = pm.embed_id";
$sSql .= " and pm.passage_id = p.id and p.division_id = d.id order by d.work_id, p.ordering) te on w.id = te.work_id";
$sSql .= " WHERE (t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $vCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $vCategoryId . "))";
$sSql .= " and t.type_id = 1 ";
$sSql .= " and t.ispublic AND w.ispublic and t.url is not null and t.url <> ''";
if(!empty($nSelectedLang))
$sSql .= " and t.language_id = " . $nSelectedLang;
else
{
//if($bTopic && !$this->showEnglishTrans($nLanguageID))
// $sSql .= " and t.language_id = " . $nLanguageID;
}
$sSql .= " ORDER BY w.id, is_featured, language_order, lang_default_order) as t where language_order < 3"
." ORDER BY language_order, is_featured, (case when image_file is not null and description is not null then 1 else case when image_file is not null then 2 else case when youtube_id is not null then 3 else case when description is not null then 4 else case when title is not null then 5 else 6 end end end end end), feature_ordering, title";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getSearchedListByCategoryAndPathway($locale, $request, $vCategoryId,$vPathwayID)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$bTopic = ($vCategoryId == $this->CATEGORY_ID_SPIRITUAL_TOPIC);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as tid,"
."t.translatedtitle AS title, t.description, t.url,"
." case when wt.display_order is null then wtg.display_order else wt.display_order end as display_order, " . $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
. ", case when wo.is_featured then 1 else 2 end as is_featured"
. ", case when fi.image_file is null then ti.image_file else fi.image_file end as image_file, case when fi.image_file is null then ti.image_title else fi.image_title end as image_title, t.primaryformat_id, case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then te.youtube_id else null end as youtube_id"
." FROM expositiontranslation t join expositionwork w on t.work_id = w.id left join work_topic wt on t.work_id = wt.work_id left join work_topicgroup wtg on t.work_id = wtg.work_id";
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1";
$sSql .= ") ti on t.id = ti.translation_id";
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering, t2.is_featured from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") wo on w.id = wo.work_id";
$sSql .= " left join (select distinct on (d.work_id) d.work_id, e.youtube_id from passage_media pm, expositionpassage p, expositiondivision d, embed e";
$sSql .= " where e.youtube_id is not null and e.id = pm.embed_id";
$sSql .= " and pm.passage_id = p.id and p.division_id = d.id order by d.work_id, p.ordering) te on w.id = te.work_id";
$sSql .= " WHERE (t.work_id in (select work_id from work_topic where topic_id in (select expositiontopic_id from expositiontopic_group where expositiontopicgroup_id = " . $vPathwayID . "))"
." OR t.work_id in (select work_id from work_topicgroup where topicgroup_id = " . $vPathwayID . ") )";
$sSql .= " AND (t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $vCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $vCategoryId . "))";
$sSql .= " and t.type_id = 1";
$sSql .= " and t.ispublic AND w.ispublic and t.url is not null and t.url <> ''";
// if(!$this->showEnglishTrans($nLanguageID))
$sSql .= " and t.language_id = " . $nLanguageID;
$sSql .= " ORDER BY w.id, is_featured, language_order, lang_default_order) as t where language_order < 3"
." ORDER BY is_featured, (case when image_file is not null and description is not null then 1 else case when image_file is not null then 2 else case when youtube_id is not null then 3 else case when description is not null then 4 else case when title is not null then 5 else 6 end end end end end), display_order";
$aRet = $this->getNativeQueryResults($sSql);
if($bTopic)
{
$aTopic = array();
if(count($aRet) > 1)
{
foreach($aRet as $r)
{
$aTemp = array();
$aTemp['translation_id'] = $r['tid'];
$aTemp['translation_url'] = $r['url'];
$aTopic[] = $aTemp;
}
}
$request->getSession()->set('searched_topic_list', $aTopic);
}
return $aRet;
}
function getAllBibleStories($locale, $vLimit=null)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) t.translatedtitle AS title, t.description, t.url, bsw.story_id, bsw.story_url, t.url as translation_url, wo.feature_ordering"
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
. ", case when wo.is_featured then 1 else 2 end as is_featured"
. ", case when fi.image_file is null then ti.image_file else fi.image_file end as image_file, case when fi.image_file is null then ti.image_title else fi.image_title end as image_title, t.primaryformat_id"
. ", bsw.story_id, bsw.story_url, case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then case when bsw.youtube_id is null then te.youtube_id else bsw.youtube_id end else null end as youtube_id, " . $sLangStr
." FROM expositiontranslation t, expositionwork w "
." join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id, bs.name as story_name, bs.startverse_id, bsy.youtube_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE
. ") as bs left join (select distinct on (se.story_id) se.story_id, e.youtube_id from story_embed se, embed e where e.youtube_id is not null and e.id = se.embed_id) as bsy on bs.id = bsy.story_id where bs.id = sw.story_id) bsw on w.id = bsw.work_id";
$sSql .= " left join (select distinct on (t1.work_id) t1.work_id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on w.id = fi.work_id";
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering, t2.is_featured from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") wo on w.id = wo.work_id";
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, ti2.* from vexpositiontranslationimage ti2, expositiontranslation t2 where t2.id = ti2.translation_id) ti on w.id = ti.work_id";
// work embed
$sSql .= " left join (select distinct on (d.work_id) d.work_id, e.youtube_id from passage_media pm, expositionpassage p, expositiondivision d, embed e";
$sSql .= " where e.youtube_id is not null and e.id = pm.embed_id";
$sSql .= " and pm.passage_id = p.id and p.division_id = d.id order by d.work_id, p.ordering) te on w.id = te.work_id";
$sSql .= " WHERE t.ispublic and t.work_id=w.id AND w.ispublic";
$sSql .= " and t.type_id = 1"
." and story_url is not null"
." ORDER BY w.id, language_order, lang_default_order";
$sSql .= ") as t"
." where language_order < 3"
." ORDER BY language_order, is_featured, feature_ordering, (case when image_file is not null and description is not null then 1 else case when image_file is not null then 2 else case when youtube_id is not null then 3 else case when description is not null then 4 else case when title is not null then 5 else 6 end end end end end), title";
if($vLimit != null && $vLimit > 0)
$sSql .= " limit " . $vLimit;
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getPrimaryFormatList($request)
{
$sText = $request->getSession()->get('ui_str')["primary.format.text"];
$sVideo = $request->getSession()->get('ui_str')["primary.format.video"];
$sAudio = $request->getSession()->get('ui_str')["primary.format.audio"];
$sSql = "SELECT id, case when id = " . $this->PRIMARY_FORMAT_ID_TEXT . " then '" . str_replace("'", "''", $sText) . "' else case when id = " . $this->PRIMARY_FORMAT_ID_AUDIO . " then '" . str_replace("'", "''", $sAudio) . "' else case when id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then '" . str_replace("'", "''", $sVideo) . "' end end end as name"
." FROM primaryformat";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getListByCategory($locale, $request, $vCategoryId, $vFormatID, $vCBookID, $vAuthorID=null)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$bBibleStory = ($vCategoryId == $this->CATEGORY_ID_BIBLE_STORY);
$bTopic = ($vCategoryId == $this->CATEGORY_ID_SPIRITUAL_TOPIC);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as tid,"
."t.translatedtitle AS title, t.description, t.url, " . $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
. ", case when wo.is_featured then 1 else 2 end as is_featured"
. ", case when fi.image_file is null then ti.image_file else fi.image_file end as image_file, case when fi.image_file is null then ti.image_title else fi.image_title end as image_title, t.primaryformat_id";
if($bBibleStory)
{
$sSql .= ", bsw.story_id, bsw.story_url, case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then case when bsw.youtube_id is null then te.youtube_id else bsw.youtube_id end else null end as youtube_id";
}
else
$sSql .= ", case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then te.youtube_id else null end as youtube_id";
$sSql .= " FROM expositiontranslation t join expositionwork w on t.work_id = w.id";
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1";
$sSql .= ") ti on t.id = ti.translation_id";
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering, t2.is_featured from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") wo on w.id = wo.work_id";
$sSql .= " left join (select distinct on (d.work_id) d.work_id, e.youtube_id from passage_media pm, expositionpassage p, expositiondivision d, embed e";
$sSql .= " where e.youtube_id is not null and e.id = pm.embed_id";
$sSql .= " and pm.passage_id = p.id and p.division_id = d.id order by d.work_id, p.ordering) te on w.id = te.work_id";
if($bBibleStory)
{
$sSql .= " join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id, bs.name as story_name, bs.startverse_id, bsy.youtube_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE;
if($vCBookID != '')
{
$sSql .= " and startverse_id in (select cv.id from canonicalbibleverse cv, canonicalbiblechapter cc where cc.book_id = " . $vCBookID . " and cv.chapter_id = cc.id)";
}
$sSql .= ") as bs left join (select distinct on (se.story_id) se.story_id, e.youtube_id from story_embed se, embed e where e.youtube_id is not null and e.id = se.embed_id) as bsy on bs.id = bsy.story_id where bs.id = sw.story_id) bsw on t.work_id = bsw.work_id";
}
$sSql .= " where w.ispublic";
if(!$bBibleStory)
{
$sSql .= " and (t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $vCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $vCategoryId . "))";
}
if($vFormatID != '')
{
//$sSql .= " and (t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $nCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $nCategoryId . ") or t.work_id IN (SELECT work_id FROM story_work))";
$sSql .= " and t.primaryformat_id = " . $vFormatID;
}
if(!empty($vAuthorID))
{
$sSql .= " and t.id in (select distinct t.id from bibliographicdata_person bp, translation_bibliographicdata tb, expositiontranslation t where bp.person_id = " . $vAuthorID . " and bp.bibliographicdata_id = tb.bibliographicdata_id and t.id = tb.translation_id)";
}
$sSql .= " and t.type_id = 1";
$sSql .= " and t.ispublic AND t.url is not null and t.url <> ''";
$sSql .= " ORDER BY w.id, is_featured, language_order, lang_default_order) as t where language_order < 3"
." ORDER BY is_featured, (case when image_file is not null and description is not null then 1 else case when image_file is not null then 2 else case when youtube_id is not null then 3 else case when description is not null then 4 else case when title is not null then 5 else 6 end end end end end), title";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
if($bBibleStory)
{
$aStory = array();
if(count($aRet) > 1)
{
foreach($aRet as $r)
{
$aTemp = array();
$aTemp['url'] = $r['story_url'];
$aTemp['commentary_url'] = $r['url'];
$aStory[] = $aTemp;
}
}
$request->getSession()->set('searched_bible_story_list', $aStory);
}
else if($bTopic)
{
$aTopic = array();
if(count($aRet) > 1)
{
foreach($aRet as $r)
{
$aTemp = array();
$aTemp['translation_id'] = $r['tid'];
$aTemp['translation_url'] = $r['url'];
$aTopic[] = $aTemp;
}
}
$request->getSession()->set('searched_topic_list', $aTopic);
}
return $aRet;
}
function getSearchedListByCategoryAndFormat($vCategoryId,$vFormatID,$locale, $request)
{
return $this->getListByCategory($locale, $request, $vCategoryId, $vFormatID, null);
}
function getSearchedListByCategoryAndBook($vCategoryId,$vCBookID,$locale, $request)
{
return $this->getListByCategory($locale, $request, $vCategoryId, null, $vCBookID);
}
function getBibleBookListForStory($request, $locale)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
// Not used in combo
// $sOldTestament = $request->getSession()->get('ui_str')["bible.testament.oldtestament"];
// $sNewTestament = $request->getSession()->get('ui_str')["bible.testament.newtestament"];
// $sApocrypha = $request->getSession()->get('ui_str')["bible.testament.apocrypha"];
$sSql = "select * from (SELECT distinct on (cb.id) cb.id as canonical_id, b.name, '' as testament_name, cb.testament_id, " . $sLangStr
." FROM biblebook b, biblechapter c, canonicalbiblebook cb, bibletranslation t"
." WHERE b.canonicalization_id = cb.id"
." and t.id = b.translation_id and c.book_id = b.id"
." and cb.testament_id <> " . $this->TESTAMENT_APOCRYPHA;
$sSql .= " order by cb.id, language_order) as t where language_order < 3"
." order by testament_id, canonical_id";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function hasSliderItemForWork($nPassageID,$nTranslationID, $request, $nLanguageID)
{
$bRet = false;
$ref = $this->getWorkTranslationTOC($nTranslationID, true);
if(!empty($ref))
return true;
/* $ref = $this->getWorkPassageRefs($nPassageID,$nTranslationID, $request, $nLanguageID, true);
if(!empty($ref))
return true; */
$ref = $this->getWorkRefForWork($nPassageID,$nTranslationID, $nLanguageID, true);
if(!empty($ref))
return true;
$ref = $this->getExplanationRefForWork($nPassageID, $nLanguageID, true);
if(!empty($ref))
return true;
$ref = $this->getGCEDRefForWork($nPassageID, true);
if(!empty($ref))
return true;
$ref = $this->getOTLERefForWork($nPassageID, true);
if(!empty($ref))
return true;
$ref = $this->getRelatedPassages($nPassageID, $nLanguageID, true);
if(!empty($ref))
return true;
return $bRet;
}
function getTranslationData($translationID)
{
$sSql = "SELECT t.url, t.language_id, w.swedenborgtype as swedenborgtype FROM expositiontranslation t, expositionwork w where t.id = " . $translationID . " and t.work_id = w.id limit 1";
// echo $sSql;
return $this->getRowData($sSql);
}
function getPassageData($passageID)
{
$sSql = "SELECT ordering, swedenborgsection FROM expositionpassage where id = " . $passageID . " limit 1";
// echo $sSql;
return $this->getRowData($sSql);
}
function getExplainedStoryListForSearch($locale, $sSearchText)
{
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (select distinct on (id) * from (SELECT bs.id, case when bsl.name is null then bs.name else bsl.name end as name, url, explanationtitle, commentary_url, work_id, case when bs.language_id = " . $nLanguageID . " then 1 else case when bs.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order, book_id, chapter_order, verse_order from vbiblestoryexplained bs left join (select * from biblestory_language where language_id = " . $nLanguageID . ") as bsl on bs.id = bsl.biblestory_id where ispublic";
$sSql .= " ) as t";
$sSql .= " order by id, lang_order) as t2";
if(!empty($sSearchText))
$sSql .= " where lower(name) like '%" . str_replace("'", "''", strtolower($sSearchText)) . "%'";
$sSql .= " order by book_id, chapter_order, verse_order";
//echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function setsessionswhenlogin($user_id, $user_level, $user_lang_id, $user_bible_trans_id, $audio_rate, $request)
{
if($user_lang_id == 0)
$user_lang_id = '';
if($user_bible_trans_id == 0)
$user_bible_trans_id = '';
$request->getSession()->set('user_id', $user_id);
$request->getSession()->set('user_level', $user_level);
$request->getSession()->set('current_language_id', $user_lang_id);
$request->getSession()->set('current_bible_translation_id', $user_bible_trans_id);
$request->getSession()->set('current_audio_rate', $audio_rate);
$bReceive = $this->getVerseOfDayStatus($user_id);
$request->getSession()->set('user_receive_verse_of_day', $bReceive);
}
function getVerseOfDayStatus($user_id)
{
$sSql1 = "select receive_email_by_date FROM \"user\" where \"UserID\" = " . $user_id;
//echo $sSql1;
$bReceive = $this->getSingleData($sSql1);
return $bReceive;
}
function getAudioRate($user_id)
{
$sSql1 = "select audio_rate FROM \"user\" where \"UserID\" = " . $user_id;
//echo $sSql1;
$nRet = $this->getSingleData($sSql1);
return $nRet;
}
function getShowInfo($showID)
{
$sSql = "SELECT s.title, e.description, e.location as video_url, e.notice, e.visibilitydefault, e.youtube_id FROM show s, embed e";
$sSql .= " WHERE s.id = " . $showID . " and s.embed_id = e.id";
//echo $sSql."<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$aRet = $aTemp[0];
return $aRet;
}
function getLanguageIDByShow($show_id)
{
$sSql = "select e.language_id from show s, embed e where s.id = " . $show_id . " and s.embed_id = e.id";
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getShowRefs($show_id, $request)
{
$language_id = $this->getLanguageIDByShow($show_id);
$bLangStr = ($language_id != null && $language_id != $this->LANGUAGE_ID_ENGLISH);
$sBibleStr = '(';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
$sBibleStr .= 'case when trans.id = ' . $nBibleTransID . ' then 1 else ';
if($bLangStr)
$sBibleStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sBibleStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.languagedefault then 3 else 4 end end';
if($nBibleTransID != '')
$sBibleStr .= ' end';
if($bLangStr)
$sBibleStr .= ' end';
$sBibleStr .= ')';
$sWorkStr = '(';
if($bLangStr)
$sWorkStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sWorkStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.islanguagedefault then 3 else 4 end end';
if($bLangStr)
$sWorkStr .= ' end';
$sWorkStr .= ')';
$sSql = "select t3.* from "
." (select * from (SELECT distinct on (sb.from_verse_id, sb.to_verse_id) cc1.book_id, cb.ordering as book_order, l.name as link_text1, cc1.ordering || (case when sb.from_verse_id is not null then ':' || cv1.ordering else '' end) || (case when sb.from_verse_id is not null and sb.to_verse_id is not null and sb.from_verse_id <> sb.to_verse_id then '-' || cv2.ordering else '' end) as link_text2, true as is_bibleref, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc1.ordering || '_' || cv1.ordering || (case when sb.from_verse_id <> sb.to_verse_id then '-' || cv2.ordering else '' end) as multilink_text, cc1.ordering as ordering1, case when cv1.ordering is null then 0 else cv1.ordering end as ordering2, " . $sBibleStr . " as priority_order, cb.url as book_url, trans.url as trans_url, cc1.ordering as chapter_order1, cv1.ordering as verse_order1, cv2.ordering as verse_order2, trans.url as div_url, 0 as passage_order"
." FROM show s, embed e, canonicalbiblechapter cc1, canonicalbiblebook cb, biblebook b, bibletranslation trans, show_bibleref sb"
." left join canonicalbibleverse cv1 on sb.from_verse_id = cv1.id"
." left join canonicalbibleverse cv2 on sb.to_verse_id = cv2.id"
." left join (select distinct on (book_id) book_id, name from canonicalbiblebooklanguage where language_id = " . $language_id . " and not is_abbr order by book_id, is_standard desc) l on sb.book_id = l.book_id"
." WHERE s.id = " . $show_id . " and s.embed_id = e.id and sb.show_id = s.id and sb.chapter_id = cc1.id and cc1.book_id = cb.id and sb.book_id = b.canonicalization_id and b.translation_id = trans.id and trans.enabled order by sb.from_verse_id, sb.to_verse_id, priority_order asc, trans.languagedefault desc) as b"
." union"
." select * from (SELECT distinct on (w.id, p.ordering) d.work_id as book_id, w.id as book_order, l.name as link_text1, (case when p.swedenborgsection is not null then p.swedenborgsection else '' end) as link_text2, false as is_bibleref, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "_' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' end) || trans.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as multilink_text, p.ordering as ordering1, 1 as ordering2, " . $sWorkStr . " as priority_order, trans.url as book_url, trans.url as trans_url, 0 as chapter_order1, 0 as verse_order1, 0 as verse_order2, d.url as div_url, p.ordering as passage_order"
." FROM show s, expositionpassage p, expositiondivision d, expositionwork w, expositiontranslation trans, show_workref sw"
." left join (select distinct on (work_id) work_id, name from expositionworklanguage where language_id = " . $language_id . " and not is_abbr order by work_id, otle_default desc, is_standard desc) l on sw.work_id = l.work_id"
." WHERE s.id = " . $show_id . " and sw.show_id = s.id and p.id = sw.passage_id and d.id = p.division_id and d.work_id = w.id and w.id = trans.work_id and trans.url is not null and trans.ispublic and w.ispublic order by w.id, p.ordering, priority_order asc, trans.islanguagedefault desc, trans.isoriginal desc) as e";
$sSql .= ") as t3 order by is_bibleref desc, book_order, ordering1, ordering2";
//echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$aTemp = $aRet;
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$bSameBibleChapter = false;
if($i > 0 && $aRet[$i]["book_id"] == $aRet[$i-1]["book_id"] && $aRet[$i]["is_bibleref"] == $aRet[$i-1]["is_bibleref"])
{
if($aRet[$i]["is_bibleref"])
{
if($aRet[$i]["chapter_order1"] == $aRet[$i-1]["chapter_order1"])
{
$bSameBibleChapter = true;
if($aRet[$i-1]["ordering2"] != 0)
{
// The previous ref is a whole chapter ref
$sTempText = $aRet[$i]["link_text2"];
$nPosColon = strpos($sTempText, ":");
if($nPosColon !== false)
{
$sTempText2 = substr($sTempText, $nPosColon+1);
$aRet[$i]["link_text"] = $sTempText2;
}
else
$aRet[$i]["link_text"] = $sTempText;
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text1"] . " " . $aRet[$i]["link_text2"];
}
$aRet[$i]["same_chapter"] = $bSameBibleChapter;
$bWorkRefAfterBibleRef = ($i > 0 && $aRet[$i]["is_bibleref"] != $aRet[$i-1]["is_bibleref"]);
$aRet[$i]["workref_after_bibleref"] = $bWorkRefAfterBibleRef;
$aRet[$i]["singlelink"] = '';
if($aRet[$i]["is_bibleref"])
{
if($aTemp[$i]["verse_order2"] != '' && $aTemp[$i]["verse_order1"] != $aTemp[$i]["verse_order2"])
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else if($aTemp[$i]["verse_order1"] != '')
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseIndex' => $aTemp[$i]["verse_order1"]
));
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_chapter', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"]
));
}
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('exposition_translation_division_passage', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'divisionUrls' => $aTemp[$i]["div_url"],
'passageNumber' => $aTemp[$i]["passage_order"]
));
}
}
return $aRet;
}
function getOTLEVideos($nStartNum, $request)
{
$aRet = array();
// $nMaxResult = 400;
// $nLanguageID = $this->getLanguageIDByShortCode($locale);
$searchOptions = $request->query->all();
$sSql = "SELECT s.id, s.title, e.description, e.youtube_id, case when e.youtube_id is not null and e.youtube_id <> '' then 1 else 2 end as ordering2";
$sSql .= " FROM show s, embed e";
$sSql .= " where e.visibilitydefault and s.embed_id = e.id and (s.showtype_id in (select id from showtype where public_on_ncbs) or s.showtype_id is null)";
$nBrandID = "";
if(array_key_exists('brandid', $searchOptions))
$nBrandID = $searchOptions['brandid'];
$bOffTheLeftEye = false;
if($nBrandID != '')
$bOffTheLeftEye = ($nBrandID == $this->BRAND_ID_OFF_THE_LEFT_EYE);
$nTopicID = "";
if($bOffTheLeftEye && array_key_exists('topicid', $searchOptions))
$nTopicID = $searchOptions['topicid'];
$nTypeID = "";
if($bOffTheLeftEye && array_key_exists('typeid', $searchOptions))
$nTypeID = $searchOptions['typeid'];
$sFilter = "";
if($nBrandID != '')
{
$sFilter .= " and s.brand_id = " . $nBrandID;
if($nTopicID != '')
{
$sFilter .= " and s.id in (select show_id from show_topic where topic_id = " . $nTopicID . ")";
}
if($nTypeID != '')
{
$sFilter .= " and s.showtype_id = " . $nTypeID;
}
}
else if($nStartNum == -1)
{
// get featured
$sFilter .= " and is_featured";
}
$sSql .= $sFilter;
$sSql .= " order by is_featured desc, ordering2";
// $sSql .= " LIMIT ". $nMaxResult;
if($nStartNum > 1)
{
$sSql .= " OFFSET " . $nStartNum;
}
// echo $sSql . "<br>";
$results = $this->getNativeQueryResults($sSql);
$aRet['results'] = $results;
/* $sSql = 'SELECT count(s.id)';
$sSql .= ' FROM show s, embed e';
$sSql .= ' where e.visibilitydefault and s.embed_id = e.id and (s.showtype_id in (select id from showtype where public_on_ncbs) or s.showtype_id is null)';
$sSql .= $sFilter;
$nTotalNum = $this->getSingleData($sSql); */
$nTotalNum = count($results);
// $bHasMore = (count($results) >= $nMaxResult);
$bHasMore = false;
$aRet['total_record'] = $nTotalNum;
$aRet['has_more'] = $bHasMore;
$aRet['start_num'] = $nStartNum;
return $aRet;
}
function getBrandList()
{
$sSql = "SELECT id, name"
." FROM brand where id in (select distinct brand_id from show) order by name";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getShowTopicList()
{
$sSql = "SELECT id, name"
." FROM showtopic where id in (select distinct topic_id from show_topic) order by name";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getShowTypeList()
{
$sSql = "SELECT id, name"
." FROM showtype where public_on_ncbs and id in (select distinct showtype_id from show) order by name";
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getLanguageListForAudioHub()
{
$sSql = "select id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct language_id from expositiontranslation where primaryformat_id = " . $this->PRIMARY_FORMAT_ID_AUDIO . ")";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getExplanationList($nStartNum, $locale, $request, $vFormatID=null)
{
$nCategoryId = $request->query->get('catid');
$nForceLanguageID = $request->query->get('language');
$nLanguageID = $nForceLanguageID;
if(empty($nLanguageID))
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sLangStr = '';
if($nLanguageID != null)
{
$sLangStr = 'case when t.language_id = ' . $nLanguageID . ' then 1 else';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' case when t.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 2 else';
$sLangStr .= ' 3 end';
if($nLanguageID != $this->LANGUAGE_ID_ENGLISH)
$sLangStr .= ' end';
$sLangStr .= ' as language_order';
}
else
$sLangStr .= '1 as language_order';
$sSql = "select * from (SELECT distinct on (w.id) w.id as work_id, t.id as tid,"
."t.translatedtitle AS title, t.description, t.url, wo.feature_ordering,"
. $sLangStr
. ", case when t.islanguagedefault then 1 else 2 end as lang_default_order"
. ", case when wo.is_featured then 1 else 2 end as is_featured"
. ", case when fi.image_file is null then ti.image_file else fi.image_file end as image_file, case when fi.image_file is null then ti.image_title else fi.image_title end as image_title, t.primaryformat_id, case when t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_VIDEO . " then te.youtube_id else null end as youtube_id"
." FROM expositiontranslation t join expositionwork w on t.work_id = w.id";
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1";
$sSql .= ") ti on t.id = ti.translation_id";
$sSql .= " left join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering, t2.is_featured from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") wo on w.id = wo.work_id";
$sSql .= " left join (select distinct on (d.work_id) d.work_id, e.youtube_id from passage_media pm, expositionpassage p, expositiondivision d, embed e";
$sSql .= " where e.youtube_id is not null and e.id = pm.embed_id";
$sSql .= " and pm.passage_id = p.id and p.division_id = d.id order by d.work_id, p.ordering) te on w.id = te.work_id";
$sSql .= " WHERE ";
if(!empty($nCategoryId))
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $nCategoryId . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $nCategoryId . "))";
else
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category) OR t.id IN (SELECT translation_id FROM translation_category))";
if($vFormatID != null)
$sSql .= " and t.primaryformat_id = " . $vFormatID;
$sFilter = "";
if($nForceLanguageID != '')
{
$sFilter .= " and t.language_id = " . $nForceLanguageID;
}
/* else if($nStartNum == -1)
{
// get featured
$sFilter .= " and (wo.is_featured or image_file is not null)";
} */
$sSql .= $sFilter;
$sSql .= " and t.type_id = 1";
$sSql .= " and t.ispublic AND w.ispublic and t.url is not null and t.url <> ''";
$sSql .= " ORDER BY w.id, is_featured, language_order, lang_default_order) as t where language_order < 3";
if(empty($nForceLanguageID) && $nStartNum == -1)
{
// Featured first, then Newest ones that have images. max of 15.
$sSql .= " and (is_featured = 1 or image_file is not null)";
$sSql .= " ORDER BY is_featured, tid desc";
$sSql .= " limit 15";
}
else
{
$sSql .= " ORDER BY is_featured, (case when image_file is not null and description is not null then 1 else case when image_file is not null then 2 else case when youtube_id is not null then 3 else case when description is not null then 4 else case when title is not null then 5 else 6 end end end end end), feature_ordering, title";
}
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getPrevNextLink($translation_id, $category_id, $request)
{
$aRet = array();
$locale = $request->getLocale();
$nUILanguageID = $this->getLanguageIDByShortCode($locale);
$sPrevUrl = null;
$sNextUrl = null;
$sSql = "select work_id, language_id from expositiontranslation where id = " . $translation_id;
$aTrans = $this->getRowData($sSql);
// print_r($aTrans);
$nWorkID = $aTrans['work_id'];
$nLangID = $aTrans['language_id'];
$sLangOrderStr = '(case when language_id = ' . $nUILanguageID . ' then 1 else case when language_id = ' . $nLangID . ' then 2 else case when language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then 3 else 4 end end end )';
$sSql = "SELECT t.url FROM expositiontranslation t, expositionwork w WHERE ";
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $category_id . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $category_id . "))";
$sSql .= " and t.work_id > " . $nWorkID . " and t.work_id = w.id and w.ispublic AND t.ispublic order by " . $sLangOrderStr . ", w.id limit 1";
// echo $sSql."<br>";
$sNextUrl = $this->getSingleData($sSql);
if($sNextUrl == '')
{
$sSql = "SELECT t.url FROM expositiontranslation t, expositionwork w WHERE ";
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $category_id . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $category_id . "))";
$sSql .= " and t.work_id = w.id and w.ispublic AND t.ispublic order by w.id, " . $sLangOrderStr . ", w.id limit 1";
// echo $sSql."<br>";
$sNextUrl = $this->getSingleData($sSql);
}
$aRet['next'] = $sNextUrl;
$sSql = "SELECT t.url FROM expositiontranslation t, expositionwork w WHERE ";
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $category_id . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $category_id . "))";
$sSql .= " and t.work_id < " . $nWorkID . " and t.work_id = w.id and w.ispublic AND t.ispublic order by " . $sLangOrderStr . ", w.id desc limit 1";
// echo $sSql."<br>";
$sPrveUrl = $this->getSingleData($sSql);
if($sPrveUrl == '')
{
$sSql = "SELECT t.url FROM expositiontranslation t, expositionwork w WHERE ";
$sSql .= "(t.work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $category_id . ") OR t.id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $category_id . "))";
$sSql .= " and t.work_id = w.id and w.ispublic AND t.ispublic order by w.id desc, " . $sLangOrderStr . ", w.id desc limit 1";
// echo $sSql."<br>";
$sPrveUrl = $this->getSingleData($sSql);
}
$aRet['prev'] = $sPrveUrl;
return $aRet;
}
function checkCategory($translation_id, $category_id)
{
$sSql = "SELECT id FROM expositiontranslation WHERE id = " . $translation_id;
$sSql .= " and (work_id IN (SELECT work_id FROM work_category WHERE category_id=" . $category_id . ") OR id IN (SELECT translation_id FROM translation_category WHERE category_id=" . $category_id . "))";
$sSql .= " limit 1";
// echo $sSql."<br>";
$nID = $this->getSingleData($sSql);
return ($nID != '');
}
function getClassList($nStartNum, $request)
{
$aRet = array();
// $nMaxResult = 400;
// $nLanguageID = $this->getLanguageIDByShortCode($locale);
$searchOptions = $request->query->all();
$sSql = "SELECT c.id, c.name, c.description, c.class_info_link, i.file as image_file, i.title as image_title";
$sSql .= " FROM course c left join image i on c.image_id = i.id";
$sSql .= " where c.is_public";
$sFilter = "";
$nLanguageID = "";
if(array_key_exists('language', $searchOptions))
$nLanguageID = $searchOptions['language'];
if($nLanguageID != '')
{
$sFilter .= " and c.language_id = " . $nLanguageID;
}
/* else if($nStartNum == -1)
{
// get featured
// $sFilter .= " and is_featured";
}
*/
$sSql .= $sFilter;
$sSql .= " order by ordering, id desc";
// $sSql .= " LIMIT ". $nMaxResult;
if($nStartNum > 1)
{
$sSql .= " OFFSET " . $nStartNum;
}
// echo $sSql . "<br>";
$results = $this->getNativeQueryResults($sSql);
$aRet['results'] = $results;
$nTotalNum = count($results);
// $bHasMore = (count($results) >= $nMaxResult);
$bHasMore = false;
$bHasFilter = ($sFilter != '');
$aRet['total_record'] = $nTotalNum;
$aRet['has_more'] = $bHasMore;
$aRet['start_num'] = $nStartNum;
$aRet['has_filter'] = $bHasFilter;
return $aRet;
}
function getLanguageListForClassHub()
{
$sSql = "select id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct language_id from course where language_id is not null and class_info_link is not null and image_id is not null)";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getCourseInfo($courseID)
{
$sDateFormat = "Mon dd, yyyy";
$sSql = "SELECT c.id, c.name, c.description, c.full_description, c.class_info_link, i.file as image_file, i.title as image_title, TO_CHAR(c.start_date:: DATE, '" . $sDateFormat . "') as start_date, TO_CHAR(c.end_date:: DATE, '" . $sDateFormat . "') as end_date, c.meeting_time, lt.id as locationtype_id, lt.name as locationtype, c.is_sync, case when locationtype_id = " . $this->LOCATIONTYPE_ID_VIRTUAL . " then '' else case when c.address is null then '' else c.address end || case when c.city is null then '' else '<br>' || c.city end || case when c.country_id in (237, 37) then case when es.name is null then '' else ', ' || es.name end else case when c.province is null then '' else ', ' || c.province end end || case when c.zip_code is null then '' else ' ' || c.zip_code end || case when ec.name is null then '' else '<br>' || ec.name end end as address";
$sSql .= " FROM locationtype lt, course c left join image i on c.image_id = i.id";
$sSql .= " left join enumcountry ec on c.country_id = ec.id";
$sSql .= " left join enumstate es on c.state_id = es.id";
$sSql .= " where c.id = " . $courseID . " and c.is_public and c.locationtype_id = lt.id";
//echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getLeadersForCourse($course_id)
{
$sSql = "select p.id, ";
$sSql .= " case when p.title is null then '' else p.title end";
$sSql .= " || case when p.firstname is null then '' else ' ' || p.firstname end";
$sSql .= " || case when p.middlename is null then '' else ' ' || p.middlename end";
$sSql .= " || case when p.lastname is null then '' else ' ' || p.lastname end";
$sSql .= " || case when p.suffix is null then '' else ' ' || p.suffix end as name";
$sSql .= " FROM course_person cp, person p";
$sSql .= " WHERE cp.course_id = " . $course_id . " and cp.person_id = p.id";
$sSql .= " order by p.firstname, p.lastname";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getAudienceForCourse($course_id)
{
$sSql = "select distinct a.id, a.description as name";
$sSql .= " FROM course_audience ca, audience a";
$sSql .= " WHERE ca.course_id = " . $course_id . " and ca.audience_id = a.id";
$sSql .= " order by a.description";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getCategoryListForAudioHub()
{
$sSql = "select id, name from expositioncategory where id in (select distinct category_id from expositiontranslation t, work_category wc where t.primaryformat_id = " . $this->PRIMARY_FORMAT_ID_AUDIO . " and t.work_id = wc.work_id)";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getLanguageListForSermonHub()
{
$sSql = "select id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct language_id from lncsermon where language_id is not null and publish and branch_id = " . $this->BRANCH_ID_LNC . ")";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getTopicListForSermonHub()
{
$sSql = "select id, description as name from lnctopic where id in (select distinct topic_id from lncsermon_topic where lncsermon_id in (select id from lncsermon where branch_id = " . $this->BRANCH_ID_LNC . "))";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getAuthorListForSermonHub()
{
$sSql = "select id, name from vpersonforcombo where id in (select distinct person_id from lncsermon_person where lncsermon_id in (select id from lncsermon where branch_id = " . $this->BRANCH_ID_LNC . "))";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getSermonList($nStartNum, $request)
{
$aRet = array();
// $nMaxResult = 400;
// $nLanguageID = $this->getLanguageIDByShortCode($locale);
$searchOptions = $request->query->all();
// Need to check function getSermonSearchResults in PostgresSearch.php too.
$sSql = "SELECT s.id, s.title, s.searchsummary as description, i.file as image_file, i.title as image_title, a.author, case when LOWER(mediaurl) like '%youtube.com%' then replace(mediaurl,'/watch?v=','/embed/') else mediaurl end as mediaurl";
$sSql .= " FROM lncsermon s left join image i on s.image_id = i.id";
$sSql .= " left join (select sp.lncsermon_id, array_to_string(array_agg(distinct p.name), ',') as author from person p, lncsermon_person sp where sp.person_id = p.id group by sp.lncsermon_id) as a on s.id = a.lncsermon_id";
$sSql .= " where publish";
$sFilter = "";
$sTypeIDs = "";
$sBranchIDs = "";
if(array_key_exists('storehouse', $searchOptions))
$sTypeIDs = $this->LNCSERMON_TYPE_ID_PUBLIC;
else
$sBranchIDs = $this->BRANCH_ID_LNC;
$nLanguageID = "";
if(array_key_exists('language', $searchOptions))
$nLanguageID = $searchOptions['language'];
$nTopicID = "";
if(array_key_exists('topicid', $searchOptions))
$nTopicID = $searchOptions['topicid'];
$nAuthorID = "";
if(array_key_exists('authorid', $searchOptions))
$nAuthorID = $searchOptions['authorid'];
if($sTypeIDs != '')
{
$sFilter .= " and s.type_id in (" . $sTypeIDs . ")";
}
if($sBranchIDs != '')
{
$sFilter .= " and s.branch_id in (" . $sBranchIDs . ")";
}
if($nLanguageID != '')
{
$sFilter .= " and s.language_id = " . $nLanguageID;
}
if($nTopicID != '')
{
$sFilter .= " and s.id in (select lncsermon_id from lncsermon_topic where topic_id = " . $nTopicID . ")";
}
if($nAuthorID != '')
{
$sFilter .= " and s.id in (select lncsermon_id from lncsermon_person where person_id = " . $nAuthorID . ")";
}
/* else if($nStartNum == -1)
{
// get featured
// $sFilter .= " and is_featured";
}
*/
if($sTypeIDs == '' && !$this->userCanSeeAllSermons($request))
{
$sFilter .= " and (s.type_id = ". $this->LNCSERMON_TYPE_ID_PUBLIC;
if($this->userCanSeeLNCSermons($request))
{
$sFilter .= " or s.type_id = ". $this->LNCSERMON_TYPE_ID_LNC_MEMBER;
}
$sFilter .= ")";
}
$sSql .= $sFilter;
$sSql .= " order by s.isrecommended desc, s.date desc";
// $sSql .= " LIMIT ". $nMaxResult;
if($nStartNum > 1)
{
$sSql .= " OFFSET " . $nStartNum;
}
// echo $sSql . "<br>";
$results = $this->getNativeQueryResults($sSql);
$aRet['results'] = $results;
$nTotalNum = count($results);
// $bHasMore = (count($results) >= $nMaxResult);
$bHasMore = false;
$bHasFilter = ($sFilter != '');
$aRet['total_record'] = $nTotalNum;
$aRet['has_more'] = $bHasMore;
$aRet['start_num'] = $nStartNum;
$aRet['has_filter'] = $bHasFilter;
return $aRet;
}
function getSermonInfo($sermonID)
{
$sDateFormat = "Mon dd, yyyy";
$sSql = "SELECT s.id, s.title, s.searchabletext as full_description, TO_CHAR(s.date:: DATE, '" . $sDateFormat . "') as date, s.searchsummary, s.publicfile, i.file as image_file, i.title as image_title, case when LOWER(mediaurl) like '%youtube.com%' then replace(mediaurl,'/watch?v=','/embed/') else mediaurl end as mediaurl";
$sSql .= " FROM lncsermon s left join image i on s.image_id = i.id";
$sSql .= " where s.id = " . $sermonID;
//echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
$sMediaUrl = $aRet['mediaurl'];
$youtube_id = $this->getYoutubeID($sMediaUrl);
$aRet['youtube_id'] = $youtube_id;
return $aRet;
}
function getSermonTypeID($sermonID)
{
$sSql = "SELECT type_id";
$sSql .= " FROM lncsermon";
$sSql .= " where id = " . $sermonID;
//echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getAuthorsForSermon($sermonID)
{
$sSql = "select p.id, ";
$sSql .= " case when p.title is null then '' else p.title end";
$sSql .= " || case when p.firstname is null then '' else ' ' || p.firstname end";
$sSql .= " || case when p.middlename is null then '' else ' ' || p.middlename end";
$sSql .= " || case when p.lastname is null then '' else ' ' || p.lastname end";
$sSql .= " || case when p.suffix is null then '' else ' ' || p.suffix end as name";
$sSql .= " FROM lncsermon_person sp, person p";
$sSql .= " WHERE sp.lncsermon_id = " . $sermonID . " and sp.person_id = p.id";
$sSql .= " order by p.firstname, p.lastname";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getTopicsForSermon($sermonID)
{
$sSql = "select distinct t.id, t.description as name";
$sSql .= " FROM lncsermon_topic st, topic t";
$sSql .= " WHERE st.lncsermon_id = " . $sermonID . " and st.topic_id = t.id";
$sSql .= " order by t.description";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function getLanguageIDBySermon($sermon_id)
{
$sSql = "select language_id from lncsermon s where s.id = " . $sermon_id;
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getSermonRefs($sermon_id, $request)
{
$language_id = $this->getLanguageIDBySermon($sermon_id);
$bLangStr = ($language_id != null && $language_id != $this->LANGUAGE_ID_ENGLISH);
$sBibleStr = '(';
$nBibleTransID = $request->getSession()->get('current_bible_translation_id');
if($nBibleTransID != '')
$sBibleStr .= 'case when trans.id = ' . $nBibleTransID . ' then 1 else ';
if($bLangStr)
$sBibleStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sBibleStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.languagedefault then 3 else 4 end end';
if($nBibleTransID != '')
$sBibleStr .= ' end';
if($bLangStr)
$sBibleStr .= ' end';
$sBibleStr .= ')';
$sWorkStr = '(';
if($bLangStr)
$sWorkStr .= 'case when trans.language_id = ' . $language_id . ' then 2 else ';
$sWorkStr .= 'case when trans.language_id = ' . $this->LANGUAGE_ID_ENGLISH . ' then case when trans.islanguagedefault then 3 else 4 end end';
if($bLangStr)
$sWorkStr .= ' end';
$sWorkStr .= ')';
$sSql = "select t3.* from "
." (select * from (SELECT distinct on (sb.from_verse_id, sb.to_verse_id) cc1.book_id, cb.ordering as book_order, l.name as link_text1, cc1.ordering || (case when sb.from_verse_id is not null then ':' || cv1.ordering else '' end) || (case when sb.from_verse_id is not null and sb.to_verse_id is not null and sb.from_verse_id <> sb.to_verse_id then '-' || cv2.ordering else '' end) as link_text2, true as is_bibleref, '" . $this->MULTI_URL_INDICATOR_BIBLE . "_' || trans.url || '_' || cb.url || '_' || cc1.ordering || '_' || cv1.ordering || (case when sb.from_verse_id <> sb.to_verse_id then '-' || cv2.ordering else '' end) as multilink_text, cc1.ordering as ordering1, case when cv1.ordering is null then 0 else cv1.ordering end as ordering2, " . $sBibleStr . " as priority_order, cb.url as book_url, trans.url as trans_url, cc1.ordering as chapter_order1, cv1.ordering as verse_order1, cv2.ordering as verse_order2, trans.url as div_url, 0 as passage_order"
." FROM lncsermon s, canonicalbiblechapter cc1, canonicalbiblebook cb, biblebook b, bibletranslation trans, lncsermon_bibleref sb"
." left join canonicalbibleverse cv1 on sb.from_verse_id = cv1.id"
." left join canonicalbibleverse cv2 on sb.to_verse_id = cv2.id"
." left join (select distinct on (book_id) book_id, name from canonicalbiblebooklanguage where language_id = " . $language_id . " and not is_abbr order by book_id, is_standard desc) l on sb.book_id = l.book_id"
." WHERE s.id = " . $sermon_id . " and sb.lncsermon_id = s.id and sb.chapter_id = cc1.id and cc1.book_id = cb.id and sb.book_id = b.canonicalization_id and b.translation_id = trans.id and trans.enabled order by sb.from_verse_id, sb.to_verse_id, priority_order asc, trans.languagedefault desc) as b"
." union"
." select * from (SELECT distinct on (w.id, p.ordering) d.work_id as book_id, w.id as book_order, l.name as link_text1, (case when p.swedenborgsection is not null then p.swedenborgsection else '' end) || (case when sw.to_passage_id is not null then '-' || p2.swedenborgsection else '' end) as link_text2, false as is_bibleref, (case when w.swedenborgtype is not null then '" . $this->MULTI_URL_INDICATOR_SWEDENBORG_WORK . "_' else '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' end) || trans.url || (case when p.swedenborgsection is not null then '_' || p.swedenborgsection else '' end) as multilink_text, p.ordering as ordering1, 1 as ordering2, " . $sWorkStr . " as priority_order, trans.url as book_url, trans.url as trans_url, 0 as chapter_order1, 0 as verse_order1, 0 as verse_order2, d.url as div_url, p.ordering as passage_order"
." FROM lncsermon s, expositionpassage p, expositiondivision d, expositionwork w, expositiontranslation trans, lncsermon_workref sw"
." left join (select distinct on (work_id) work_id, name from expositionworklanguage where language_id = " . $language_id . " and not is_abbr order by work_id, otle_default desc, is_standard desc) l on sw.work_id = l.work_id"
." left join expositionpassage p2 on sw.to_passage_id = p2.id"
." WHERE s.id = " . $sermon_id . " and sw.lncsermon_id = s.id and p.id = sw.passage_id and d.id = p.division_id and d.work_id = w.id and w.id = trans.work_id and trans.url is not null and trans.ispublic and w.ispublic order by w.id, p.ordering, priority_order asc, trans.islanguagedefault desc, trans.isoriginal desc) as e";
$sSql .= ") as t3 order by is_bibleref desc, book_order, ordering1, ordering2";
//echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$aTemp = $aRet;
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$bSameBibleChapter = false;
if($i > 0 && $aRet[$i]["book_id"] == $aRet[$i-1]["book_id"] && $aRet[$i]["is_bibleref"] == $aRet[$i-1]["is_bibleref"])
{
if($aRet[$i]["is_bibleref"])
{
if($aRet[$i]["chapter_order1"] == $aRet[$i-1]["chapter_order1"])
{
$bSameBibleChapter = true;
if($aRet[$i-1]["ordering2"] != 0)
{
// The previous ref is a whole chapter ref
$sTempText = $aRet[$i]["link_text2"];
$nPosColon = strpos($sTempText, ":");
if($nPosColon !== false)
{
$sTempText2 = substr($sTempText, $nPosColon+1);
$aRet[$i]["link_text"] = $sTempText2;
}
else
$aRet[$i]["link_text"] = $sTempText;
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text2"];
}
}
else
{
$aRet[$i]["link_text"] = $aRet[$i]["link_text1"] . " " . $aRet[$i]["link_text2"];
}
$aRet[$i]["same_chapter"] = $bSameBibleChapter;
$bWorkRefAfterBibleRef = ($i > 0 && $aRet[$i]["is_bibleref"] != $aRet[$i-1]["is_bibleref"]);
$aRet[$i]["workref_after_bibleref"] = $bWorkRefAfterBibleRef;
$aRet[$i]["singlelink"] = '';
if($aRet[$i]["is_bibleref"])
{
if($aTemp[$i]["verse_order2"] != '' && $aTemp[$i]["verse_order1"] != $aTemp[$i]["verse_order2"])
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse_range', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseStartIndex' => $aTemp[$i]["verse_order1"],
'verseEndIndex' => $aTemp[$i]["verse_order2"]
));
}
else if($aTemp[$i]["verse_order1"] != '')
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_verse', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"],
'verseIndex' => $aTemp[$i]["verse_order1"]
));
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('bible_chapter', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'bookUrl' => $aTemp[$i]["book_url"],
'chapterIndex' => $aTemp[$i]["chapter_order1"]
));
}
}
else
{
$aRet[$i]["singlelink"] = $this->get('router')->generate('exposition_translation_division_passage', array(
'translationUrl' => $aTemp[$i]["trans_url"],
'divisionUrls' => $aTemp[$i]["div_url"],
'passageNumber' => $aTemp[$i]["passage_order"]
));
}
}
return $aRet;
}
function userCanSeeAllSermons($request)
{
$nUserLevelID = $this->getCurrUserLevel($request);
$bRet = ($nUserLevelID == $this->USER_LEVEL_ID_ADMIN || $nUserLevelID == $this->USER_LEVEL_ID_APPLICATION_MANAGER || $nUserLevelID == $this->USER_LEVEL_ID_LNC_ADMIN);
return $bRet;
}
function userCanSeeLNCSermons($request)
{
$nUserLevelID = $this->getCurrUserLevel($request);
$bRet = ($nUserLevelID == $this->USER_LEVEL_ID_LNC_MEMBER_WEB_USER);
return $bRet;
}
function getBibleBookSummary($translationUrl, $bookUrl)
{
$sSql = "select url from (SELECT t.url, case when bt.language_id = t.language_id then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end as lang_order";
$sSql .= " FROM expositionwork w, expositiontranslation t, bibletranslation bt, biblebook b, canonicalbiblebook cb where cb.url = '" . trim(str_replace("'", "''", $bookUrl)) . "' and bt.url = '" . trim(str_replace("'", "''", $translationUrl)) . "' and cb.id = w.book_id and t.work_id = w.id and w.ispublic AND t.ispublic and b.canonicalization_id = cb.id and b.translation_id = bt.id) as t where lang_order < 3 order by lang_order limit 1";
// echo $sSql."<br>";
return $this->getSingleData($sSql);
}
function getBibleBookIDForSummary($translation_id)
{
$sSql = "SELECT w.book_id";
$sSql .= " FROM expositionwork w, expositiontranslation t where t.id = " . $translation_id . " and t.work_id = w.id limit 1";
// echo $sSql."<br>";
return $this->getSingleData($sSql);
}
function getBibleBookSummaryList($vBibleBookIDForSummary)
{
$sSql = "SELECT distinct t.id as translation_id, t.translatedtitle as explanationtitle, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end as lang_order, w.id as work_id, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name "
." FROM expositionwork w, expositiontranslation t, language l"
." WHERE w.book_id = " . $vBibleBookIDForSummary
." and t.work_id=w.id and t.language_id = l.id"
." and w.ispublic AND t.ispublic order by lang_order";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getUIStrings($request)
{
$aRet = array();
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$aStringLang = $request->getSession()->get('ui_string_lang');
$aDateMod = $request->getSession()->get('ui_string_date_mod');
$bCheckDB = false;
$bCheckDB = (empty($aStringLang) || !array_key_exists($nLanguageID, $aStringLang));
$sSql1 = "SELECT max(sl.datemod)"
." FROM ui_string_language sl, ui_string s"
." WHERE sl.language_id in (" . $nLanguageID . ", " . $this->LANGUAGE_ID_ENGLISH . ")"
." and s.id = sl.ui_string_id and s.is_active and s.type_id in (1, 3)";
$sDateMod = $this->getSingleData($sSql1);
if(!$bCheckDB)
{
//$sLastDateMod = $request->getSession()->get('ui_string_date_mod');
$bCheckDB = (empty($aDateMod) || !array_key_exists($nLanguageID, $aDateMod));
if(!$bCheckDB)
{
$bCheckDB = ($sDateMod != $aDateMod[$nLanguageID]);
}
}
$aDateMod[$nLanguageID] = $sDateMod;
$request->getSession()->set('ui_string_date_mod', $aDateMod);
if($bCheckDB)
{
$sSql = "";
if($nLanguageID == $this->LANGUAGE_ID_ENGLISH)
{
$sSql = "SELECT distinct s.name, sl.text, s.fix_quote"
." FROM ui_string_language sl, ui_string s"
." WHERE sl.language_id = " . $nLanguageID . " and s.id = sl.ui_string_id";
}
else
{
$sSql = "SELECT distinct s.name, case when sl2.id is null then sl.text else sl2.text end as text, s.fix_quote"
." FROM (select * from ui_string_language where language_id = " . $this->LANGUAGE_ID_ENGLISH . ") sl, ui_string s"
." left join (select * from ui_string_language where language_id = " . $nLanguageID . " and text is not null and text <> '') sl2 on s.id = sl2.ui_string_id where s.id = sl.ui_string_id";
}
$sSql .= " and s.is_active and s.type_id in (1, 3)";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
foreach($aTemp as $t)
{
$sCurrText = trim($t['text']);
if($t['fix_quote'])
$sCurrText = str_replace('\\\\"','\\"', str_replace('"','\\"', $sCurrText));
$aRet[$t['name']] = $sCurrText;
}
$request->getSession()->set('ui_str', $aRet);
$aStringLang[$nLanguageID] = $aRet;
$request->getSession()->set('ui_string_lang', $aStringLang);
}
else
{
//$aRet = $request->getSession()->get('ui_str');
$aRet = $aStringLang[$nLanguageID];
$request->getSession()->set('ui_str', $aRet);
// echo "languageID: ".$nLanguageID."<br>";
// print_r($aRet);
}
return $aRet;
}
function onloadPage($request)
{
$this->getUIStrings($request);
}
function getYmlData($vYml)
{
//print_r($vYml);
foreach($vYml as $key=>$item)
{
$sName = $key;
$sText = "";
foreach($item as $key2=>$item2)
{
$sName2 = $sName.".".$key2;
if (is_array($item2))
{
foreach($item2 as $key3=>$item3)
{
$sName3 = $sName2.".".$key3;
if (is_array($item3))
{
foreach($item3 as $key4=>$item4)
{
$sName4 = $sName3.".".$key4;
if (is_array($item4))
{
foreach($item4 as $key5=>$item5)
{
$sName5 = $sName4.".".$key5;
if (is_array($item5))
{
foreach($item5 as $key6=>$item6)
{
$sText = $item6;
$sCurrName = $sName5 . "." . $key6;
echo $sCurrName . "^f^" . $sText . "~|~";
}
}
else
{
$sText = $item5;
//$sCurrName = $sName5 . "." . $key5;
echo $sName5 . "^f^" . $sText . "~|~";
}
}
}
else
{
$sText = $item4;
// $sCurrName = $sName4 . "." . $key4;
echo $sName4 . "^f^" . $sText . "~|~";
}
}
}
else
{
$sText = $item3;
//$sCurrName = $sName3 . "." . $key3;
echo $sName3 . "^f^" . $sText . "~|~";
}
}
}
else
{
$sText = $item2;
// $sCurrName = $sName2 . "." . $key2;
echo $sName2 . "^f^" . $sText . "~|~";
}
}
}
}
function ui_string_replace($ui_string, $replace_array)
{
return str_replace(array_keys($replace_array), array_values($replace_array), $ui_string);
}
function getWorkTranslationsByLanguage($work_id, $language_id=null)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, t.name, t.translatedtitle, (case when b.datecreated is null then '' else ' (' || b.datecreated || ')' end) as datestr, t.url, (case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else case when l.id = " . $this->LANGUAGE_ID_LATIN . " then 2 else 3 end end) as group_order, l.righttoleft, w.title as work_title, t.isoriginal, t.language_id"
." FROM language l, expositionwork w, expositiontranslation t left join (select tb.translation_id, b.datecreated from bibliographicdata b, translation_bibliographicdata tb where b.id = tb.bibliographicdata_id) as b on t.id = b.translation_id"
." WHERE w.id = " . $work_id . " and w.ispublic and t.work_id = w.id"
." and t.ispublic and t.language_id = l.id"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
if(!empty($language_id))
$sSql .= " and t.language_id in (" . $language_id . ")";
$sSql .= " order by group_order, group_name, t.name";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$sTransName = $aRet[$i]['name'];
$sTranslatedTitle = $aRet[$i]['translatedtitle'];
$sWorkTitle = $aRet[$i]['work_title'];
$sDateStr = $aRet[$i]['datestr'];
$bOriginal = $aRet[$i]['isoriginal'];
$nLanguageID = $aRet[$i]['language_id'];
$sIdentifier = $this->convertWorkTransIdentifier($sTransName, $sTranslatedTitle, $sWorkTitle, $sDateStr, $bOriginal, $nLanguageID);
$aRet[$i]["identifier"] = $sIdentifier;
}
return $aRet;
}
function getWorkTranslationsByLanguageAllType($work_id)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, t.name, t.translatedtitle, (case when b.datecreated is null then '' else ' (' || b.datecreated || ')' end) as datestr, t.url, (case when l.id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else case when l.id = " . $this->LANGUAGE_ID_LATIN . " then 2 else 3 end end) as group_order, l.righttoleft, w.title as work_title, t.isoriginal, t.type_id, t.public_file, t.language_id"
." FROM language l, expositionwork w, expositiontranslation t left join (select tb.translation_id, b.datecreated from bibliographicdata b, translation_bibliographicdata tb where b.id = tb.bibliographicdata_id) as b on t.id = b.translation_id"
." WHERE w.id = " . $work_id . " and w.ispublic and t.work_id = w.id"
." and t.ispublic and t.language_id = l.id";
$sSql .= " order by group_order, group_name, t.name";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$sTransName = $aRet[$i]['name'];
$sTranslatedTitle = $aRet[$i]['translatedtitle'];
$sWorkTitle = $aRet[$i]['work_title'];
$sDateStr = $aRet[$i]['datestr'];
$bOriginal = $aRet[$i]['isoriginal'];
$nLanguageID = $aRet[$i]['language_id'];
$sIdentifier = $this->convertWorkTransIdentifier($sTransName, $sTranslatedTitle, $sWorkTitle, $sDateStr, $bOriginal, $nLanguageID);
$aRet[$i]["identifier"] = $sIdentifier;
}
return $aRet;
}
function getWorkTranslationShortName($vTranslationID)
{
$sRet = "";
$sSql = "SELECT t.name, t.translatedtitle, (case when b.datecreated is null then '' else ' (' || b.datecreated || ')' end) as datestr, w.title as work_title, t.isoriginal, t.language_id"
." FROM language l, expositionwork w, expositiontranslation t left join (select tb.translation_id, b.datecreated from bibliographicdata b, translation_bibliographicdata tb where b.id = tb.bibliographicdata_id) as b on t.id = b.translation_id"
." WHERE t.id = " . $vTranslationID . " and t.work_id = w.id"
." and t.language_id = l.id";
$aData = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
{
$aData = $aTemp[0];
$sTransName = $aData['name'];
$sTranslatedTitle = $aData['translatedtitle'];
$sWorkTitle = $aData['work_title'];
$sDateStr = $aData['datestr'];
$bOriginal = $aData['isoriginal'];
$nLanguageID = $aData['language_id'];
$sRet = $this->convertWorkTransIdentifier($sTransName, $sTranslatedTitle, $sWorkTitle, $sDateStr, $bOriginal, $nLanguageID);
}
return $sRet;
}
function convertWorkTransIdentifier($vTransName, $vTranslatedTitle, $vWorkTitle, $vDateStr, $vOriginal, $vLanguageID)
{
$identifier = $vTransName;
// Take out the work title if it appears
$identifier = str_ireplace(array($vTranslatedTitle, $vWorkTitle), "", $identifier);
if($vLanguageID == $this->LANGUAGE_ID_ENGLISH && $identifier == $vTransName)
{
$sTempTitle = str_replace("The ", "", $vTranslatedTitle);
if($sTempTitle != $vTranslatedTitle)
{
$identifier = str_ireplace($sTempTitle, "", $identifier);
}
}
// Take out the word "translation"
$sTranslationUIStr = $this->getTranslationUIStr($vLanguageID);
if(!empty($sTranslationUIStr))
$identifier = str_ireplace($sTranslationUIStr, "", $identifier);
// Remove whitespace and various kinds of enclosing characters from the ends
$identifier = trim($identifier, " \t\n\r\0\x0B()[]{}<>\"'");
if(empty($identifier) && $vOriginal)
$identifier = 'original' . $vDateStr;
else
$identifier .= $vDateStr;
if(trim($identifier) == '')
$identifier = $vTransName;
else
{
if(strpos($identifier, "(") !== false && strpos($identifier, ")") === false)
$identifier .= ")";
}
return $identifier;
}
function getTranslationUIStr($vLanguageID)
{
$sSql1 = "select text from ui_string_language where ui_string_id = 469 and language_id = " . $vLanguageID;
//echo $sSql1;
$sRet = $this->getSingleData($sSql1);
$sRet = trim(str_replace(":", "", $sRet));
return $sRet;
}
function getPrevNextLinkFromList($translation_id, $aList)
{
$aRet = array();
$sPrveUrl = '';
$sNextUrl = '';
$bCurrWork = false;
foreach($aList as $d)
{
if ($translation_id == $d['translation_id'])
$bCurrWork = true;
else
{
if(!$bCurrWork)
{
$sPrveUrl = $d['translation_url'];
}
else
{
$sNextUrl = $d['translation_url'];
break;
}
}
}
$aRet['prev'] = $sPrveUrl;
$aRet['next'] = $sNextUrl;
return $aRet;
}
function getLanguageIDByBibleTrans($sBibleTranslationUrl)
{
$sSql1 = "select language_id from bibletranslation where url = '" . str_replace("'", "''", $sBibleTranslationUrl) . "'";
//echo $sSql1;
$nBookLanguageID = $this->getSingleData($sSql1);
return $nBookLanguageID;
}
function showBibleChapterSummary($vLanguageID)
{
$bRet = true;
if(!empty($vLanguageID))
{
$sSql1 = "select show_bible_chapter_summary from language where id = " . $vLanguageID;
//echo $sSql1;
$bRet = $this->getSingleData($sSql1);
}
return $bRet;
}
function showBibleHighlights($vLanguageID)
{
$bRet = true;
if(!empty($vLanguageID))
{
$sSql1 = "select show_bible_verse_highlights from language where id = " . $vLanguageID;
//echo $sSql1;
$bRet = $this->getSingleData($sSql1);
}
return $bRet;
}
function isSwedenborg($vTranslationID)
{
$bRet = false;
if(!empty($vTranslationID))
{
$sSql1 = "select case when w.swedenborgtype is null then false else true end as sw from expositionwork w, expositiontranslation t where t.id = " . $vTranslationID . " and t.work_id = w.id";
//echo $sSql1;
$bRet = $this->getSingleData($sSql1);
}
return $bRet;
}
function getStoryInfo($vTranslationID)
{
$aRet = array();
if(!empty($vTranslationID))
{
$sSql1 = "select bs.level_id, sw.story_id from story_work sw, biblestory bs, expositiontranslation t where t.id = " . $vTranslationID . " and t.work_id = sw.work_id and sw.story_id = bs.id order by bs.level_id limit 1";
//echo $sSql1;
$aRet = $this->getRowData($sSql1);
}
return $aRet;
}
function getVerseData($verse_id)
{
$sSql = "select t.language_id, l.bibliographiccode, c.book_id, b.translation_id, v.chapter_id from bibleverse v, biblechapter c, biblebook b, bibletranslation t, language l where v.id = " . $verse_id . " and v.chapter_id = c.id and c.book_id = b.id and b.translation_id = t.id and t.language_id = l.id";
// echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getBibleTranslationListByLanguage($language_id=null)
{
$sGroupOrder = $this->getGroupOrderForBibleTranslation($language_id);
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, bk.url as first_book_url, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, " . $sGroupOrder . " as group_order, true as has_book, true as has_chapter, l.bibliographiccode"
." FROM language l, bibletranslation t"
." join ("
." select distinct on (t2.id) t2.id as translation_id, cb.url"
." from bibletranslation t2, biblebook b, canonicalbiblebook cb where b.translation_id = t2.id and cb.id = b.canonicalization_id"
." order by t2.id, b.ordering"
.") as bk on t.id = bk.translation_id";
$sSql .= " WHERE t.enabled and t.language_id = l.id";
$sSql .= " order by group_order, language_name, t.name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getGroupOrderForBibleTranslation($language_id)
{
$sGroupOrder = "";
if(empty($language_id))
{
$sGroupOrder = "(case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end)";
}
else
{
$sGroupOrder = "(case when language_id = " . $language_id . " then 1 else case when language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end)";
}
return $sGroupOrder;
}
function getLanguageStrForMeta($vLanguageID)
{
$sRet = "";
if(!empty($vLanguageID))
{
$sSql1 = "select '- ' || (case when nativename is not null and nativename <> '' and nativename <> name then nativename else name end) as name from language where id = " . $vLanguageID;
//echo $sSql1;
$sRet = $this->getSingleData($sSql1);
}
return $sRet;
}
function getCVerseInfo($vVerseIDs)
{
$aRet = array();
if(!empty($vVerseIDs))
{
$sSql1 = "select string_agg(distinct cv.id::text, ',') as cverse_ids, string_agg(distinct cc.id::text, ',') as cchapter_ids, string_agg(distinct cc.ordering::text, ',' order by cc.ordering::text) as cchapter_orders from canonicalbibleverse cv, canonicalbiblechapter cc, canonicalbiblebook cb, bibleverse v, (select * from canonicalbibleverse_bibleverse where bibleverse_id in (" . $vVerseIDs . ")) as cvv where v.id in (" . $vVerseIDs . ") and v.id = cvv.bibleverse_id and cv.id = cvv.canonicalbibleverse_id and cv.chapter_id = cc.id";
// echo $sSql1;
$aRet = $this->getRowData($sSql1);
if(!empty($aRet))
{
$sChapterOrders = $aRet['cchapter_orders'];
$aChapterOrder = explode(",",$sChapterOrders);
$nChapterOrder = $aChapterOrder[0];
$aRet['first_chapter_index'] = $nChapterOrder;
}
}
return $aRet;
}
function getBibleTranslationListForLanguage($language_id)
{
$sSql = "SELECT t.id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name, t.name as name, t.url, bk.url as first_book_url, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as group_name, 1 as group_order, true as has_book, true as has_chapter, l.bibliographiccode"
." FROM language l, bibletranslation t"
." join ("
." select distinct on (t2.id) t2.id as translation_id, cb.url"
." from bibletranslation t2, biblebook b, canonicalbiblebook cb where b.translation_id = t2.id and cb.id = b.canonicalization_id"
." order by t2.id, b.ordering"
.") as bk on t.id = bk.translation_id";
$sSql .= " WHERE l.id = " . $language_id . " and t.enabled and t.language_id = l.id";
$sSql .= " order by group_order, language_name, t.name";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getYoutubeID($vMediaUrl)
{
$sRet = null;
$sStrToCheck = "youtube.com/embed/";
$nPos = strpos($vMediaUrl, $sStrToCheck);
if($nPos !== false)
{
$nLen = strlen($sStrToCheck);
$sRet = substr($vMediaUrl, $nPos+$nLen);
$nPosQ = strpos($sRet, "&");
if($nPosQ !== false)
{
$sRet = substr($sRet, 0, $nPosQ);
}
}
return $sRet;
}
function getLanguageListForConceptHub()
{
$sSql = "select id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct t.language_id from concept_work cl, expositiontranslation t where cl.work_id = t.work_id)";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getWorkTranslationData($translation_id)
{
$sSql = "SELECT language_id, work_id"
." FROM expositiontranslation"
." WHERE id = " . $translation_id;
// echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getChapterExplanationsForPopup($translation_id)
{
$aTrans = $this->getWorkTranslationData($translation_id);
$nLanguageID = $aTrans['language_id'];
$nWorkID = $aTrans['work_id'];
$sSql = "SELECT t.id as translation_id, t.translatedtitle as explanationtitle, bs.chapter_id as cchapter_id, null as chapter_id, cbc.ordering as chapter_order, t.url as translation_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $translation_id . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, w.id as work_id, t.is_modern, bs.id as story_id, t.language_id, (case when l.nativename is not null and l.nativename <> '' then l.nativename else l.name end) as language_name"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc, language l"
." WHERE bs.chapter_id in (select cbc2.id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $nWorkID . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.language_id = l.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by lang_order, language_name, t.is_modern desc, trans_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getAuthorListForStoryHub()
{
$nCategoryId = $this->CATEGORY_ID_BIBLE_STORY;
$sSql = "select id, name from vpersonforcombo where id in (select distinct bp.person_id from bibliographicdata_person bp, translation_bibliographicdata tb, expositiontranslation t where bp.bibliographicdata_id = tb.bibliographicdata_id and t.id = tb.translation_id and t.work_id IN (SELECT sw.work_id FROM story_work sw, biblestory st where st.level_id = " . $this->STORY_LEVEL_ID_BIBLE . " and sw.story_id = st.id))";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getSWWorkTranslations($work_ids, $language_id, $work_type)
{
$sSql = "SELECT t.id as translation_id, t.name, t.translatedtitle, (case when b.datecreated is null then '' else ' (' || b.datecreated || ')' end) as datestr, t.url, l.righttoleft, w.title as work_title, t.isoriginal, w.id as work_id, t.language_id"
." FROM language l, expositionwork w, expositiontranslation t left join (select tb.translation_id, b.datecreated from bibliographicdata b, translation_bibliographicdata tb where b.id = tb.bibliographicdata_id) as b on t.id = b.translation_id"
." WHERE w.swedenborgtype is not null and w.ispublic";
if(!empty($work_ids))
$sSql .= " and w.id in (" . $work_ids . ")";
if(!empty($language_id))
$sSql .= " and t.language_id = " . $language_id;
if(!empty($work_type))
$sSql .= " and w.swedenborgtype = '" . $work_type . "'";
$sSql .= " and t.ispublic and t.work_id = w.id and t.language_id = l.id"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by work_id, t.name";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$sTransName = $aRet[$i]['name'];
$sTranslatedTitle = $aRet[$i]['translatedtitle'];
$sWorkTitle = $aRet[$i]['work_title'];
$sDateStr = $aRet[$i]['datestr'];
$bOriginal = $aRet[$i]['isoriginal'];
$nLanguageID = $aRet[$i]['language_id'];
$sIdentifier = $this->convertWorkTransIdentifier($sTransName, $sTranslatedTitle, $sWorkTitle, $sDateStr, $bOriginal, $nLanguageID);
$aRet[$i]["identifier"] = $sIdentifier;
}
return $aRet;
}
/* function showEnglishTrans($vLanguageID)
{
return ($vLanguageID == $this->LANGUAGE_ID_ZULU);
}*/
function fixVerseText($vStr)
{
$sRet = preg_replace("/~ppss~[^~]+~ppee~/", "", $vStr);
$sRet = preg_replace("/\<sup\>[^\<]+\<\/sup\>/", "", $sRet);
$sRet = str_replace('"', "'", $sRet);
$sRet = str_replace(array("\r", "\n"), " ", $sRet);
$sRet = strip_tags($sRet);
return $sRet;
}
function getWorkPassageContents($passage_id,$translation_id)
{
$sSql = "select array_to_string(array_agg(d.text ORDER BY tu.ordering, d.ordering),'') as content from expositiontranslationtext tt, expositiontextunit tu, expositiondisplayitem d where tu.passage_id = " . $passage_id . " and tt.translation_id = " . $translation_id;
$sSql .= " and tu.id = tt.placement_id and d.container_id = tt.id";
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function fixWorkText($vStr)
{
$sRet = strip_tags($vStr);
$sRet = str_replace(array("\r", "\n"), " ", $sRet);
$sRet = str_replace("---", "", $sRet);
$sRet = str_replace('"', "'", $sRet);
$sRet = preg_replace('!\s+!', ' ', $sRet);
return $sRet;
}
function getWorkTranslationDesc($translation_id)
{
$sSql = "SELECT description"
." FROM expositiontranslation"
." WHERE id = " . $translation_id;
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function getExplanationContents($translation_id)
{
$sSql = "select array_to_string(array_agg(d.text ORDER BY tu.ordering, d.ordering),'') as content from expositiontranslationtext tt, expositiontextunit tu, expositiondisplayitem d where tt.translation_id = " . $translation_id;
$sSql .= " and tu.id = tt.placement_id and d.container_id = tt.id";
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}
function isExplainedVerse($translation_id, $verse_id)
{
$sSql = "select v.id as verse_id FROM (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_VERSE . ") as bs, canonicalbibleverse cv, bibleverse v, canonicalbibleverse_bibleverse cvv,"
." expositionwork w, expositiontranslation t, story_work sw"
." WHERE t.id = " . $translation_id . " and v.id = " . $verse_id
." and t.work_id=w.id and w.id = sw.work_id and sw.story_id = bs.id"
." and bs.startverse_id = cv.id"
." and cv.id = cvv.canonicalbibleverse_id and v.id = cvv.bibleverse_id"
." and w.ispublic and t.ispublic";
// echo $sSql."<br>";
$nID = $this->getSingleData($sSql);
return ($nID != '');
}
function replacePopupStr($str)
{
$sRet = $str;
$sPopupStartMark = "~ppss~";
$sPopupEndMark = "~ppee~";
$sRet = str_replace($sPopupStartMark, '<a data-toggle="popover" style="cursor: pointer;" data-content="', $sRet);
$sRet = str_replace($sPopupEndMark, '"></a>', $sRet);
return $sRet;
}
function getDailyVerse($request)
{
$sRet = "";
$sSql = "SELECT text FROM page_message WHERE id = " . $this->PAGE_MESSAGE_ID_EMAIL_BY_DATE;
//echo $sSql."<br>";
$sText = $this->getSingleData($sSql);
if(!empty($sText))
{
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT startverse_id, endverse_id, commentary, work_id, passage_id, subsection_num FROM emailbydate WHERE date_to_send = CURRENT_DATE limit 1";
//echo $sSql."<br>";
$aData = $this->getRowData($sSql);
if(!empty($aData))
{
$startverse_id = trim($aData['startverse_id']);
$endverse_id = trim($aData['endverse_id']);
$commentary = trim($aData['commentary']);
$work_id = trim($aData['work_id']);
$passage_id = trim($aData['passage_id']);
$subsection_num = trim($aData['subsection_num']);
$bMultipleVerses = (!empty($endverse_id) && $endverse_id != $startverse_id);
$sVerses = "";
$sBibleTransIDs = "";
// $aBibleTransIDUnique = array();
$aVerse = array();
$aCVerseID = array();
$sLinkStart = "";
$sVerseLinkText = "";
// Get first verse info
$sVerseUrl = $sLinkStart;
$sVerseUrlEnd = "";
$sWorkLink = "";
// Link to full chapter page
$sSql1 = "SELECT bc.id as chapter_id, cbv.ordering as verse_order, t.url || '/' || cb.url || '/' || cc.ordering as link, cc.ordering || ':' || cbv.ordering as linktext,";
$sSql1 .= " bv.content";
$sSql1 .= " as text, cb.id as cbook_id, cc.id as cchapter_id, cc.ordering as cchapter_order, bc.book_id, cbv.id as cverse_id, cb.url || '/' || cc.ordering as link_end FROM bibleverse bv, biblechapter bc, biblebook b, bibletranslation t, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, canonicalbiblebook cb where t.language_id = " . $nLanguageID . " and cbv.id = " . $startverse_id . " and cbv.id = cbv_bv.canonicalbibleverse_id and bv.id = cbv_bv.bibleverse_id and bv.chapter_id = bc.id and bc.book_id = b.id and b.translation_id = t.id and cbv.chapter_id = cc.id and cc.book_id = cb.id order by t.languagedefault desc limit 1";
// echo $sSql1 . "<br>";
$aFirstVerse = $this->getRowData($sSql1);
if(!empty($aFirstVerse))
{
$nFirstChapterID = $aFirstVerse['chapter_id'];
$nFirstVerseOrder = $aFirstVerse['verse_order'];
$sVerseUrl .= 'bible/'.$aFirstVerse['link'];
$sVerseLinkText = $aFirstVerse['linktext'];
$sVerses .= $this->replacePopupStr($aFirstVerse['text']);
$nFirstCBookID = $aFirstVerse['cbook_id'];
$nFirstCChapterID = $aFirstVerse['cchapter_id'];
$nFirstCChapterOrder = $aFirstVerse['cchapter_order'];
$nFirstBookID = $aFirstVerse['book_id'];
$nFirstCVerseID = $aFirstVerse['cverse_id'];
$sVerseUrlEnd = $aFirstVerse['link_end'];
$aCVerseID[] = $nFirstCVerseID;
// Get book name
$sSql2 = "select name from canonicalbiblebooklanguage where book_id = " . $nFirstCBookID . " and language_id = " . $nLanguageID . " and not is_abbr order by is_standard desc limit 1";
$sBookName = $this->getSingleData($sSql2);
$sVerseLinkText = $sBookName . " " . $sVerseLinkText;
if($bMultipleVerses)
{
//$sSql2 = "select ordering from canonicalbibleverse where id = " . $endverse_id;
$sSql2 = "SELECT cc.book_id as cbook_id, cc.id as cchapter_id, cbv.ordering as verse_order, bc.id as chapter_id, bc.book_id, cc.ordering as cchapter_order FROM bibleverse bv, biblechapter bc, biblebook b, bibletranslation t, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, canonicalbiblebook cb where t.language_id = " . $nLanguageID . " and cbv.id = " . $endverse_id . " and cbv.id = cbv_bv.canonicalbibleverse_id and bv.id = cbv_bv.bibleverse_id and bv.chapter_id = bc.id and bc.book_id = b.id and b.translation_id = t.id and cbv.chapter_id = cc.id and cc.book_id = cb.id order by t.languagedefault desc limit 1";
$aSecondVerse = $this->getRowData($sSql2);
$nLastCBookID = $aSecondVerse['cbook_id'];
$nLastCChapterID = $aSecondVerse['cchapter_id'];
$nLastVerseOrder = $aSecondVerse['verse_order'];
$nLastChapterID = $aSecondVerse['chapter_id'];
$nLastBookID = $aSecondVerse['book_id'];
$nLastCChapterOrder = $aSecondVerse['cchapter_order'];
$sSql3 = "";
$bSameChapter = false;
$bSameBook = false;
if($nFirstCChapterID == $nLastCChapterID)
{
$bSameChapter = true;
$bSameBook = true;
$sSql3 = "SELECT bv.content as text, cbv.id FROM bibleverse bv, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv where bv.chapter_id = " . $nFirstChapterID . " and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.ordering > " . $nFirstVerseOrder . " and cbv.ordering <= " . $nLastVerseOrder . " order by cbv.ordering";
}
else if($nFirstCBookID == $nLastCBookID)
{
$bSameBook = true;
$sSql3 = "SELECT bv.content as text, cbv.id FROM bibleverse bv, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, biblechapter bc where bc.book_id = " . $nFirstBookID . " and bv.chapter_id = bc.id and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.chapter_id = cc.id and ((bv.chapter_id = " . $nFirstChapterID . " and cbv.ordering > " . $nFirstVerseOrder . ") or (cc.ordering > " . $nFirstCChapterOrder . " and cc.ordering < " . $nLastCChapterOrder . ") or (bv.chapter_id = " . $nLastChapterID . " and cbv.ordering <= " . $nLastVerseOrder . ")) order by cc.ordering, cbv.ordering";
}
else
{
$sSql3 = "select * from (SELECT bv.content as text, cbv.id, cb.ordering as book_order, cc.ordering as chapter_order, cbv.ordering as verse_order FROM bibleverse bv, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, canonicalbiblebook cb, biblechapter bc where bc.book_id = " . $nFirstBookID . " and bv.chapter_id = bc.id and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.chapter_id = cc.id and cc.book_id = cb.id and ((bv.chapter_id = " . $nFirstChapterID . " and cbv.ordering > " . $nFirstVerseOrder . ") or (cc.ordering > " . $nFirstCChapterOrder . ")) ";
$sSql3 .= " union SELECT bv.content as text, cbv.id, cb.ordering as book_order, cc.ordering as chapter_order, cbv.ordering as verse_order FROM bibleverse bv, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, canonicalbiblebook cb, biblechapter bc where bc.book_id = " . $nLastBookID . " and bv.chapter_id = bc.id and bv.id = cbv_bv.bibleverse_id and cbv.id = cbv_bv.canonicalbibleverse_id and cbv.chapter_id = cc.id and cc.book_id = cb.id and ((bv.chapter_id = " . $nLastChapterID . " and cbv.ordering <= " . $nLastVerseOrder . ") or (cc.ordering < " . $nLastCChapterOrder . ")) ) as t order by book_order, chapter_order, verse_order";
}
// echo $sSql3 . "<br>";
$aOtherVerse = $this->getListData($sSql3);
if(!empty($aOtherVerse))
{
// $sVerseLink .= "-".$nLastVerseOrder;
// $sVerseUrl .= "-".$nLastVerseOrder;
if($bSameChapter)
$sVerseLinkText .= "-".$nLastVerseOrder;
else if($bSameBook)
{
$sVerseLinkText .= "-".$nLastCChapterOrder.":".$nLastVerseOrder;
}
else
{
// Get last book name
$sSql2 = "select name from canonicalbiblebooklanguage where book_id = " . $nLastCBookID . " and language_id = " . $nLanguageID . " and not is_abbr order by is_standard desc limit 1";
$sLastBookName = $this->getSingleData($sSql2);
$sVerseLinkText .= " - " . $sLastBookName . " " . $nLastCChapterOrder.":".$nLastVerseOrder;
}
//echo $sVerseLinkText;
foreach($aOtherVerse as $v)
{
$sVerses .= " ". $this->replacePopupStr($v['text']);
$aCVerseID[] = $v['id'];
}
}
}
}
// $sVerseLink = "<a href='" . $sVerseLink . "'>" . $sVerseLinkText . "</a>";
// $sVerses = $sVerseLinkText . "<br><br>" . $sVerses;
$aCVerseContent = array();
$sCVerseIDs = implode(",", $aCVerseID);
if(!empty($sBibleTransIDs) && !empty($sCVerseIDs))
{
$sSql2 = "SELECT b.translation_id, array_to_string(array_agg(bv.content ORDER BY cb.ordering, cc.ordering, cbv.ordering), ' ') as text FROM bibleverse bv, biblechapter bc, biblebook b, canonicalbibleverse_bibleverse cbv_bv, canonicalbibleverse cbv, canonicalbiblechapter cc, canonicalbiblebook cb where b.translation_id in (" . $sBibleTransIDs . ") and cbv.id in (" . $sCVerseIDs . ") and cbv.id = cbv_bv.canonicalbibleverse_id and bv.id = cbv_bv.bibleverse_id and bv.chapter_id = bc.id and bc.book_id = b.id and cbv.chapter_id = cc.id and cc.book_id = cb.id group by b.translation_id";
// echo $sSql2 . "<br>";
$aTemp = $this->getListData($sSql2);
foreach($aTemp as $t)
{
$aCVerseContent[$t['translation_id']] = $this->replacePopupStr($t['text']);
}
}
if(!empty($passage_id))
{
// Get work link
$sSql2 = "SELECT distinct on (w.id) w.id, p.swedenborgsection, 'exposition/translation/' || t.url || '/' || d.url || '/' || p.ordering as link"
." FROM expositionwork w, expositiontranslation t, expositionpassage p, expositiondivision d"
." WHERE p.id = " . $passage_id . " and p.division_id = d.id and d.work_id = w.id and t.work_id=w.id and t.language_id = ".$nLanguageID. " and t.ispublic and w.ispublic";
$sSql2 .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql2 .= " order by w.id, t.islanguagedefault desc";
$aWork = $this->getRowData($sSql2);
if(!empty($aWork))
{
$nWorkID = $aWork['id'];
$sSql2 = "SELECT name from expositionworklanguage where language_id = " . $nLanguageID . " and work_id = " . $nWorkID . " order by is_standard desc";
// echo $sSql2;
$sWorkName = $this->getSingleData($sSql2);
$sRef = $sWorkName.' '.$aWork['swedenborgsection'];
if($subsection_num > 1)
$sRef .= "[".$subsection_num."]";
$sLinkStr = $aWork['link'];
$sWorkLink = "<a href='".$sLinkStart.$sLinkStr."'>".$sRef."</a>";
}
}
$sText = str_replace('[VERSE_RANGE_TITLE]', $sVerseLinkText, $sText);
$sText = str_replace('[VERSE_RANGE_TEXT]', $sVerses, $sText);
$sText = str_replace('[BIBLE_SOURCE_URL]', $sVerseUrl, $sText);
$sText = str_replace('[WORK_LINK]', $sWorkLink, $sText);
$sText = str_replace('[COMMENTARY]', $commentary, $sText);
$sRet = $sText;
}
}
return $sRet;
}
function getCategoryList($language_id, $category_id, $locale)
{
$nUILanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT t.translatedtitle AS title, t.url as translation_url, d.url as division_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' || t.url as ref_column_spec, w.id as work_id, t.id as translation_id"
." FROM expositionwork w, expositiontranslation t, (select distinct on (work_id) work_id, url, min(ordering) as ordering from expositiondivision group by work_id, url) as d"
." WHERE t.work_id=w.id and w.id in (select work_id from work_category where category_id = " . $category_id . ")"
." and d.work_id = w.id AND w.ispublic AND t.ispublic"
." AND w.swedenborgtype IS NULL"
." AND t.translatedtitle IS NOT NULL and t.id in (";
$sSql .= "SELECT distinct on (w.id) t.id"
." FROM expositionwork w, expositiontranslation t WHERE ";
$sSql .= " t.work_id=w.id order by w.id, (case when t.language_id = " . $nUILanguageID . " then 1 else case when t.language_id = " . $language_id . " then 2 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 3 else 4 end end end), t.islanguagedefault desc)"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by w.ordering, t.translatedtitle";
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
function getReadingPlanStepBasicInfo($readingPlanUrl, $stepOrder, $request)
{
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "SELECT rs.id, rs.name, rs.text, rs.closing_text, rs.image_file, rs.image_title, rs.video_link, rs.youtube_id, rs.video_notice, ";
$sSql .= "case when rl.name is null then r.name else rl.name end";
$sSql .= " as plan_title, rs.ordering as step_order, rs.type_id, rs.work_id, rs.passage_id, rs.explanation_id";
$sSql .= " FROM ";
$sSql .= " (select rsa.id, case when rsl.name is null then rsa.name else rsl.name end as name, rsa.readingplan_id, rsa.startverse_id, rsa.endverse_id, rsa.ordering, rsa.type_id, rsa.work_id, rsa.passage_id, rsa.explanation_id, case when rsl.text is null then rsa.text else rsl.text end as text, case when rsl.closing_text is null then rsa.closing_text else rsl.closing_text end as closing_text, i.file as image_file, i.title as image_title, e.location as video_link, e.youtube_id, e.notice as video_notice from readingplanstep rsa left join (select * from readingplanstep_language where language_id = " .$nLanguageID . ") rsl on rsa.id = rsl.readingplanstep_id left join image i on rsa.image_id = i.id left join (select * from embed where visibilitydefault) e on rsa.embed_id = e.id) rs, ";
$sSql .= " readingplan r left join (select * from readingplan_language where language_id = " .$nLanguageID . ") rl on r.id = rl.readingplan_id ";
$sSql .= " where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." and rs.ordering = " . $stepOrder;
// echo $sSql . "<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getDefaultWorkTranslation($work_id, $request)
{
$locale = $request->getLocale();
$sLangStr = "(case when t.language_id in (select id from language where shortcode = '" . trim(str_replace("'", "''", $locale)) . "') then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 else 3 end end)";
$sSql = "SELECT t.id, t.url"
." FROM language l, expositionwork w, expositiontranslation t"
." WHERE w.id = " . $work_id . " and w.ispublic and t.work_id = w.id"
." and t.ispublic and t.language_id = l.id"
." AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT;
$sSql .= " order by " . $sLangStr . ", t.islanguagedefault desc limit 1";
// echo $sSql."<br>";
$aRet = $this->getRowData($sSql);
return $aRet;
}
function getChapterExplanationWorksForPopup($translation_id)
{
$aTrans = $this->getWorkTranslationData($translation_id);
$nLanguageID = $aTrans['language_id'];
$nWorkID = $aTrans['work_id'];
$sSql = "SELECT * from (select distinct on (work_id) t.id as translation_id, t.translatedtitle as explanationtitle, bs.chapter_id as cchapter_id, null as chapter_id, cbc.ordering as chapter_order, t.url as translation_url, '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "' || '_' || t.url as explanation_spec, case when t.language_id = " . $nLanguageID . " then 1 else case when t.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 2 end end as lang_order, case when t.id = " . $translation_id . " then 1 else case when t.islanguagedefault then 2 else 3 end end as trans_order, w.id as work_id, t.is_modern, bs.id as story_id"
." FROM expositionwork w, expositiontranslation t, story_work sw, biblestory bs, canonicalbiblechapter cbc, language l"
." WHERE bs.chapter_id in (select cbc2.id from biblestory bs2, story_work sw2, canonicalbiblechapter cbc2 where sw2.work_id = " . $nWorkID . " and sw2.story_id = bs2.id and cbc2.id = bs2.chapter_id) and sw.story_id = bs.id and w.id = sw.work_id and t.work_id=w.id and t.language_id = l.id"
." and cbc.id = bs.chapter_id and w.ispublic AND t.ispublic order by w.id, lang_order, t.is_modern desc, trans_order) as t order by lang_order, is_modern desc, trans_order";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getWorkPassages($work_id)
{
$sSql = "SELECT p.swedenborgsection as passage_num"
." FROM expositionpassage p, expositiondivision d"
." WHERE d.work_id = " . $work_id . " and p.division_id = d.id"
." order by p.ordering";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
// Get refs for "Kempton view"
function getBibleCrossRef($aVerseInfo, $bChapter, $bCheck=false)
{
if(empty($aVerseInfo))
return array();
$verseLanguage = null;
$sCVerseIDs = $aVerseInfo['CVerseIDs'];
$verseLanguage = $aVerseInfo['LanguageID'];
if($verseLanguage == '')
$verseLanguage = $this->LANGUAGE_ID_ENGLISH;
$sCChapterIDs = $aVerseInfo['CChapterIDs'];
if($sCVerseIDs == '' && $sCChapterIDs == '') {
return array();
} else {
return $this->getBibleCrossRefInfo($bChapter, $verseLanguage, $sCVerseIDs, $sCChapterIDs, $bCheck);
}
}
function getBibleCrossRefInfo($bChapter, $language_id, $sCVerseIDs, $sChapterIDs, $bCheck)
{
if($sCVerseIDs == '' && $sChapterIDs == '')
return array();
$nDefaultLanguageID = $this->LANGUAGE_ID_ENGLISH; // English
$sTable = " (";
if ($sCVerseIDs <> '')
{
$aCVerseID = explode(",", $sCVerseIDs);
$nFirstCVerseID = current($aCVerseID);
$nLastCVerseID = end($aCVerseID);
$bSingleVerse = ($nFirstCVerseID == $nLastCVerseID);
if($bSingleVerse)
{
$sTable .= "select rb.id, rc.originallinktext as bible_ref FROM refbible rb, refcombined rc, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbibleverse cv2, canonicalbiblechapter cc2, canonicalbibleverse cva where cc1.id in (" . $sChapterIDs . ") and cc2 = cc1 and cv1.chapter_id = cc1.id and cv1.id = rb.startverse_id and rc.id = rb.container_id and cv2.chapter_id = cc2.id and cv2.id = rb.endverse_id and cva.id = " . $nFirstCVerseID . " and cv1.ordering = cva.ordering and cv2.ordering = cv1.ordering";
}
else
{
$sTable .= "select rb.id, rc.originallinktext as bible_ref, cc1.ordering as chapter_order, cv1.ordering as verse_order1, cv2.ordering as verse_order2 FROM refbible rb, refcombined rc, canonicalbibleverse cv1, canonicalbiblechapter cc1, canonicalbibleverse cv2, canonicalbiblechapter cc2, canonicalbibleverse cva, canonicalbibleverse cvb where cc1.id in (" . $sChapterIDs . ") and cc2 = cc1 and cv1.chapter_id = cc1.id and cv1.id = rb.startverse_id and cv2.chapter_id = cc2.id and cv2.id = rb.endverse_id and rc.id = rb.container_id and cva.id = " . $nFirstCVerseID . " and cvb.id = " . $nLastCVerseID . " and cv1.ordering >= cva.ordering and cv2.ordering <= cvb.ordering";
}
}
$sTable .= ") as br";
$sSql = "select * from (SELECT distinct on (p.id, rc.originallinktext, br.bible_ref) p.id, rc.originallinktext as work_ref, br.bible_ref";
if(!$bSingleVerse)
$sSql .= ", br.chapter_order, br.verse_order1, br.verse_order2";
$sSql .= ", t.translatedtitle AS title, p.swedenborgsection as num, t.url as translation_url, d.url as division_url, w.ordering as work_order, p.ordering, case when t.language_id = " . $language_id . " then 1 else 2 end as lang_order"
." FROM expositionwork w, expositiontranslation t, expositiontextunit tu, expositionpassage p, expositiondivision d, ref_code r_c, refcombined rc, refexposition re, " . $sTable
." WHERE r_c.refbible_id = br.id and r_c.refexposition_id = re.id and rc.id = re.container_id and t.work_id=w.id and d.work_id = w.id and p.division_id = d.id and tu.id = re.startlocation_id and tu.passage_id = p.id";
if($language_id == $nDefaultLanguageID)
{
$sSql .= " and t.language_id = " . $language_id;
}
else
{
$sSql .= " and t.language_id in ( ".$language_id. "," . $nDefaultLanguageID . ")";
}
$sSql .= " and t.ispublic and w.ispublic and t.url is not null and t.url <> ''";
$sSql .= " AND t.type_id = " . $this->TRANSLATION_TYPE_ID_TEXT . " order by p.id, rc.originallinktext, br.bible_ref, lang_order, t.islanguagedefault desc";
if($bCheck)
$sSql .= " limit 1";
if($bSingleVerse)
$sSql .= ") as t2 order by title, ordering";
else
$sSql .= ") as t2 order by chapter_order, case when bible_ref like '%:%' then 2 else 1 end, verse_order1, verse_order2, bible_ref, title, ordering";
// echo $sSql."<br>";
$aRet = $this->getNativeQueryResults($sSql);
$nLen = count($aRet);
for($i=0;$i<$nLen;$i++)
{
$sWorkRef = $aRet[$i]["work_ref"];
$sNumPart = $this->getRefNumPart($sWorkRef);
$sSubSection = "";
if(is_numeric($sNumPart))
$aRet[$i]['passage_str'] = $aRet[$i]['num'];
else
{
$sPattern = '/\[(\d+)\]/';
$nCount = preg_match_all($sPattern, $sNumPart, $matches);
if ($nCount > 0)
{
$sSubSection = trim(current(end($matches)));
$sNumPart = str_replace(" [", "[", $sNumPart);
}
$aRet[$i]['passage_str'] = $sNumPart;
}
$aRet[$i]['subsection'] = $sSubSection;
}
return $aRet;
}
function getRefNumPart($vRef)
{
$sRet = '';
// first check patterns like 'Marriage (Index) 1 30' (the book name has a number at the end)
$sPattern = '/[^\d]\s[\d]+\s(\d[\d\s\[\]\-:]*)/';
$nCount = preg_match_all($sPattern, $vRef, $matches);
if ($nCount > 0)
$sRet = trim(current(end($matches)));
else
{
// if no match, check patterns like 'Ezekiel 47:1-23' (the book name has no number at the end)
$sPattern = '/[^\d]\s(\d[\d\s\[\]\-:]*)/';
$nCount = preg_match_all($sPattern, $vRef, $matches);
if ($nCount > 0)
$sRet = trim(current(end($matches)));
}
return $sRet;
}
function canUseSpeechToText()
{
$user_agent = @$_SERVER['HTTP_USER_AGENT'];
$sPattern = '/(Chrome\/|Edg\/)/';
$nCount = preg_match_all($sPattern, $user_agent, $matches);
$bRet = ($nCount > 0);
return $bRet;
}
function getLanguageForSpeech($locale)
{
// not support tl and la now.
$sRet = "en-US";
if($locale == 'fr')
$sRet = "fr-FR";
elseif($locale == 'zh')
$sRet = "zh";
elseif($locale == 'de')
$sRet = "de-DE";
elseif($locale == 'es')
$sRet = "es-ES";
elseif($locale == 'it')
$sRet = "it-IT";
elseif($locale == 'bg')
$sRet = "bg-BG";
elseif($locale == 'cs')
$sRet = "cs-CZ";
elseif($locale == 'nl')
$sRet = "nl-NL";
elseif($locale == 'ka')
$sRet = "ka-GE";
elseif($locale == 'hi')
$sRet = "hi-IN";
elseif($locale == 'ko')
$sRet = "ko-KR";
elseif($locale == 'pt')
$sRet = "pt-PT";
elseif($locale == 'sv')
$sRet = "sv-SE";
elseif($locale == 'uk')
$sRet = "uk-UA";
elseif($locale == 'zu')
$sRet = "zu-ZA";
elseif($locale == 'sr')
$sRet = "sr-RS";
elseif($locale == 'ml')
$sRet = "ml-IN";
elseif($locale == 'tl')
$sRet = "fil-PH";
elseif($locale == 'ru')
$sRet = "ru-RU";
elseif($locale == 'ja')
$sRet = "ja-JP";
return $sRet;
}
function getLanguageListForSermonStorehouse()
{
$sSql = "select id, (case when nativename is not null and nativename <> '' then nativename else name end) as name from language where id in (select distinct language_id from lncsermon where language_id is not null and publish and type_id = " . $this->LNCSERMON_TYPE_ID_PUBLIC . ")";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getTopicListForSermonStorehouse()
{
$sSql = "select id, description as name from lnctopic where id in (select distinct topic_id from lncsermon_topic where lncsermon_id in (select id from lncsermon where type_id = " . $this->LNCSERMON_TYPE_ID_PUBLIC . "))";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getAuthorListForSermonStorehouse()
{
$sSql = "select id, name from vpersonforcombo where id in (select distinct person_id from lncsermon_person where lncsermon_id in (select id from lncsermon where type_id = " . $this->LNCSERMON_TYPE_ID_PUBLIC . "))";
$sSql .= " order by name";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function isBibleFullChapter($startverse_id, $endverse_id)
{
$bRet = false;
$sSql1 = "select cv1.ordering as order1, cv2.ordering as order2, cv1.chapter_id FROM canonicalbibleverse cv1, canonicalbibleverse cv2 where cv1.id = " . $startverse_id . " and cv2.id = " . $endverse_id;
// echo $sSql1."<br>";
$aData = $this->getRowData($sSql1);
if(!empty($aData))
{
$startverse_order = trim($aData['order1']);
$endverse_order = trim($aData['order2']);
$chapter_id = trim($aData['chapter_id']);
$sSql2 = "select min(cv.ordering) as min_order, max(cv.ordering) as max_order FROM canonicalbibleverse cv where chapter_id = " . $chapter_id;
// echo $sSql2."<br>";
$aData2 = $this->getRowData($sSql2);
if(!empty($aData2))
{
$min_order = trim($aData2['min_order']);
$max_order = trim($aData2['max_order']);
$bRet = ($startverse_order == $min_order && $endverse_order == $max_order);
}
}
return $bRet;
}
function getMynoteList($nStartNum, $locale, $request)
{
$aRet = array();
$nUserID = $this->getCurrUserID($request);
if(empty($nUserID))
{
return $this->getEmptyResult();
}
// $nMaxResult = 400;
$searchOptions = $request->query->all();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sBoundary = $this->getBoundaryStrForSql($nLanguageID);
$searchFor = "";
if(array_key_exists('searchTerm', $searchOptions))
{
$searchFor = $searchOptions["searchTerm"];
$searchFor = $this->fixQuotes($searchFor);
}
$sSql = "SELECT id, text, url, link_text, linktype_id";
$sSql .= " FROM user_note";
$sSql .= " where user_id = " . $nUserID;
$sFilter = "";
if(!empty($searchFor))
{
$sFilter .= " and (text ~* '" . $sBoundary . str_replace("'", "''", $searchFor) . $sBoundary . "' or link_text ~* '" . $sBoundary . str_replace("'", "''", $searchFor) . $sBoundary . "')";
}
$sSql .= $sFilter;
$sSql .= " order by id desc";
// $sSql .= " LIMIT ". $nMaxResult;
if($nStartNum > 1)
{
$sSql .= " OFFSET " . $nStartNum;
}
// echo $sSql . "<br>";
$results = $this->getNativeQueryResults($sSql);
$nResultCount = count($results);
if($nResultCount > 0)
{
$nMaxTextLen = 250;
for($i=0;$i<$nResultCount;$i++)
{
$nID = $results[$i]['id'];
$sText = $results[$i]['text'];
if(strlen($sText) > $nMaxTextLen)
{
$sTempText = substr($sText, 0, $nMaxTextLen);
$nPos = $nMaxTextLen;
if(!empty($sBoundary))
{
$nPos = strrpos($sTempText, " ");
}
$sNewText = substr($sText, 0, $nPos) . "<a id='link" . $nID . "' href='javascript:opentag(" . $nID . ")'>...</a><span id='tag" . $nID . "' style='display:none;'>" . substr($sText, $nPos) . "</span>";
$results[$i]['text'] = $sNewText;
}
}
}
$aRet['results'] = $results;
$nTotalNum = count($results);
// $bHasMore = (count($results) >= $nMaxResult);
$bHasMore = false;
$bHasFilter = ($sFilter != '');
$aRet['total_record'] = $nTotalNum;
$aRet['has_more'] = $bHasMore;
$aRet['start_num'] = $nStartNum;
$aRet['has_filter'] = $bHasFilter;
return $aRet;
}
function getMyBookmarkList($nStartNum, $locale, $request)
{
$aRet = array();
$nUserID = $this->getCurrUserID($request);
if(empty($nUserID))
{
return $this->getEmptyResult();
}
$sBible = $request->getSession()->get('ui_str')["linktype.header.bible"];
$sTheology = $request->getSession()->get('ui_str')["linktype.header.theology"];
$sOther = $request->getSession()->get('ui_str')["linktype.header.other"];
// $nMaxResult = 400;
$searchOptions = $request->query->all();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sBoundary = $this->getBoundaryStrForSql($nLanguageID);
$searchFor = "";
if(array_key_exists('searchTerm', $searchOptions))
{
$searchFor = $searchOptions["searchTerm"];
$searchFor = $this->fixQuotes($searchFor);
}
$sSql = "SELECT id, description, url, title as link_text, linktype_id, description, case when linktype_id = 1 then " . $this->dbstr($sBible) . " else case when linktype_id = 2 then " . $this->dbstr($sTheology) . " else " . $this->dbstr($sOther) . " end end as linktype";
$sSql .= " FROM user_bookmark";
$sSql .= " where user_id = " . $nUserID;
$sFilter = "";
if(!empty($searchFor))
{
$sFilter .= " and (description ~* '" . $sBoundary . str_replace("'", "''", $searchFor) . $sBoundary . "' or title ~* '" . $sBoundary . str_replace("'", "''", $searchFor) . $sBoundary . "')";
}
$nTypeID = "";
if(array_key_exists('type', $searchOptions))
$nTypeID = $searchOptions['type'];
if($nTypeID != '')
{
$sFilter .= " and linktype_id = " . $nTypeID;
}
$sSql .= $sFilter;
$sSql .= " order by id desc";
// $sSql .= " LIMIT ". $nMaxResult;
if($nStartNum > 1)
{
$sSql .= " OFFSET " . $nStartNum;
}
// echo $sSql . "<br>";
$results = $this->getNativeQueryResults($sSql);
$aRet['results'] = $results;
$nTotalNum = count($results);
// $bHasMore = (count($results) >= $nMaxResult);
$bHasMore = false;
$bHasFilter = ($sFilter != '');
$aRet['total_record'] = $nTotalNum;
$aRet['has_more'] = $bHasMore;
$aRet['start_num'] = $nStartNum;
$aRet['has_filter'] = $bHasFilter;
return $aRet;
}
function getEmptyResult()
{
$aRet = array();
$aRet['results'] = array();
$bHasFilter = false;
$aRet['total_record'] = 0;
$aRet['has_more'] = false;
$aRet['start_num'] = 0;
$aRet['has_filter'] = false;
return $aRet;
}
function fixQuotes($str)
{
$sRet = trim($str, "'");
$sRet = str_replace('"', "", $sRet);
return $sRet;
}
function dbstr($str)
{
$sRet = "'".str_replace("'", "''", $str)."'";
return $sRet;
}
function getLinkTypeList($request)
{
$sBible = $request->getSession()->get('ui_str')["linktype.header.bible"];
$sTheology = $request->getSession()->get('ui_str')["linktype.header.theology"];
$sOther = $request->getSession()->get('ui_str')["linktype.header.other"];
$sSql = "select id, case when id = 1 then " . $this->dbstr($sBible) . " else case when id = 2 then " . $this->dbstr($sTheology) . " else " . $this->dbstr($sOther) . " end end as name from linktype";
$sSql .= " order by id";
// echo $sSql . "<br>";
return $this->getNativeQueryResults($sSql);
}
function getFeaturedContentList($locale, $vLimit, $vUseDefaultLang=false)
{
if($vUseDefaultLang)
$nLanguageID = $this->LANGUAGE_ID_ENGLISH;
else
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "select * from (SELECT distinct on (t.id) t.translatedtitle AS title, t.description, t.url, bsw.story_id, bsw.story_url, t.url as translation_url, wo.feature_ordering, ";
$sSql .= "case when fi.image_file is not null then fi.image_file else case when fi2.image_file is not null then fi2.image_file else case when ci.image_file is not null then ci.image_file else ti.image_file end end end as image_file, case when fi.image_file is not null then fi.image_title else case when fi2.image_file is not null then fi2.image_title else case when ci.image_file is not null then ci.image_title else ti.image_title end end end as image_title";
$sSql .= " FROM expositionwork w, expositiontranslation t";
// featured image from the current translation
$sSql .= " left join (select distinct on (t1.id) t1.id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id) fi on t.id = fi.id";
// featured image from the translation with image
$sSql .= " left join (select distinct on (t1.work_id) t1.work_id, i.file as image_file, i.title as image_title from expositiontranslation t1, image i where t1.is_featured and t1.feature_image_id = i.id order by t1.work_id, case when t1.language_id = " . $this->LANGUAGE_ID_ENGLISH . " then 1 else 2 end) fi2 on t.work_id = fi2.work_id";
// textunit image
$sSql .= " left join (select distinct on (tt1.translation_id) tt1.translation_id, i.file as image_file, i.title as image_title from textunit_image ti, image i, expositiontranslationtext tt1 where ti.textunit_id = tt1.placement_id and ti.image_id = i.id and i.mediatype_id = 1) ti on t.id = ti.translation_id";
// For concepts
$sSql .= " left join (select distinct on (cl.work_id) cl.work_id as id, i.file as image_file, i.title as image_title from image i, conceptillustration ci1, concept_work cl where cl.concept_id = ci1.concept_id and ci1.image_id = i.id) ci on t.work_id = ci.id";
$sSql .= " join (select distinct on (t2.work_id) t2.work_id, t2.feature_ordering from expositiontranslation t2 where t2.is_featured and t2.language_id = " . $this->LANGUAGE_ID_ENGLISH . ") as wo on t.work_id = wo.work_id"
." left join (select bs.id, bs.url as story_url, sw.work_id, sw.story_id from story_work sw, (select * from biblestory where level_id = " . $this->STORY_LEVEL_ID_BIBLE . ") as bs where bs.id = sw.story_id) bsw on t.work_id = bsw.work_id"
." WHERE t.language_id = " . $nLanguageID . " AND t.ispublic AND w.ispublic and t.work_id=w.id";
$sSql .= " and w.id not IN (SELECT work_id FROM work_category WHERE category_id=" . $this->CATEGORY_ID_DOCUMENTS . ")";
$sSql .= " and t.id not IN (SELECT translation_id FROM translation_category WHERE category_id=" . $this->CATEGORY_ID_DOCUMENTS . ")";
$sSql .= ") as t1"
." ORDER BY t1.feature_ordering";
if($vLimit != null && $vLimit > 0)
$sSql .= " limit " . $vLimit;
// echo $sSql."<br>";
return $this->getNativeQueryResults($sSql);
}
// Check if the the translation has contents for a passage
function getNewWorkTranslationUrl($translation_id, $passage_id)
{
$sRet = null;
$sSql = "select tt.id from expositiontranslationtext tt, expositiontextunit tu where tu.passage_id = " . $passage_id . " and tt.translation_id = " . $translation_id . " and tu.id = tt.placement_id limit 1";
$nID = $this->getSingleData($sSql);
if(empty($nID))
{
$sSql2 = "select url from (select distinct t.id, t.url from expositiondisplayitem di, expositiontranslationtext tt, expositiontextunit tu, expositiontranslation t where tu.passage_id = " . $passage_id . " and t.language_id in (select language_id from expositiontranslation where id = " . $translation_id . ") and tu.id = tt.placement_id and di.container_id = tt.id and t.id = tt.translation_id) as t order by t.id desc limit 1";
// echo $sSql2."<br>";
$sUrl = $this->getSingleData($sSql2);
if(!empty($sUrl))
{
$sSql2 = "select swedenborgsection from expositionpassage where id = " . $passage_id;
// echo $sSql2."<br>";
$nPassageOrder = $this->getSingleData($sSql2);
if(!empty($nPassageOrder))
{
$sRet = "swedenborg_" . $sUrl . "_" . $nPassageOrder;
}
}
}
return $sRet;
}
function getLastAvailablePassageNum($translation_id)
{
$sSql = "select p.swedenborgsection from expositiontranslationtext tt, expositiontextunit tu, expositionpassage p where tt.translation_id = " . $translation_id . " and tu.passage_id = p.id and tu.id = tt.placement_id order by p.ordering desc limit 1";
// echo $sSql."<br>";
$nRet = $this->getSingleData($sSql);
return $nRet;
}
function getExplanationUrl($translation_id)
{
$sSql = "SELECT '" . $this->MULTI_URL_INDICATOR_EXPLANATION . "_' || url as ref_column_spec"
." FROM expositiontranslation"
." WHERE id = " . $translation_id;
// echo $sSql."<br>";
return $this->getSingleData($sSql);
}
function getReadingPlanStepsForOutline($readingPlanUrl, $UserID, $request)
{
$locale = $request->getLocale();
$nLanguageID = $this->getLanguageIDByShortCode($locale);
$sSql = "";
if(!empty($UserID))
{
$sSql = "SELECT rs.id, rs.ordering, case when rsl.name is null then rs.name else rsl.name end as name, case when us.id is null then false else true end as is_completed, r.url FROM readingplan r, readingplanstep rs left join (select urs.* from user_readingplanstep urs, user_readingplan ur where ur.user_id = " . $UserID . " and ur.id = urs.user_readingplan_id) us on rs.id = us.readingplanstep_id left join (select * from readingplanstep_language where language_id = " .$nLanguageID . ") rsl on rs.id = rsl.readingplanstep_id"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." order by rs.ordering";
}
else
{
$sSql = "SELECT rs.id, rs.ordering, case when rsl.name is null then rs.name else rsl.name end as name, false as is_completed, r.url FROM readingplan r, readingplanstep rs left join (select * from readingplanstep_language where language_id = " .$nLanguageID . ") rsl on rs.id = rsl.readingplanstep_id"
." where r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = rs.readingplan_id"
." order by rs.ordering";
}
// echo $sSql . "<br>";
$aRet = $this->getNativeQueryResults($sSql);
return $aRet;
}
function IsLastReadingPlanStep($vStepID, $request)
{
$bRet = false;
$nUserID = $this->getCurrUserID($request);
if(empty($nUserID))
{
return $bRet;
}
$sSql = "select readingplan_id, ordering from readingplanstep where id = " . $vStepID;
// echo $sSql."<br>";
$aStep = $this->getRowData($sSql);
$nReadingPlanID = $aStep['readingplan_id'];
$nStepOrder = $aStep['ordering'];
$sSql = "SELECT r.url as plan_url, rs.ordering as step_order"
." FROM readingplan r, readingplanstep rs"
." where r.id = " . $nReadingPlanID
." and r.id = rs.readingplan_id"
." and rs.ordering > " . $nStepOrder
." order by rs.ordering limit 1";
// echo $sSql."<br>";
$aTemp = $this->getNativeQueryResults($sSql);
if(empty($aTemp))
$bRet = true;
return $bRet;
}
function hasReadingPlanStarted($readingPlanUrl, $UserID)
{
$bRet = false;
if(!empty($UserID))
{
$sSql = "SELECT r.id FROM readingplan r, user_readingplan ur"
." where ur.user_id = " . $UserID
." and r.url = '" . trim(str_replace("'", "''", $readingPlanUrl)) . "'"
." and r.id = ur.readingplan_id";
// echo $sSql . "<br>";
$aRet = array();
$aTemp = $this->getNativeQueryResults($sSql);
if(count($aTemp) > 0)
$bRet = true;
}
return $bRet;
}
function getRandomNum($length = 6) {
$sSeed = '1234567890';
$sRet = '';
$nSeedLen = strlen($sSeed);
for ($i = 0; $i < $length; $i++) {
$sRet .= $sSeed[rand(0, $nSeedLen - 1)];
}
return $sRet;
}
//--------
// For numbers less than 19 digits
function simpleEncrypt($num) {
return (intval($num)+$this->SC_SIMPLE_ENCRYPT_SEED)*$this->SC_SIMPLE_ENCRYPT_SEED;
}
function simpleDecrypt($num) {
return (intval($num)/$this->SC_SIMPLE_ENCRYPT_SEED)-$this->SC_SIMPLE_ENCRYPT_SEED;
}
//--------
/* function getStartedReadingPlanIDs($vUserID)
{
$sSql = "select array_to_string(array_agg(readingplan_id),',') as IDs from user_readingplan where user_id = " . $vUserID;
// echo $sSql."<br>";
$sRet = $this->getSingleData($sSql);
return $sRet;
}*/
function getReminderOptionList($request)
{
$sOff = $request->getSession()->get('ui_str')["readingplan.reminderoption.off"];
$sDaily = $request->getSession()->get('ui_str')["readingplan.reminderoption.daily"];
$sWeekly = $request->getSession()->get('ui_str')["readingplan.reminderoption.weekly"];
$sSql = "select id, case when id = 1 then " . $this->dbstr($sOff) . " else case when id = 2 then " . $this->dbstr($sDaily) . " else " . $this->dbstr($sWeekly) . " end end as name from readingplanreminderoption order by id";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return $aRet;
}
function getChatHistoryList($vUserID, $vLimit=null)
{
$aRet = array();
$sSql = "SELECT id, description, session, TO_CHAR(last_modify_time, 'mm/dd/yyyy') as datetime FROM chatbotthread";
$sSql .= " WHERE id in (select distinct thread_id from chatbotmessage where user_id = " . $vUserID . ")";
$sSql .= " order by last_modify_time desc";
if(!empty($vLimit))
{
$sSql .= " limit " . $vLimit;
}
// echo $sSql."<br>";
$aChat = $this->getListData($sSql);
$aRet['chat_list'] = $aChat;
/* $nChatCount = count($aChat);
$bShowMore = false;
if((!empty($vLimit) && $nChatCount >= $vLimit) || $nChatCount > 0)
{
$sSql = "SELECT count(id) FROM chatbotthread";
$sSql .= " WHERE id in (select distinct thread_id from chatbotmessage where user_id = " . $vUserID . ")";
$nTotal = $this->getSingleData($sSql);
if(!empty($vLimit) && $nChatCount > $vLimit)
{
$bShowMore = true;
}
}
$aRet['show_more'] = $bShowMore; */
return $aRet;
}
function hasSessionHistory($vSessionID)
{
$sSql = "SELECT id FROM chatbotthread";
$sSql .= " WHERE session = " . $vSessionID . " limit 1";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return !empty($aRet);
}
function hasChatHistory($vUserID)
{
$sSql = "SELECT t.id FROM chatbotthread t, chatbotmessage m where m.user_id = " . $vUserID . " and t.id = m.thread_id limit 1";
// echo $sSql."<br>";
$aRet = $this->getListData($sSql);
return !empty($aRet);
}
} // end of php class