<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\Manager;

use phpws2\Database;
use properties\Resource\Manager as Resource;
use properties\Exception\MissingInput;
use properties\Exception\PrivilegeMissing;

class Listing
{

    public $limit = 20;
    public $offset = 0;
    public $search = null;
    public $active = 1; // null pulls all
    public $approved = 1; // null pulls all
    public $include_property_count = true;
    public $restricted = true;
    public $view = false;
    public $orderby = 'company_name';
    public $orderby_dir = 'asc';
    public $include_inquiry = true;
    public $full_search = false;
    public $must_have_property = false;
    public $more_rows = true;

    public function get()
    {
        if ((int) $this->limit < 0 || (int) $this->limit > 20) {
            $this->limit = 20;
        }


        $groups = array();
        $db = Database::getDB();

        if ($this->limit) {
            $offset = $this->offset * $this->limit;
            $db->setLimit($this->limit, $offset);
        }
        
        $tbl = $db->addTable('prop_contacts');

        if ($this->include_property_count) {
            $tbl2 = $db->addTable('properties');
            $property_conditional1 = $db->createConditional($tbl2->getField('active'),
                    1);
            $property_conditional2 = $db->createConditional($tbl2->getField('active'),
                    null, 'is');
            $property_conditional3 = $db->createConditional($property_conditional1,
                    $property_conditional2, 'or');
            $db->addConditional($property_conditional3);
            $count_field = $tbl2->addField('id', 'property_count');
            $count_field->showCount();
            if ($this->must_have_property) {
                $tbl2->addHaving($count_field, 0, '>');
            }
        }
        if ($this->include_inquiry) {
            $tbl3 = $db->addTable('prop_inquiry');
            $groups[] = $tbl3->addField('inquiry_date');
            $groups[] = $tbl3->addField('inquiry_type');
        }

        // avoiding the password field
        $groups[] = $tbl->addField('id');
        $tbl->addField('username');
        $tbl->addField('phone');
        $tbl->addField('email_address');
        $tbl->addField('company_name');
        $tbl->addField('company_address');
        $tbl->addField('company_url');
        $tbl->addField('times_available');
        $tbl->addField('first_name');
        $tbl->addField('last_name');<commit_msg>must_have_property fixed.<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>>
 *
 * @license http://opensource.org/licenses/lgpl-3.0.html
 */

namespace properties\Factory\Manager;

use phpws2\Database;
use properties\Resource\Manager as Resource;
use properties\Exception\MissingInput;
use properties\Exception\PrivilegeMissing;

class Listing
{

    public $limit = 20;
    public $offset = 0;
    public $search = null;
    public $active = 1; // null pulls all
    public $approved = 1; // null pulls all
    public $include_property_count = true;
    public $restricted = true;
    public $view = false;
    public $orderby = 'company_name';
    public $orderby_dir = 'asc';
    public $include_inquiry = true;
    public $full_search = false;
    public $must_have_property = false;
    public $more_rows = true;

    public function get()
    {
        if ((int) $this->limit < 0 || (int) $this->limit > 20) {
            $this->limit = 20;
        }


        $groups = array();
        $db = Database::getDB();

        if ($this->limit) {
            $offset = $this->offset * $this->limit;
            $db->setLimit($this->limit, $offset);
        }

        $tbl = $db->addTable('prop_contacts');

        if ($this->include_property_count) {
            $tbl2 = $db->addTable('properties');

            $count_field = $tbl2->addField('id', 'property_count');
            $count_field->showCount();
            if ($this->must_have_property) {
                $property_conditional1 = $db->createConditional($tbl2->getField('active'),
                        1);
                $property_conditional2 = $db->createConditional($tbl2->getField('active'),
                        null, 'is');
                $property_conditional3 = $db->createConditional($property_conditional1,
                        $property_conditional2, 'or');
                $db->addConditional($property_conditional3);
                $tbl2->addHaving($count_field, 0, '>');
            }
        }
        if ($this->include_inquiry) {
            $tbl3 = $db->addTable('prop_inquiry');
            $groups[] = $tbl3->addField('inquiry_date');
            $groups[] = $tbl3->addField('inquiry_type');
        }

        // avoiding the password field
        $groups[] = $tbl->addField('id');
        $tbl->addField('username');
        $tbl->addField('phone');
        $tbl->addField('email_address');
        $tbl->addField('company_name');
        $tbl->addField('company_address');
        $tbl->addField('company_url');
        $tbl->addField('times_available');
        $tbl->addField('first_name');
        $tbl->addField('last_name');
{'fraction_non_alphanumeric': 0.1386696730552424, 'fraction_numerical': 0.00966339185053954, 'mean_word_length': 2.50451467268623, 'pattern_counts': {'":': 0, '<': 13, '<?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': '1764164', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1745, 'n_words': 430}