<commit_before><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Shortcut methods to perform some operations.
 *
 * Modelled on the django.shortcuts.
 */

/**
 * Get an object by id or raise a 404 error.
 *
 * @param string Model
 * @param int Id of the model to get
 * @return Object The found object.
 */
function Pluf_Shortcuts_GetObjectOr404($object, $id)
{
    $item = new $object($id);
    if ((int)$id > 0 && $item->id == $id) {
        return $item;
    }
    throw new Pluf_HTTP_Error404();
}

/**
 * Render a template file and an array as a reponse.
 *
 * If a none null request object is given, the context used will
 * automatically be a Pluf_Template_Context_Request context and thus
 * the context will be populated using the
 * 'template_context_processors' functions.
 *
 * @param string Template file name
 * @param array Associative array for the context
 * @param Pluf_HTTP_Request Request object (null)
 * @return Pluf_HTTP_Response The response with the rendered template
 */
function Pluf_Shortcuts_RenderToResponse($tplfile, $params, $request=null)
{
    $tmpl = new Pluf_Template($tplfile);
    if (is_null($request)) {
        $context = new Pluf_Template_Context($params);
    } else {
        $context = new Pluf_Template_Context_Request($request, $params);
    }
    return new Pluf_HTTP_Response($tmpl->render($context));
}


/**
 * Get a given form from a model.
 *<commit_msg>Added a shortcuts to get one object or throw a 404 exception.<commit_after><?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 <NAME> and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Shortcut methods to perform some operations.
 *
 * Modelled on the django.shortcuts.
 */

/**
 * Get an object by id or raise a 404 error.
 *
 * @param string Model
 * @param int Id of the model to get
 * @return Object The found object.
 */
function Pluf_Shortcuts_GetObjectOr404($object, $id)
{
    $item = new $object($id);
    if ((int)$id > 0 && $item->id == $id) {
        return $item;
    }
    throw new Pluf_HTTP_Error404();
}

/**
 * Get an object by SQL or raise a 404 error.
 *
 * Usage:
 * <pre>
 * $obj = Pluf_Shortcuts_GetOneOr404('MyApp_Model',
 *                                   'path=%s AND status=%s',
 *                                    array('welcome', 1));
 * </pre>
 *
 * @param string Model
 * @param string Base SQL request
 * @param string Parameters for the base SQL
 * @return Object The found object
 */
function Pluf_Shortcuts_GetOneOr404($object, $bsql, $psql)
{
    $sql = new Pluf_SQL($bsql, $psql);
    $item = Pluf::factory($object)->getOne(array('filter' => $sql->gen()));
    if ($item != null) {
        return $item;
    }
    throw new Pluf_HTTP_Error404();
}

/**
 * Render a template file and an array as a reponse.
 *
 * If a none null request object is given, the context used will
 * automatically be a Pluf_Template_Context_Request context and thus
 * the context will be populated using the
 * 'template_context_processors' functions.
 *
 * @param string Template file name
 * @param array Associative array for the context
 * @param Pluf_HTTP_Request Request object (null)
 * @return Pluf_HTTP_Response The response with the rendered template
 */
function Pluf_Shortcuts_RenderToResponse($tplfile, $params, $request=null)
{
    $tmpl = new Pluf_Template($tplfile);
    if (is_null($request)) {
        $context = new Pluf_Template_Context($params);
    } else {
        $context = new Pluf_Template_Context_Request($request, $params);
    }
    return new Pluf_HTTP_Response($tmpl->render($context));
}


/**
 * Get a given form from a model.
 *
{'fraction_non_alphanumeric': 0.10618820944708898, 'fraction_numerical': 0.015012815818381545, 'mean_word_length': 3.5449251247920133, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7424181', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1563, 'n_words': 648}