Last active
March 8, 2016 09:30
-
-
Save Llewellynvdm/e1e03371b07524fce72d to your computer and use it in GitHub Desktop.
Moving data from JMS Music to Sermon Distributor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Moving extends JModelList | |
{ | |
public function getData() | |
{ | |
// start up the DB | |
$db = JFactory::getDBO(); | |
// move the preachers | |
$this->movePreachers($db); | |
// move Series | |
$this->moveSeries($db); | |
// move the sermons | |
$this->moveSermons($db); | |
} | |
protected function moveSermons($db) | |
{ | |
$query = $db->getQuery(true); | |
// Select all fields | |
$query->select('a.*'); | |
$query->from($db->quoteName('jos_jmsmusic_songs', 'a')); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$items = $db->loadObjectList(); | |
// now repack it | |
foreach ($items as $nr => &$item) | |
{ | |
$bucket = new stdClass; | |
$bucket->id = (int) $item->id; | |
$bucket->name = $item->song_name; | |
$bucket->alias = SermondistributorHelper::safeString($item->song_name,'L','-'); | |
$bucket->preacher = $this->getPreacher($item->id,$db); | |
$bucket->source = 1; | |
$bucket->link_type = 1; | |
$bucket->build = 1; | |
$bucket->auto_sermons = 1; | |
if ($item->media_file) | |
{ | |
$bucket->local_files = '["'.$item->media_file.'"]'; | |
$bucket->published = (int)$item->published; | |
} | |
else | |
{ | |
$bucket->published = 0; | |
} | |
$bucket->manual_files = '"0"'; | |
$bucket->series = (int)$item->playlist_id; | |
$bucket->catid = (int) $this->getCatogory($item->category_id,$db); | |
$bucket->short_description = SermondistributorHelper::sorten(trim(strip_tags($item->desc)), 20, false); | |
$bucket->description = $item->desc; | |
// defaults | |
$bucket->created_by = 50; | |
$bucket->created = $item->created; | |
$bucket->hits = $item->hits; | |
$bucket->access = 1; | |
$bucket->ordering = (int)$item->id; | |
$bucket->version = 1; | |
$bucket->metakey = SermondistributorHelper::safeString($item->song_name,'L',','); | |
$bucket->metadesc = $item->song_name; | |
$bucket->metadata = '{"robots":"","author":"","rights":""}'; | |
// Insert the object into the user profile table. | |
$done = JFactory::getDbo()->insertObject('#__sermondistributor_sermon', $bucket); | |
if ($done) | |
{ | |
$aId = $db->insertid(); | |
// make sure the access of asset is set | |
SermondistributorHelper::setAsset($aId,'sermon'); | |
// only add if there is a file | |
if ($item->media_file) | |
{ | |
// now update the statistics | |
$stat = new stdClass; | |
// not all hits are downloads in jmsmusic | |
$stat->counter = $this->getHonestValue($item->hits); | |
$stat->filename = 'jmsmusic_'.$item->media_file; | |
$stat->preacher = $bucket->preacher; | |
$stat->series = $bucket->series; | |
$stat->sermon = $aId; | |
// defaults | |
$stat->created_by = 50; | |
$stat->created = $item->created; | |
$stat->hits = 0; | |
$stat->access = 1; | |
$stat->ordering = 0; | |
$stat->version = 1; | |
// Insert the object into the user profile table. | |
$done = JFactory::getDbo()->insertObject('#__sermondistributor_statistic', $stat); | |
if ($done) | |
{ | |
$bId = $db->insertid(); | |
// make sure the access of asset is set | |
SermondistributorHelper::setAsset($bId,'statistic'); | |
} | |
} | |
} | |
} | |
} | |
} | |
protected $category = array(); | |
protected function getCatogory($id,$db) | |
{ | |
if (!isset($this->category[$id])) | |
{ | |
// Create a new query object. | |
$query = $db->getQuery(true); | |
$query->select($db->quoteName(array('category_name'))); | |
$query->from($db->quoteName('jos_jmsmusic_categories')); | |
$query->where($db->quoteName('id') . ' = '. (int) $id); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$this->category[$id] = $this->getCatogoryId($db->loadResult(),$db); | |
// if still not set, then create | |
if (0 == $this->category[$id]) | |
{ | |
$this->category[$id] = $this->createCategory($id,$db); | |
} | |
} | |
else | |
{ | |
$this->category[$id] = 0; | |
} | |
} | |
return $this->category[$id]; | |
} | |
protected function createCategory($id,$db) | |
{ | |
$query = $db->getQuery(true); | |
// Select all fields | |
$query->select($db->quoteName(array('a.category_name','a.desc','a.published'),array('title','description','published'))); | |
$query->from($db->quoteName('jos_jmsmusic_categories', 'a')); | |
$query->where($db->quoteName('a.id') . ' = '. (int) $id); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
// get the category | |
$item = $db->loadObject(); | |
// load the category table | |
JTable::addIncludePath(JPATH_LIBRARIES . '/joomla/database/table'); | |
$category = JTable::getInstance('Category'); | |
$category->extension = 'com_sermondistributor.sermons'; | |
$category->title = $item->title; | |
$category->alias = $this->getAlias($item->title); | |
$category->description = $item->description; | |
$category->published = $item->published; | |
$category->access = 1; | |
$category->params = '{"category_layout":"","image":"","image_alt":""}'; | |
$category->metadata = '{"author":"","robots":""}'; | |
$category->language = '*'; | |
$category->setLocation(1, 'last-child'); | |
$category->store(true); | |
$category->rebuildPath($category->id); | |
return $category->id; | |
} | |
return 0; | |
} | |
protected function getCatogoryId($name,$db) | |
{ | |
// sanitize the name to an alias | |
$alias = $this->getAlias($name); | |
// Create a new query object. | |
$query = $db->getQuery(true); | |
$query->select($db->quoteName(array('id'))); | |
$query->from($db->quoteName('#__categories')); | |
$query->where($db->quoteName('alias') . ' = '. $db->quote($alias)); | |
$query->where($db->quoteName('extension') . ' = '. $db->quote('com_sermondistributor.sermons')); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
return $db->loadResult(); | |
} | |
return 0; | |
} | |
protected function getAlias($name,$type = false) | |
{ | |
// sanitize the name to an alias | |
if (JFactory::getConfig()->get('unicodeslugs') == 1) | |
{ | |
$alias = JFilterOutput::stringURLUnicodeSlug($name); | |
} | |
else | |
{ | |
$alias = JFilterOutput::stringURLSafe($name); | |
} | |
if ($type) | |
{ | |
return $this->getUniqe($alias,'alias',$type); | |
} | |
return $alias; | |
} | |
/** | |
* Method to generate a uniqe value. | |
* | |
* @param string $field name. | |
* @param string $value data. | |
* @param string $type table. | |
* | |
* @return string New value. | |
*/ | |
protected function getUniqe($value,$field,$type) | |
{ | |
// insure the filed is always uniqe | |
while (isset($this->uniqeValueArray[$type][$field][$value])) | |
{ | |
$value = JString::increment($value, 'dash'); | |
} | |
$this->uniqeValueArray[$type][$field][$value] = $value; | |
return $value; | |
} | |
protected function getHonestValue($hits) | |
{ | |
if(10000 < $hits) | |
{ | |
$low = (int) round($hits / 50); | |
$hig = $hits - rand(1000,9000); | |
} | |
elseif(5000 < $hits) | |
{ | |
$low = (int) round($hits / 20); | |
$hig = $hits - rand(1000,400); | |
} | |
elseif(1000 < $hits) | |
{ | |
$low = (int) round($hits / 10); | |
$hig = $hits - rand(100,900); | |
} | |
elseif (500 < $hits) | |
{ | |
$low = (int) round($hits / 5); | |
$hig = $hits - rand(100,400); | |
} | |
elseif (50 < $hits) | |
{ | |
$low = (int) round($hits / 2); | |
$hig = $hits - rand(10,40); | |
} | |
else | |
{ | |
$low = 3; | |
$hig = $hits; | |
} | |
return rand($low,$hig); | |
} | |
protected $preacher = array(); | |
protected function getPreacher($id,$db) | |
{ | |
if (!isset($this->preacher[$id])) | |
{ | |
$query = $db->getQuery(true); | |
// Select song_id | |
$query->select('a.artist_id'); | |
$query->from($db->quoteName('jos_jmsmusic_song_artists', 'a')); | |
$query->where($db->quoteName('song_id')." = ".(int) $id); | |
$query->order('a.song_id ASC'); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
// start new query | |
$this->preacher[$id] = $db->loadResult(); | |
} | |
else | |
{ | |
$this->preacher[$id] = null; | |
} | |
} | |
return $this->preacher[$id]; | |
} | |
protected function moveSeries($db) | |
{ | |
$query = $db->getQuery(true); | |
// Select all fields | |
$query->select('a.*'); | |
$query->from($db->quoteName('jos_jmsmusic_playlists', 'a')); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$items = $db->loadObjectList(); | |
// now repack it | |
foreach ($items as $nr => &$item) | |
{ | |
$bucket= new stdClass; | |
$bucket->id = (int)$item->id; | |
$bucket->name = $item->playlist_name; | |
$bucket->alias = SermondistributorHelper::safeString($item->playlist_name,'L','-'); | |
if ($item->playlist_image) | |
{ | |
$bucket->icon = 'images/'.$item->playlist_image; | |
} | |
$bucket->description = $item->desc; | |
$bucket->published = (int)$item->published; | |
// defaults | |
$bucket->created_by = 50; | |
$bucket->created = $item->created; | |
$bucket->hits = $item->hits; | |
$bucket->access = 1; | |
$bucket->ordering = (int)$item->id; | |
$bucket->version = 1; | |
$bucket->metakey = SermondistributorHelper::safeString($item->playlist_name,'L',','); | |
$bucket->metadesc = $item->playlist_name; | |
$bucket->metadata = '{"robots":"","author":"","rights":""}'; | |
// Insert the object into the user profile table. | |
$done = JFactory::getDbo()->insertObject('#__sermondistributor_series', $bucket); | |
if ($done) | |
{ | |
$aId = $db->insertid(); | |
// make sure the access of asset is set | |
SermondistributorHelper::setAsset($aId,'series'); | |
} | |
} | |
} | |
} | |
protected function movePreachers($db) | |
{ | |
$query = $db->getQuery(true); | |
// Select all fields | |
$query->select('a.*'); | |
$query->from($db->quoteName('jos_jmsmusic_artists', 'a')); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$items = $db->loadObjectList(); | |
// now repack it | |
foreach ($items as $nr => &$item) | |
{ | |
$bucket= new stdClass; | |
$bucket->id = (int) $item->id; | |
$bucket->name = $item->artist_name; | |
$bucket->alias = SermondistributorHelper::safeString($item->artist_name,'L','-'); | |
if ($item->artist_image) | |
{ | |
$bucket->icon = 'images/'.$item->artist_image; | |
} | |
$bucket->description = $item->desc; | |
$bucket->published = (int)$item->published; | |
// defaults | |
$bucket->created_by = 50; | |
$bucket->created = $this->getFirstDate($item->id,$db); | |
$bucket->hits = 0; | |
$bucket->access = 1; | |
$bucket->ordering = (int)$item->id; | |
$bucket->version = 1; | |
$bucket->metakey = SermondistributorHelper::safeString($item->artist_name,'L',','); | |
$bucket->metadesc = $item->artist_name; | |
$bucket->metadata = '{"robots":"","author":"","rights":""}'; | |
// Insert the object into the user profile table. | |
$done = JFactory::getDbo()->insertObject('#__sermondistributor_preacher', $bucket); | |
if ($done) | |
{ | |
$aId = $db->insertid(); | |
// make sure the access of asset is set | |
SermondistributorHelper::setAsset($aId,'preacher'); | |
} | |
} | |
} | |
// move the images | |
} | |
protected $firstDate = array(); | |
protected function getFirstDate($artist,$db) | |
{ | |
if (!isset($this->firstDate[$artist])) | |
{ | |
$query = $db->getQuery(true); | |
// Select song_id | |
$query->select('a.song_id'); | |
$query->from($db->quoteName('jos_jmsmusic_song_artists', 'a')); | |
$query->where($db->quoteName('artist_id')." = ".(int) $artist); | |
$query->order('a.song_id ASC'); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$id = $db->loadResult(); | |
// start new query | |
$query = $db->getQuery(true); | |
// Select song created date | |
$query->select('a.created'); | |
$query->from($db->quoteName('jos_jmsmusic_songs', 'a')); | |
$query->where($db->quoteName('id')." = ".(int) $id); | |
$db->setQuery($query); | |
$db->execute(); | |
if ($db->getNumRows()) | |
{ | |
$this->firstDate[$artist] = $db->loadResult(); | |
} | |
else | |
{ | |
$this->firstDate[$artist] = '2012-09-18 10:09:09'; | |
} | |
} | |
else | |
{ | |
$this->firstDate[$artist] = '2012-09-18 10:09:09'; | |
} | |
} | |
return $this->firstDate[$artist]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment