function onAfterInitialize()
 {
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     if (!JFile::exists(JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php')) {
         return;
     }
     $app = JFactory::getApplication();
     if (!$app->isSite()) {
         return;
     }
     // synch the records if there are any changes
     if ($app->isSite()) {
         $db = JFactory::getDBO();
         $user = JFactory::getUser();
         $db->setQuery("\n                    Update\n                        #__contentbuilder_articles As articles,\n                        #__content As content, \n                        #__contentbuilder_forms As forms,\n                        #__contentbuilder_registered_users As cbusers,\n                        #__users As users\n                    Set \n                        content.state = 0\n                    Where \n                        articles.article_id = content.id\n                    And\n                        content.state = 1\n                    And\n                        articles.form_id = forms.id\n                    And\n                        forms.act_as_registration = 1\n                    And\n                        forms.id = cbusers.form_id\n                    And\n                        content.created_by = cbusers.user_id\n                    And\n                      (\n                        (\n                            users.id = cbusers.user_id\n                          And\n                            users.block = 1\n                        )\n                      )\n                    ");
         $db->query();
         $db->setQuery("\n                    Update \n                        #__contentbuilder_articles As articles,\n                        #__content As content, \n                        #__contentbuilder_forms As forms,\n                        #__contentbuilder_records As records,\n                        #__contentbuilder_registered_users As cbusers,\n                        #__users As users\n                    Set \n                        content.state = forms.auto_publish\n                    Where \n                        articles.article_id = content.id\n                    And\n                        content.state = 0\n                    And\n                        articles.form_id = forms.id\n                    And\n                        forms.act_as_registration = 1\n                    And\n                        forms.id = cbusers.form_id\n                    And\n                        content.created_by = cbusers.user_id\n                    And\n                        users.id = cbusers.user_id\n                    And\n                        records.record_id = cbusers.record_id\n                    And\n                        records.`type` = forms.`type`\n                    And\n                        users.block = 0\n                    ");
         $db->query();
         $pluginParams = CBCompat::getPluginParams($this, 'system', 'contentbuilder_system');
         require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php';
         $db->setQuery("\n                Select \n                    form.id As form_id,\n                    form.act_as_registration,\n                    form.default_category,\n                    form.registration_name_field, \n                    form.registration_username_field, \n                    form.registration_email_field, \n                    form.registration_email_repeat_field, \n                    form.`last_update`,\n                    article.`article_id`,\n                    form.`title_field`,\n                    form.`create_articles`,\n                    form.`name`,\n                    form.`use_view_name_as_title`,\n                    form.`protect_upload_directory`,\n                    form.`reference_id`,\n                    records.`record_id`,\n                    form.`type`,\n                    form.`published_only`,\n                    form.`own_only`,\n                    form.`own_only_fe`,\n                    records.`last_update` As record_last_update,\n                    article.`last_update` As article_last_update\n                From\n                    #__contentbuilder_records As records\n                    Left Join #__contentbuilder_forms As form On ( form.`type` = records.`type` And form.reference_id = records.reference_id )\n                    Left Join #__contentbuilder_articles As article On ( form.`type` = records.`type` And form.reference_id = records.reference_id And article.form_id = form.id And article.record_id = records.record_id )\n                    Left Join #__content As content On ( form.`type` = records.`type` And form.reference_id = records.reference_id And article.article_id = content.id And article.form_id = form.id And article.record_id = records.record_id )\n                Where \n                    form.`published` = 1\n                And\n                    form.create_articles = 1\n                And\n                    form.`type` = records.`type`\n                And \n                    form.reference_id = records.reference_id\n                And\n                   (\n                     (\n                        article.form_id = form.id \n                      And \n                        article.record_id = records.record_id\n                      And \n                        article.article_id = content.id \n                      And \n                        ( content.state = 1 Or content.state = 0 )\n                      And\n                      (\n                        form.`last_update` > article.`last_update`   \n                       Or\n                        records.`last_update` > article.`last_update`\n                      )\n                     )\n                     Or\n                     (\n                        form.id Is Not Null And records.id Is Not Null And content.id Is Null And article.id Is Null\n                     )\n                   )\n                Limit " . intval($pluginParams->def('limit_per_turn', 50)));
         $list = $db->loadAssocList();
         if (isset($list[0])) {
             $lang = JFactory::getLanguage();
             $lang->load('com_contentbuilder', JPATH_ADMINISTRATOR);
         }
         $jdate = JFactory::getDate();
         $now = CBCompat::toSql($jdate);
         foreach ($list as $data) {
             if (is_array($data)) {
                 $form = contentbuilder::getForm($data['type'], $data['reference_id']);
                 if (!$form || !$form->exists) {
                     return;
                 }
                 // creating the article
                 if ($data['create_articles']) {
                     $data['labels'] = $form->getElementLabels();
                     $ids = array();
                     foreach ($data['labels'] as $reference_id => $label) {
                         $ids[] = $db->Quote($reference_id);
                     }
                     if (count($ids)) {
                         $db->setQuery("Select Distinct `label`, reference_id From #__contentbuilder_elements Where form_id = " . intval($data['form_id']) . " And reference_id In (" . implode(',', $ids) . ") And published = 1 Order By ordering");
                         $rows = $db->loadAssocList();
                         $ids = array();
                         foreach ($rows as $row) {
                             $ids[] = $row['reference_id'];
                         }
                     }
                     $data['items'] = $form->getRecord($data['record_id'], false, -1, true);
                     $article_id = contentbuilder::createArticle($data['form_id'], $data['record_id'], $data['items'], $ids, $data['title_field'], $form->getRecordMetadata($data['record_id']), array(), false, 1, $data['default_category']);
                     if ($article_id) {
                         $db->setQuery("Update #__contentbuilder_articles Set `last_update`=" . $db->Quote($now) . " Where article_id = " . $db->Quote($article_id) . " And record_id = " . $db->Quote($data['record_id']) . " And form_id = " . $db->Quote($data['form_id']));
                         $db->query();
                     }
                 }
             }
         }
     }
 }