Open Llewellynvdm opened 5 years ago
Looks tremendous, great functionality!
Here is the updated script as currently being used:
/**
* API Model for Sermonindex
*/
class APIModelSermonindex extends JModelList
{
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__si_mydownloads_cat as a
$query->select($db->quoteName(
array('a.cid','a.title','a.imgurl','a.description'),
array('id','name','image','description')));
$query->from($db->quoteName('#__si_mydownloads_cat', 'a'));
// return the query object
return $query;
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*/
public function getItems()
{
$user = JFactory::getUser();
// load parent items
$items = parent::getItems();
// Get the global params
$globalParams = JComponentHelper::getParams('com_hello_world', true);
// Insure all item fields are adapted where needed.
if (APIHelper::checkArray($items))
{
foreach ($items as $nr => &$item)
{
// Always create a slug for sef URL's
$item->slug = (isset($item->alias) && isset($item->id)) ? $item->id.':'.$item->alias : $item->id;
// set mydownloads to the $item object.
$item->mydownloads = $this->getMydownloads($item->id);
}
}
if (APIHelper::checkArray($items))
{
jimport('joomla.filesystem.folder');
// repo path
$repo_path = '/home/llewellyn/audio_api/';
// remove all the old files
APIHelper::removeFolder($repo_path, array('README.md', 'LICENSE.txt', '.git', 'images', 'api-image.jpg', 'app-image.jpg'));
// create the core folders again
JFolder::create($repo_path . 'scripture');
JFolder::create($repo_path . 'speaker');
JFolder::create($repo_path . 'speaker_sermons');
JFolder::create($repo_path . 'topic');
JFolder::create($repo_path . 'topic_sermons');
// Get a db connection.
$db = JFactory::getDbo();
// function to get all scriptural keys
$getAllScripture = function ($id) use($db) {
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__si_mydownloads_scripture_to_sermon as a
$query->select('a.scriptureId');
$query->from($db->quoteName('#__si_mydownloads_scripture_to_sermon', 'a'));
// Get where bid is equal to
$query->where('a.sermonId = ' . (int) $id);
$db->setQuery($query);
$db->execute();
// check if there was data returned
if ($db->getNumRows())
{
return $db->loadColumn();
}
return false;
};
// function to get scriptural keys
$getScripture = function ($id) use($db) {
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__si_mydownloads_scripture as a
$query->select($db->quoteName(
array('a.book','a.scripture'),
array('book','chapter')));
$query->from($db->quoteName('#__si_mydownloads_scripture', 'a'));
// Get where bid is equal to
$query->where('a.id = ' . (int) $id);
$db->setQuery($query);
$db->execute();
// check if there was data returned
if ($db->getNumRows())
{
return $db->loadObject();
}
return false;
};
// speaker list
$per_speaker = array();
// all speakers
$array_speakers = array();
// topic buckets
$per_topic = array();
$index_topic = array();
$array_topic = array();
$topic_name = array();
$topic_sermons = array();
// scripture buckets
$per_book = array();
$per_beok = array();
$per_chapter = array();
foreach ($items as $nr => &$item)
{
// prep name
$item->name = trim(preg_replace("/[^A-Za-z0-9\. ]/", '', $item->name));
// set the speaker safe name
$name = APIHelper::safeString($item->name);
// do we have an image path
if (APIHelper::checkString($item->image) && strpos($item->image, 'http') !== false)
{
// get image extension
$ext = pathinfo($item->image, PATHINFO_EXTENSION);
// set the preacher image name
$image_name = $name . '.' . $ext;
// check if the image exist
if (!file_exists($repo_path . 'images/' . $image_name))
{
// get the preacher image
if (($image = APIHelper::getFileContents($item->image, false)) !== false)
{
// store the speaker image to folder
APIHelper::writeFile($repo_path . 'images/' . $image_name, $image);
// update with full path
$item->image = '/images/' . $image_name;
}
else
{
$item->image = '';
}
}
else
{
// update with full path
$item->image = '/images/' . $image_name;
}
}
else
{
$item->image = '';
}
$item->description = trim(strip_tags($item->description));
$item->sermons = $item->mydownloads;
unset($item->id);
unset($item->slug);
unset($item->mydownloads);
// start sermon object
if (APIHelper::checkArray($item->sermons))
{
usort($item->sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// sermon bucket
$speaker_sermons = array();
foreach ($item->sermons as $i => $sermon)
{
if (($text = APIHelper::getVar('mydownloads_text', $sermon->id, 'id', 'description', '=', 'si')) !== false && $text !== 'nil')
{
$sermon->description = trim($text);
}
else
{
$sermon->description = '';
}
// set the sermon ID
$sermonID = $sermon->id;
// remove some stuff
unset($sermon->id);
unset($sermon->archive_url);
// set the short URL
$sermon->url = 'http://api.sermonindex.net/' . $sermon->short_url;
$sermon->download = 'http://api.sermonindex.net/' . $sermon->short_url . '-download';
unset($sermon->short_url);
// prep sermon in temp bucket
$tmp = $sermon;
$tmp->speaker_name = $item->name;
// removed as this adds to much bulky values
// $tmp->preacher_image = $item->image;
// $tmp->preacher_description = $item->description;
// fix topic string
$tmp->topic = trim(preg_replace("/[^A-Za-z0-9\. ]/", '', $tmp->topic));
// this sermons scripture bucket
$scripture = array();
$tmp_per_scripture = array();
// now sort by scripture
if (($scriptureIDs = $getAllScripture($sermonID)) !== false)
{
foreach ($scriptureIDs as $scriptureID)
{
if (($keys = $getScripture($scriptureID)) !== false)
{
if (isset($keys->book) && APIHelper::checkString($keys->book))
{
if (!isset($tmp_per_scripture[$keys->book]))
{
$tmp_per_scripture[$keys->book] = array();
$scripture[$keys->book] = array();
}
// load the chapter
if (isset($keys->chapter) && APIHelper::checkString($keys->chapter))
{
$cnr = trim(str_replace($keys->book, '', $keys->chapter));
// check if verse is added
if (strpos($cnr, ':') !== false)
{
$cnr = explode(':', $cnr)[0];
}
// check if verse is added
if (strpos($cnr, '-') !== false)
{
$cnr = explode('-', $cnr);
}
// continue only if we have a number
if (is_numeric($cnr) && $cnr > 0)
{
$cnr = array($cnr);
}
// load chapters
if (APIHelper::checkArray($cnr))
{
foreach ($cnr as $chnr)
{
if (!isset($tmp_per_scripture[$keys->book][$chnr]))
{
$tmp_per_scripture[$keys->book][$chnr] = array();
if (!in_array($chnr, $scripture[$keys->book]))
{
$scripture[$keys->book][] = $chnr;
}
}
// per scripture
$tmp_per_scripture[$keys->book][$chnr] = true;
}
}
}
}
}
}
}
// build scripture object
if (APIHelper::checkArray($scripture))
{
$sermon->_scripture = array();
foreach ($scripture as $b => $cs)
{
foreach ($cs as $_c)
{
if (!empty($_c))
{
$sermon->_scripture[] = array('book' => $b, 'chapter' => $_c);
}
}
}
$tmp->_scripture = $sermon->_scripture;
}
else
{
// now set the scripture as an object
$sermon->_scripture = array();
$tmp->_scripture = array();
}
// no load the global per scripture
if (APIHelper::checkArray($tmp_per_scripture))
{
foreach ($tmp_per_scripture as $book_ => $chapters_)
{
if (APIHelper::checkString($book_))
{
if (!isset($per_book[$book_]))
{
$per_book[$book_] = array();
}
// set per book
$per_book[$book_][] = $tmp;
// set per chapter
if (APIHelper::checkArray($chapters_))
{
if (!isset($per_chapter[$book_]))
{
$per_chapter[$book_] = array();
}
foreach($chapters_ as $chapter_ => $hmm)
{
if (!isset($per_chapter[$book_][$chapter_]))
{
$per_chapter[$book_][$chapter_] = array();
}
// per scripture
$per_chapter[$book_][$chapter_][] = $tmp;
}
}
}
}
}
// build the topic array
$key = APIHelper::safeString($tmp->topic);
if (!isset($per_topic[$key]))
{
$per_topic[$key] = array();
$topic_name[$key] = $tmp->topic;
}
$per_topic[$key][] = $tmp;
// sermon details
$speaker_sermons[] = $tmp;
}
// store the preacher
APIHelper::writeFile($repo_path . 'speaker/' . $name . '.json', json_encode($item, JSON_PRETTY_PRINT));
APIHelper::writeFile($repo_path . 'speaker_sermons/' . $name . '.json', json_encode(array_values($speaker_sermons), JSON_PRETTY_PRINT));
// speaker details
$object_speaker = new stdClass();
$object_speaker->name = $item->name;
$object_speaker->code = $name;
$object_speaker->description = $item->description;
$object_speaker->file = $name . '.json';
$object_speaker->image = $item->image;
// set the speakers
$array_speakers[$name] = $object_speaker;
// set the speaker links
$per_speaker[$name] = '/speaker/' .$name . '.json';
}
// unset the memory
unset($items[$nr]);
}
// sort the speakers
ksort($per_speaker);
ksort($array_speakers);
// store the speaker index's
APIHelper::writeFile($repo_path . 'speakers.json', json_encode(array_values($array_speakers), JSON_PRETTY_PRINT));
APIHelper::writeFile($repo_path . 'speaker/_sermonindex.json', json_encode($per_speaker, JSON_PRETTY_PRINT));
// now set the topics
foreach ($per_topic as $key => $_sermons)
{
// sort sermons
usort($_sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set name
$_topic_name = trim($topic_name[$key]);
if (!APIHelper::checkString($_topic_name))
{
$_topic_name = 'unsorted';
$key = $_topic_name;
}
// set the topic
$topic = new stdClass();
$topic->name = $_topic_name;
$topic->description = '';
$topic->sermons = $_sermons;
// store the topic
APIHelper::writeFile($repo_path . 'topic/' . $key . '.json', json_encode($topic, JSON_PRETTY_PRINT));
APIHelper::writeFile($repo_path . 'topic_sermons/' . $key . '.json', json_encode($_sermons, JSON_PRETTY_PRINT));
// unset the memory
unset($topic_name[$key]);
unset($topic_sermons[$key]);
// set the topic index
$index_topic[$key] = '/topic/' . $key . '.json';
// set topic array
unset($topic->sermons);
$topic->code = $key;
$topic->file = $key . '.json';
$array_topic[$key] = $topic;
}
// sort the topics
ksort($index_topic);
ksort($array_topic);
// build the topic index
APIHelper::writeFile($repo_path . 'topics.json', json_encode(array_values($array_topic), JSON_PRETTY_PRINT));
APIHelper::writeFile($repo_path . 'topic/_sermonindex.json', json_encode($index_topic, JSON_PRETTY_PRINT));
// now set the topics
foreach ($per_book as $boek => $_chapters)
{
// sort sermons
usort($_chapters, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set the book
$book = new stdClass();
$book->name = $boek;
$book->sermons = $_chapters;
// get book key
$key = APIHelper::safeString($boek, 'L', '_', false, false);
// store the topic
APIHelper::writeFile($repo_path . 'scripture/' . $key . '.json', json_encode($book, JSON_PRETTY_PRINT));
// unset the memory
unset($book);
unset($per_book[$boek]);
// set the book index
$per_beok[$key] = '/scripture/' . $key . '.json';
// create the folder if it does not exist
if (!JFolder::exists($repo_path . 'scripture/' . $key))
{
JFolder::create($repo_path . 'scripture/' . $key);
}
// now set per chapter
if (isset($per_chapter[$boek]))
{
foreach ($per_chapter[$boek] as $number => $__sermons)
{
// sort sermons
usort($__sermons, function ($a, $b) {
return strcmp($a->title, $b->title);
});
// set the book
$chapter = new stdClass();
$chapter->name = $boek . ' ' . $number;
$chapter->book = $boek;
$chapter->chapter = $number;
$chapter->sermons = $__sermons;
// store the topic
APIHelper::writeFile($repo_path . 'scripture/' . $key . '/' . $number . '.json', json_encode($chapter, JSON_PRETTY_PRINT));
// unset the memory
unset($chapter);
unset($per_chapter[$boek]);
// set the topic index
$per_chapter[$key][$number] = '/scripture/' . $key . '/' . $number . '.json';
}
// sort the chapters
ksort($per_chapter[$key]);
// build the scripture index
APIHelper::writeFile($repo_path . 'scripture/' . $key . '/_sermonindex.json', json_encode($per_chapter[$key], JSON_PRETTY_PRINT));
// unset the memory
unset($per_chapter[$key]);
}
}
if (APIHelper::checkArray($per_beok))
{
// sort the books
ksort($per_beok);
// build the scripture index
APIHelper::writeFile($repo_path . 'scripture/_sermonindex.json', json_encode($per_beok, JSON_PRETTY_PRINT));
}
}
jexit('Done!'); // we end here
}
/**
* Method to get an array of Si_mydownloads_downloads Objects.
*
* @return mixed An array of Si_mydownloads_downloads Objects on success, false on failure.
*
*/
public function getMydownloads($cid)
{
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__si_mydownloads_downloads as b
$query->select($db->quoteName(
array('b.lid','b.title','b.url','b.archive_url','b.short_url','b.size','b.platform','b.scripture','b.topic'),
array('id','title','url','archive_url','short_url','size','format','scripture','topic')));
$query->from($db->quoteName('#__si_mydownloads_downloads', 'b'));
$query->where('b.cid = ' . $db->quote($cid));
// Get where b.status is 1
$query->where('b.status >= 1');
// Reset the query using our newly populated query object.
$db->setQuery($query);
$db->execute();
// check if there was data returned
if ($db->getNumRows())
{
return $db->loadObjectList();
}
return false;
}
}
Here is the script I use to convert the Sermon Index database into this repo(API) there are some helper static methods used, and the code for them can be found in the JCB helper class. There are also some Joomla API static helper classes.... but yea any contribution to improve this is welcome. I honestly just took the easies path to crank out the API :) I am very sure there are smarter ways.
We start with checking if we have all the sermons in memory... and then:
So every time we change things in the DB, I just run this script, and minutes later the whole API is updated.