1

我在这个特定的应用程序上使用 CakePHP。通过 在网站上提交博客文章时add(),它应该通过 向管理员发送通知电子邮件pending_email(),但我收到以下错误:

Fatal error: Call to undefined function pending_email() in .../app/controllers/blog_posts_controller.php on line 134

这是代码:

function add($blog_id = null) {
    if (!empty($this->data)) {
        switch ($this->params['form']['submit']) {
            case 'draft':
                $this->data['BlogPost']['status_id'] = DRAFT;
                break;
            case 'approval':
                $this->data['BlogPost']['status_id'] = PENDING;
                break;
            case 'publish':
                $this->data['BlogPost']['status_id'] = PUBLISHED;
                break;
        }
        $this->data['BlogPost']['tags'] = $this->__tagCleanup($this->data['BlogPost']['tags']);
        $this->BlogPost->create();
        if ($this->BlogPost->save($this->data)) {
            if ($this->data['BlogPost']['status_id'] == PUBLISHED) {
                $this->__tagUp($this->data['BlogPost']['blog_id'], $this->data['BlogPost']['tags']);
            }

            // Send the e-mail notating that a blog is pending
            pending_email($blog_id);          

            $this->Session->setFlash(__('The blog post has been saved', true));
            $this->redirect(array('action' => 'index', $this->data['BlogPost']['blog_id']));
           } else {
            $this->Session->setFlash(__('The blog post could not be saved. Please, try again.', true));
        }
    }
    // fetch blog_post context
    $this->BlogPost->Blog->recursive = -1;
    $blog = $this->BlogPost->Blog->read(array('id','title'),$blog_id);
    $this->set('blog', $blog);

    $this->data['BlogPost']['user_id'] = $this->user_id;
    if (!is_null($blog_id)) {
        $this->data['BlogPost']['blog_id'] = $blog_id;
    }


}


function pending_email($id) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid blog post', true));
            $this->redirect(array('action' => 'index'));
        }

        $this->BlogPost->contain(array(
            'User' => array('fields' => 'id', 'full_name', 'name', 'last_name'),
            'Tag' => array('fields' => 'name'),
            'BlogPostComment' => array('fields' => array('created', 'content')),
            'BlogPostComment.User' => array('fields' => array('name', 'last_name')),
        ));

        if(($blogPost = $this->BlogPost->read(null, $id)) == NULL){
            $this->Session->setFlash(__('Invalid blog post', true));
            $this->redirect(array('controller'=>'spotlights', 'action' => 'index'));
        }

        $this->set('blogPost', $this->BlogPost->read(null, $id));
        $temp = $this->BlogPost->read(null, $id);
        $this->BlogPost->Blog->recursive = -1;

        //Blog information
        $title = $temp['BlogPost']['title'];
        $author = $temp['User']['full_name'];
        $date_created = $temp['BlogPost']['created'];

        // Properly format the excerpt
        $excerpt = preg_replace("/&#?[a-z0-9]+;/i","", $temp['BlogPost']['content']);

        $content = "<h2>Hello,</h2><p>$author has submitted a new blog post for review. <ul><li><b>Title:</b> $title </li><li><b>Author</b>: $author</li><li><b>Excerpt</b>: \"$excerpt\"</li><li><b>Created on:</b> $date_created</li></ul></p><p>You can log into the dashboard to approve this post.</p>";
        $to = ADMIN_EMAIL;
        $from = SMTP_FROM;
        $subject = "New blog post submitted by $author";
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: ' . $from . "\r\n" .
        'Reply-To: ' . $from . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        //Send the e-mail
        mail($to, $subject, $content, $headers);
}  

为了隐私起见,我删掉了一些东西,但不应该涉及这个问题。一如既往,非常感谢任何想法!

4

4 回答 4

7

由于pending_email不是全局函数并且是控制器上的方法,因此您需要这样调用它:

$this->pending_email($blog_id);

关键字是$this指博客文章控制器类。

于 2012-10-23T19:07:11.287 回答
6

由于这些方法在您的控制器类中,您应该使用$this->pending_email(),而不仅仅是pending_email()

$this->pending_email($blog_id);
于 2012-10-23T19:07:10.927 回答
1

你在一个对象中,你需要用 $this 来引用它。

 $this->pending_email($blog_id);
于 2012-10-23T19:08:25.493 回答
1

我想你应该使用:

$this->pending_email($blog_id);

它会起作用的。

于 2012-10-23T19:16:39.620 回答