<commit_before><?php

/*
 * See docs/AUTHORS and docs/COPYRIGHT for relevant info.
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @license http://opensource.org/licenses/lgpl-3.0.html
 */

namespace properties\Factory;

use properties\Resource\Sublease as Resource;
use properties\Factory\Sublease\Photo as Photo;
use phpws2\Database;
use phpws2\Settings;

class Sublease extends Base
{

    public $more_rows = true;

    public function build()
    {
        return new Resource;
    }

    public function delete(Resource $sublease)
    {
        $photo = new Photo;
        $photo->removeBySublease($sublease->id);
        self::deleteResource($sublease);
    }

    public function listing(\Canopy\Request $request, $admin = false)
    {
        $listing = new Sublease\Listing();
        $listing->pullVariables($request);
        $result = $listing->get(true);
        $this->more_rows = $listing->more_rows;
        return $result;
    }

    public function getSubleaseByUser($user_id)
    {
        if (empty($user_id)) {
            throw new \properties\Exception\ResourceNotFound;
        }
        $db = Database::getDB();
        $tbl = $db->addTable('prop_sublease');
        $tbl->addFieldConditional('user_id', $user_id);
        $row = $db->selectOneRow();
        if (empty($row)) {
            return null;
        }
        $sublease = $this->build();
        $sublease->setVars($row);
        return $sublease;
    }

    public function save(Resource $sublease)
    {
        $sublease->updated = time();
        $sublease->forwardTimeout();
        self::saveResource($sublease);
        return $sublease->id;
    }

    public function post(\Canopy\Request $request, $user_id)
    {
        $sublease = new Resource;
        $sublease->user_id = $user_id;
        try {
            $sublease->loadPostByType($request,
                    array('active', 'timeout', 'created', 'updated', 'user_id', 'id', 'thumbnail'));
            $sublease->active = true;
            $sublease->created = time();
            return array('id' => $this->save($sublease));
        } catch (\Exception $e) {
            throw new \properties\Exception\PropertySaveFailure($e->getMessage());
        }
    }

    public function view($sublease, $admin = false)
    {
        if (empty($sublease)) {
            throw new \properties\Exception\ResourceNotFound($sublease);
        }
        if (is_numeric($sublease)) {
            $sublease = $this->load($sublease);
        } elseif (!is_a($sublease, 'properties\Resource\Sublease')) {
            throw new \properties\Exception\ResourceNotFound;
        }

        if (!$sublease->active && !$admin) {
            $template = new \phpws2\Template();
            $template->setModuleTemplate('properties',
                    'errorpage/ResourceNotFound.html');
        } else {
            $tpl = $sublease->view();
            $tpl['inactive_warning'] = $sublease->active ? false : true;
            $tpl['photo'] = $this->reactView('subleasephoto');
            $tpl['photoupdate'] = $admin ? $this->reactView('subleaseimage') : null;
            $photoFactory = new Photo;
            $tpl['current_photos'] = json_encode($photoFactory->thumbs($sublease->id));
            if (\Current_User::isDeity()) {
                $tpl['ban_user'] = $this->reactView('banuser');
                $tpl['user_id'] = $sublease->user_id;
            } else {
                $tpl['ban_user'] = $tpl['user_id'] = null;
            }
            $seconds_left = $sublease->timeout - time();
            if ($admin){
                if ($seconds_left > 0) {
                    $days_left = floor($seconds_left / 86400);
                    $tpl['inactive_notice'] = "This sublease will remain active for $days_left more days.";
                } else {
                    $tpl['inactive_notice'] = "This sublease is scheduled to be deactivated. <a href='./properties/Sublease/extend' style='color:black'>Click here to extend your deadline.</a>";
                }
            } else {
                $tpl['inactive_notice'] = null;
            }
            $template = new \phpws2\Template($tpl);
            $template->setModuleTemplate('properties', 'sublease/view.html');
        }<commit_msg>Updated object name to prevent errors.<commit_after><?php

/*
 * See docs/AUTHORS and docs/COPYRIGHT for relevant info.
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * @author <NAME> <<EMAIL>ey at gmail dot com>
 *
 * @license http://opensource.org/licenses/lgpl-3.0.html
 */

namespace properties\Factory;

use properties\Resource\Sublease as Resource;
use properties\Factory\Sublease\Photo as SubPhoto;
use phpws2\Database;
use phpws2\Settings;

class Sublease extends Base
{

    public $more_rows = true;

    public function build()
    {
        return new Resource;
    }

    public function delete(Resource $sublease)
    {
        $photo = new SubPhoto;
        $photo->removeBySublease($sublease->id);
        self::deleteResource($sublease);
    }

    public function listing(\Canopy\Request $request, $admin = false)
    {
        $listing = new Sublease\Listing();
        $listing->pullVariables($request);
        $result = $listing->get(true);
        $this->more_rows = $listing->more_rows;
        return $result;
    }

    public function getSubleaseByUser($user_id)
    {
        if (empty($user_id)) {
            throw new \properties\Exception\ResourceNotFound;
        }
        $db = Database::getDB();
        $tbl = $db->addTable('prop_sublease');
        $tbl->addFieldConditional('user_id', $user_id);
        $row = $db->selectOneRow();
        if (empty($row)) {
            return null;
        }
        $sublease = $this->build();
        $sublease->setVars($row);
        return $sublease;
    }

    public function save(Resource $sublease)
    {
        $sublease->updated = time();
        $sublease->forwardTimeout();
        self::saveResource($sublease);
        return $sublease->id;
    }

    public function post(\Canopy\Request $request, $user_id)
    {
        $sublease = new Resource;
        $sublease->user_id = $user_id;
        try {
            $sublease->loadPostByType($request,
                    array('active', 'timeout', 'created', 'updated', 'user_id', 'id', 'thumbnail'));
            $sublease->active = true;
            $sublease->created = time();
            return array('id' => $this->save($sublease));
        } catch (\Exception $e) {
            throw new \properties\Exception\PropertySaveFailure($e->getMessage());
        }
    }

    public function view($sublease, $admin = false)
    {
        if (empty($sublease)) {
            throw new \properties\Exception\ResourceNotFound($sublease);
        }
        if (is_numeric($sublease)) {
            $sublease = $this->load($sublease);
        } elseif (!is_a($sublease, 'properties\Resource\Sublease')) {
            throw new \properties\Exception\ResourceNotFound;
        }

        if (!$sublease->active && !$admin) {
            $template = new \phpws2\Template();
            $template->setModuleTemplate('properties',
                    'errorpage/ResourceNotFound.html');
        } else {
            $tpl = $sublease->view();
            $tpl['inactive_warning'] = $sublease->active ? false : true;
            $tpl['photo'] = $this->reactView('subleasephoto');
            $tpl['photoupdate'] = $admin ? $this->reactView('subleaseimage') : null;
            $photoFactory = new SubPhoto;
            $tpl['current_photos'] = json_encode($photoFactory->thumbs($sublease->id));
            if (\Current_User::isDeity()) {
                $tpl['ban_user'] = $this->reactView('banuser');
                $tpl['user_id'] = $sublease->user_id;
            } else {
                $tpl['ban_user'] = $tpl['user_id'] = null;
            }
            $seconds_left = $sublease->timeout - time();
            if ($admin){
                if ($seconds_left > 0) {
                    $days_left = floor($seconds_left / 86400);
                    $tpl['inactive_notice'] = "This sublease will remain active for $days_left more days.";
                } else {
                    $tpl['inactive_notice'] = "This sublease is scheduled to be deactivated. <a href='./properties/Sublease/extend' style='color:black'>Click here to extend your deadline.</a>";
                }
            } else {
                $tpl['inactive_notice'] = null;
            }
            $template = new \phpws2\Template($tpl);
            $template->setModuleTemplate('properties', 'sublease/view.html');
        }
{'fraction_non_alphanumeric': 0.13591301566997122, 'fraction_numerical': 0.0027715595352307857, 'mean_word_length': 2.240759930915371, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2820013', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2602, 'n_words': 613}