コード例 #1
0
ファイル: api.php プロジェクト: kidaa30/lojinha
    protected function initialise()
    {
        // already initialised
        if ((int) $this->id > 0) {
            return true;
        }
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from('#__djrevs_objects');
        $query->where('entry_id=' . (int) $this->entry_id);
        $query->where('object_type=' . $db->quote($this->object_type));
        // not sure about this
        $query->where('rating_group_id=' . (int) $this->rating_group_id);
        $db->setQuery($query);
        $object = $db->loadObject();
        $update_needed = false;
        $is_new = true;
        if ($object) {
            $is_new = false;
            $this->id = $object->id;
            if ($this->name != $object->name) {
                //$this->name 	= $object->name;
                $update_needed = true;
            }
            if ($this->link != $object->link) {
                //$this->link 	= $object->link;
                $update_needed = true;
            }
            if ($this->rating_group_id != $object->rating_group_id) {
                //$this->rating_group_id 	= $object->rating_group_id;
                $update_needed = true;
            }
            $this->avg_rate = $object->avg_rate;
        }
        if ($is_new || $update_needed) {
            $row = new stdClass();
            $row->id = $this->id;
            $row->object_type = $this->object_type;
            $row->entry_id = $this->entry_id;
            $row->rating_group_id = $this->rating_group_id;
            $row->name = $this->name;
            $row->link = $this->link;
            $row->avg_rate = $this->avg_rate;
            if ($is_new) {
                try {
                    $db->insertObject('#__djrevs_objects', $row, 'id');
                } catch (RuntimeException $e) {
                    if (defined('JDEBUG') && JDEBUG) {
                        JLog::add('DJ-Reviews - Cannot initialise object', JLOG::ERROR);
                    }
                    return false;
                }
                $this->id = $row->id;
            } else {
                if ($update_needed) {
                    try {
                        $db->updateObject('#__djrevs_objects', $row, 'id', false);
                    } catch (RuntimeException $e) {
                        if (defined('JDEBUG') && JDEBUG) {
                            JLog::add('DJ-Reviews - Cannot update object', JLOG::ERROR);
                        }
                        return false;
                    }
                }
            }
        }
        if (!self::$js_core) {
            $version = new JVersion();
            $document = JFactory::getDocument();
            if ($this->params->get('jquery', '0') != '3') {
                if ($this->params->get('jquery', '0') == '0' && JFile::exists(JPath::clean(JPATH_ROOT . '/libraries/cms/html/jquery.php'))) {
                    JHtml::_('jquery.framework', true);
                } else {
                    if ($this->params->get('jquery') == '1') {
                        $document->addScript(JUri::base() . '/components/com_djreviews/assets/js/jquery.min.js');
                    } else {
                        $document->addScript('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js');
                    }
                }
            }
            $document->addScript(JUri::base() . 'components/com_djreviews/assets/js/core.js');
            $options = array('url' => JUri::base(false), 'object_url' => $this->link, 'object_id' => $this->id);
            JFactory::getDocument()->addScriptDeclaration('
					jQuery(document).ready(function(){
                            DJReviewsCore.init(' . json_encode($options) . ');
                        });
					');
            self::$js_core = true;
        }
        return true;
    }