【问题标题】:using joomla function into external php script在外部 php 脚本中使用 joomla 函数
【发布时间】:2014-01-07 19:54:02
【问题描述】:

我已经安装了自定义函数的 joomla,我需要在我拥有的另一个 php 脚本中使用这些自定义函数 在 joomla 站点中,我在 library>rvv>factory.php 中有函数 和组件>com_customs 文件夹

在视图 tmpl 页面上显示的功能之一是<?php $points = RFactory::getPoint($downLine->agentID); ?> <th><?php echo $points['left'].' - '.$points['right']; ?></th>

关于组件功能脚本文件

public function getDownLine($agentId)
{
    ini_set('max_execution_time', 600); //300 seconds = 5 minutes

    $this->downLinesRecursion($agentId);

    function cmp( $a, $b )
    { 
      if(  $a->agentID ==  $b->agentID ){ return 0 ; } 
      return ($a->agentID < $b->agentID) ? -1 : 1;
    }

    usort($this->downLine,'cmp');

    $this->_total = count($this->downLine);

    $endDownLine = array_splice($this->downLine, $this->getState('limitstart'), $this->getState('limit'));

    return $endDownLine;
}



protected function downLinesRecursion($agentId) 
{
    $downLineIDs = RFactory::getDownLines($agentId);
    if($downLineIDs){
        foreach ($downLineIDs as $downLineID){
            $agent = RFactory::getAgent($downLineID->id);
            $this->downLine[] = $agent;
            $this->downLinesRecursion($agent->agentID);
        }
    }
}

public function getAgentExisting($agentId)
{
    ini_set('max_execution_time', 300); //300 seconds = 5 minutes
    $this->subAgentID = $agentId;
    $this->existingRecursion($this->agentID);

    return $this->agentExisting;
}

protected function existingRecursion($agentId)
{
    $downLineIDs = RFactory::getDownLines($agentId);
    if($downLineIDs){
        foreach ($downLineIDs as $downLineID){
            if($downLineID->id == $this->subAgentID){
                $this->agentExisting = true;
            }else{
                $this->existingRecursion($downLineID->id);
            }
        }
    }
}


public function getAgentId($username)
{
    $userId = $this->getUserId($username);
    if($userId){
        $agentIds = RFactory::getAgentIds($userId);
        foreach ($agentIds as $AgentId) {
            $agentId = $AgentId->id;
            break;
        }
        return $agentId;
    }else {
        return false;
    }
}

关于工厂文件:

public static function getAgent($agentId)
{
    $db    = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select(' ag.id AS agentID, ag.user_id AS agentUserID, ag.parent_client_id, ag.directe_client_id, ag.position, date(ag.reg_time) AS joinDate, ag.offers_id, ag.cash');
    $query->select(' ofr.* ');
    $query->select(' usrinf.* ');
    $query->select(' usr.name AS agentName, usr.username, usr.email, date(usr.registerDate) AS regDate ');
    $query->select(' stk.value AS stockValue ');
    $query->from(' #__r_clients AS ag ');
    $query->leftJoin(' #__r_offers AS ofr ON ofr.id = ag.offers_id ');
    $query->leftJoin(' #__r_clients_info AS usrinf ON usrinf.user_id = ag.user_id ');
    $query->leftJoin(' #__users AS usr On usr.id = ag.user_id ');
    $query->leftJoin(' #__r_stock AS stk On stk.client_id = ag.id ');
    $query->where(' ag.id = '.$agentId);
    $db->setQuery((string)$query);
    $data = $db->loadObject();
    if (!$db->query()) {
        JError::raiseError(500, $db->getErrorMsg());
        return false;
    }else {
        if ($db->getNumRows() < 1){
            return false;
        }else{
            return $data;
        }
    }
}

public static function getAgents()
{
    $db     = JFactory::getDBO();
    $query  = $db->getQuery(true);
    $query->select(' ag.id AS agentID, ag.user_id AS agentUserID, ag.parent_client_id, ag.directe_client_id, ag.position, date(ag.reg_time) AS joinDate, ag.offers_id, ag.cash');
    $query->select(' ofr.* ');
    $query->select(' usr.name AS agetName, usr.username, usr.email, usr.registerDate ');
    $query->from(' #__r_clients AS ag ');
    $query->leftJoin(' #__r_offers AS ofr ON ofr.id = ag.offers_id ');
    $query->leftJoin(' #__users AS usr On usr.id = ag.user_id ');
    $db->setQuery((string)$query);
    $data = $db->loadObjectList();
    if (!$db->query()) {
        JError::raiseError(500, $db->getErrorMsg());
        return false;
    }else {
        if ($db->getNumRows() < 1){
            return false;
        }else{
            return $data;
        }
    }
}


public static function getDownLines($agentId)
{
    $db    = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select(' id ');
    $query->from(' #__r_clients ');
    $query->where(' parent_client_id = '.$agentId);
    $db->setQuery((string)$query);
    $data = $db->loadObjectList();
    if (!$db->query()) {
        JError::raiseError(500, $db->getErrorMsg());
        return false;
    }else {
        if ($db->getNumRows() < 1){
            return false;
        }else{
            return $data;
        }
    }
}

public static function getPoint($agentId)
{
    $downLines = self::getDownLines($agentId);
    self::$rightPoint = 0;
    self::$leftPoint = 0;
    if($downLines){
        foreach ($downLines as $downLine){
            $agent = self::getAgent($downLine->id);
            if($agent->position == 'R'){
                self::$rightPoint += $agent->point;
                self::rightPointRecursion($agent->agentID);
            }else {
                self::$leftPoint += $agent->point;
                self::leftPointRecursion($agent->agentID);
            }
        }
        return $pointArray = array('right' => self::$rightPoint, 'left' => self::$leftPoint);
    }else {
        return $pointArray = array('right' => 0, 'left' => 0);
    }
}

private function rightPointRecursion($agentId)
{
    $downLines = self::getDownLines($agentId);
    if($downLines){
        foreach ($downLines as $downLine){
            $agent = self::getAgent($downLine->id);
            self::$rightPoint += $agent->point;
            self::rightPointRecursion($agent->agentID);
        }
    }
}

private function leftPointRecursion($agentId)
{
    $downLines = self::getDownLines($agentId);
    if($downLines){
        foreach ($downLines as $downLine){
            $agent = self::getAgent($downLine->id);
            self::$leftPoint += $agent->point;
            self::leftPointRecursion($agent->agentID);
        }
    }
}

自定义 joomla 代码在组件内>com_hierarchical>hierarchical.php 包含:

JLoader::register('HierarchicalHelper', dirname(__FILE__) . DS . 'helpers'. DS . 'hierarchical.php');

jimport('joomla.application.component.controller');

$document =& JFactory::getDocument();
 $script = JURI::base(true).'/components/com_hierarchical/assist/gscript.js';
 $style = JURI::base(true).'/components/com_hierarchical/assist/gstyle.css';
 $document->addScript($script);
 $document->addStyleSheet($style);

 $controller    = JController::getInstance('Hierarchical');
 $controller->execute(JFactory::getApplication()->input->get('task'));
 $controller->redirect();

我如何在 joomla 脚本之外的另一个 php 文件中使用这些函数?

【问题讨论】:

    标签: php mysql joomla


    【解决方案1】:

    在您的外部 php 脚本中只需加载 Joomla 框架,

    那么你就可以在你的外部 php 文件中使用所有的 Joomla 函数和对象了。

    define( '_JEXEC', 1 );
    define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
    define( 'DS', DIRECTORY_SEPARATOR );
    
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    
    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();
    

    希望对你有所帮助..

    【讨论】:

    • 你能否给出更详细的例子来使用像 'agentID); 这样的函数?> ' 在 php 文件中,我只是将您的行添加到 joomla 根目录中的外部 php 文件中,但仍然无法使用该函数获取结果。
    • 您的 Joomla 安装的外部脚本路径必须给出 define('JPATH_BASE', "here---");然后在页面顶部添加脚本,然后只需使用 Joomla 函数,如下所示。$points = RFactory::getPoint($downLine->agentID);
    • 403 Forbidden 请检查
    • 对不起,请再检查一遍
    • 你创建了这个工厂文件吗?然后在你的脚本上导入,比如 require_once ( JPATH_BASE .DS.'includes'.DS.'rfactory.php' );我认为它不是 joomla 的默认库,默认的是 JFactory
    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2012-03-24
    • 2012-12-13
    相关资源
    最近更新 更多