<commit_before><?php
/**
 * @copyright 2009-2020 City of Bloomington, Indiana
 * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE
 */
declare (strict_types=1);
namespace Application\Models;

use Web\ActiveRecord;
use Web\Database;

class Term extends ActiveRecord
{
	protected $tablename = 'terms';

	protected $seat;

	/**
	 * Populates the object with data
	 *
	 * Passing in an associative array of data will populate this object without
	 * hitting the database.
	 *
	 * Passing in a scalar will load the data from the database.
	 * This will load all fields in the table as properties of this class.
	 * You may want to replace this with, or add your own extra, custom loading
	 *
	 * @param int|string|array $id (ID, email, username)
	 */
	public function __construct($id=null)
	{
		if ($id) {
			if (is_array($id)) {
				$this->exchangeArray($id);
			}
			else {
				$db = Database::getConnection();
				$sql = 'select * from terms where id=?';

				$result = $db->createStatement($sql)->execute([$id]);
				if (count($result)) {
					$this->exchangeArray($result->current());
				}
				else {
					throw new \Exception('terms/unknownTerm');
				}
			}
		}
		else {
			// This is where the code goes to generate a new, empty instance.
			// Set any default values for properties that need it here
		}
	}

	/**
	 * Throws an exception if anything's wrong
	 * @throws Exception $e
	 */
	public function validate()
	{
		if (!$this->getStartDate()) {
			 $this->setStartDate(date(DATE_FORMAT));
		}

        $seat = $this->getSeat();
        if (!$seat) { throw new \Exception('terms/missingSeat'); }

        // Create a valid endDate if there isn't one already
        $termLength = new \DateInterval($seat->getTermLength());
        $oneDay     = new \DateInterval('P1D');
		if (!$this->getEndDate()) {
            $s = new \DateTime($this->getStartDate());
            $s->add($termLength);
            $s->sub($oneDay);
            $this->setEndDate($s->format(DATE_FORMAT));
        }

		// Make sure this term is not overlapping terms for the seat
		$db = Database::getConnection();
		$sql = "select id from terms
                where seat_id=?
                and (?<endDate and ?>startDate)";
		if ($this->getId()) { $sql.= ' and id!='.$this->getId(); }

		$result = $db->createStatement($sql)->execute([
            $this->getSeat_id(),
			$this->getStartDate(), $this->getEndDate()
		]);
		if (count($result) > 0) {
			throw new \Exception('terms/overlappingTerms');
		}
	}

	public function save() { parent::save(); }

	public function delete()
	{
        if ($this->isSafeToDelete()) {
            parent::delete();
        }
	}

	//----------------------------------------------------------------
	// Generic Getters & Setters
	//----------------------------------------------------------------<commit_msg>Fixed error: term default end date generation<commit_after><?php
/**
 * @copyright 2009-2020 City of Bloomington, Indiana
 * @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE
 */
declare (strict_types=1);
namespace Application\Models;

use Web\ActiveRecord;
use Web\Database;

class Term extends ActiveRecord
{
	protected $tablename = 'terms';

	protected $seat;

	/**
	 * Populates the object with data
	 *
	 * Passing in an associative array of data will populate this object without
	 * hitting the database.
	 *
	 * Passing in a scalar will load the data from the database.
	 * This will load all fields in the table as properties of this class.
	 * You may want to replace this with, or add your own extra, custom loading
	 *
	 * @param int|string|array $id (ID, email, username)
	 */
	public function __construct($id=null)
	{
		if ($id) {
			if (is_array($id)) {
				$this->exchangeArray($id);
			}
			else {
				$db = Database::getConnection();
				$sql = 'select * from terms where id=?';

				$result = $db->createStatement($sql)->execute([$id]);
				if (count($result)) {
					$this->exchangeArray($result->current());
				}
				else {
					throw new \Exception('terms/unknownTerm');
				}
			}
		}
		else {
			// This is where the code goes to generate a new, empty instance.
			// Set any default values for properties that need it here
		}
	}

	/**
	 * Throws an exception if anything's wrong
	 * @throws Exception $e
	 */
	public function validate()
	{
		if (!$this->getStartDate()) {
			 $this->setStartDate(date(DATE_FORMAT));
		}

        $seat = $this->getSeat();
        if (!$seat) { throw new \Exception('terms/missingSeat'); }

        // Create a valid endDate if there isn't one already
		if (!$this->getEndDate()) {
            $termLength = new \DateInterval($seat->getTermLength());
            $termMod    = $seat->getTermModifier();
            $oneDay     = new \DateInterval('P1D');

            $s = new \DateTime($this->getStartDate());
            $s->add($termLength);
            if ($termMod) { $s->modify($termMod); }
            $s->sub($oneDay);
            $this->setEndDate($s->format(DATE_FORMAT));
        }

		// Make sure this term is not overlapping terms for the seat
		$db  = Database::getConnection();
		$sql = "select id from terms
                where seat_id=?
                and (?<endDate and ?>startDate)";
		if ($this->getId()) { $sql.= ' and id!='.$this->getId(); }

		$result = $db->createStatement($sql)->execute([
            $this->getSeat_id(),
			$this->getStartDate(), $this->getEndDate()
		]);
		if (count($result) > 0) {
			throw new \Exception('terms/overlappingTerms');
		}
	}

	public function save() { parent::save(); }

	public function delete()
	{
        if ($this->isSafeToDelete()) {
            parent::delete();
        }
	}

	//----------------------------------------------------------------
	// Generic Getters & Setters
	//----------------------------------------------------------------
{'fraction_non_alphanumeric': 0.18878600823045266, 'fraction_numerical': 0.0037722908093278463, 'mean_word_length': 2.34077892325315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '6415450', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1685, 'n_words': 491}