<commit_before><?php

/**
 * Controller used to render pages in the catalogue (either categories
 * or pages)
 *
 * @author i-lateral (http://www.i-lateral.com)
 * @package catalogue
 */
class CatalogueProductController extends CatalogueController
{

    /**
     * Template names to be removed from the default template list 
     * 
     * @var array
     * @config
     */
    private static $classes_to_remove = array(
        "Object",
        "ViewableData",
        "DataObject",
        "CatalogueProduct"
    );

    private static $allowed_actions = array(
        'iid',
        'Form'
    );

    /**
     * Return the link to this controller, but force the expanded link to be returned so that form methods and
     * similar will function properly.
     *
     * @return string
     */
    public function Link($action = null)
    {
        return $this->data()->Link(($action ? $action : true));
    }

    /**
     * The Controller will take the URLSegment parameter from the URL
     * and use that to look up a record.
     */
    public function __construct($dataRecord = null)
    {
        if (!$dataRecord) {
            $dataRecord = new CatalogueProduct();
            if ($this->hasMethod("Title")) {
                $dataRecord->Title = $this->Title();
            }
            $dataRecord->URLSegment = get_class($this);
            $dataRecord->ID = -1;
        }
        
        $this->dataRecord = $dataRecord;
        $this->failover = $this->dataRecord;
        parent::__construct();
    }
    
    protected function get_index_templates()
    {
        $classes = ClassInfo::ancestry($this->dataRecord->class);
        $classes = array_reverse($classes);
        $remove_classes = self::config()->classes_to_remove;
        $return = array();

        array_push($classes, "Catalogue", "Page");

        foreach ($classes as $class) {
            if (!in_array($class, $remove_classes)) {
                $return[] = $class;
            }
        }
        
        return $return;
    }
    
    /**
     * The productimage action is used to determine the default image that will
     * appear related to a product
     *
     * @return Image
     */
    public function getImageForProduct()
    {
        $images = $this->SortedImages();
        $action = $this->request->param('Action');
        $id = $this->request->param('ID');

        $image = null;

        if ($action && $action === "iid" && $id) {
            $image = $images->filter("ID", $id)->first();
        }

        if (!$image) {
            $image = $images->first();
        }
            
        $this->extend("updateImageForProduct", $image);

        return $image;
    }

    /**
     * Get a list of templates to call and return a default render with
     */
    public function index()
    {
        $this->customise(array("ProductImage" => $this->getImageForProduct()));
        
        $this->extend("onBeforeIndex");
        
        return $this->renderWith($this->get_index_templates());
    }
    
    /**
     * Get a list of templates to call and return a default render with
     */
    public function iid()
    {
        $this->customise(array("ProductImage" => $this->getImageForProduct()));
        
        $this->extend("onBeforeIndex");
        
        return $this->renderWith($this->get_index_templates());
    }<commit_msg>Switch to using find on product image<commit_after><?php

/**
 * Controller used to render pages in the catalogue (either categories
 * or pages)
 *
 * @author i-lateral (http://www.i-lateral.com)
 * @package catalogue
 */
class CatalogueProductController extends CatalogueController
{

    /**
     * Template names to be removed from the default template list 
     * 
     * @var array
     * @config
     */
    private static $classes_to_remove = array(
        "Object",
        "ViewableData",
        "DataObject",
        "CatalogueProduct"
    );

    private static $allowed_actions = array(
        'iid',
        'Form'
    );

    /**
     * Return the link to this controller, but force the expanded link to be returned so that form methods and
     * similar will function properly.
     *
     * @return string
     */
    public function Link($action = null)
    {
        return $this->data()->Link(($action ? $action : true));
    }

    /**
     * The Controller will take the URLSegment parameter from the URL
     * and use that to look up a record.
     */
    public function __construct($dataRecord = null)
    {
        if (!$dataRecord) {
            $dataRecord = new CatalogueProduct();
            if ($this->hasMethod("Title")) {
                $dataRecord->Title = $this->Title();
            }
            $dataRecord->URLSegment = get_class($this);
            $dataRecord->ID = -1;
        }
        
        $this->dataRecord = $dataRecord;
        $this->failover = $this->dataRecord;
        parent::__construct();
    }
    
    protected function get_index_templates()
    {
        $classes = ClassInfo::ancestry($this->dataRecord->class);
        $classes = array_reverse($classes);
        $remove_classes = self::config()->classes_to_remove;
        $return = array();

        array_push($classes, "Catalogue", "Page");

        foreach ($classes as $class) {
            if (!in_array($class, $remove_classes)) {
                $return[] = $class;
            }
        }
        
        return $return;
    }
    
    /**
     * The productimage action is used to determine the default image that will
     * appear related to a product
     *
     * @return Image
     */
    public function getImageForProduct()
    {
        $sorted_images = $this->SortedImages();
        $images = $this->Images();
        $image = null;
        $action = $this->request->param('Action');
        $id = $this->request->param('ID');

        if ($action && $action === "iid" && $id) {
            $image = $images->find("ID", $id);
        }

        if (!$image) {
            $image = $sorted_images->first();
        }
            
        $this->extend("updateImageForProduct", $image);

        return $image;
    }

    /**
     * Get a list of templates to call and return a default render with
     */
    public function index()
    {
        $this->customise(array("ProductImage" => $this->getImageForProduct()));
        
        $this->extend("onBeforeIndex");
        
        return $this->renderWith($this->get_index_templates());
    }
    
    /**
     * Get a list of templates to call and return a default render with
     */
    public function iid()
    {
        $this->customise(array("ProductImage" => $this->getImageForProduct()));
        
        $this->extend("onBeforeIndex");
        
        return $this->renderWith($this->get_index_templates());
    }
{'fraction_non_alphanumeric': 0.13268465280849182, 'fraction_numerical': 0.0002948547840188707, 'mean_word_length': 1.9767441860465116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2758414', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1847, 'n_words': 478}