<commit_before><?php
/**
 * conjoon
 * (c) 2002-2009 siteartwork.de/conjoon.org
 * <EMAIL>
 *
 * $Author$
 * $Id$
 * $Date$
 * $Revision$
 * $LastChangedDate$
 * $LastChangedBy$
 * $URL$
 */

/**
 * @see Zend_Db_Table
 */
require_once 'Zend/Db/Table/Abstract.php';

/**
 * @see Conjoon_BeanContext_Decoratable
 */
require_once 'Conjoon/BeanContext/Decoratable.php';

/**
 * Table data gateway. Models the table <tt>groupware_email_accounts</tt>.
 *
 * @uses Zend_Db_Table
 * @package Conjoon_Groupware_Email
 * @subpackage Model
 * @category Model
 *
 * @author <NAME> <<EMAIL>>
 */
class Conjoon_Modules_Groupware_Email_Account_Model_Account
    extends Zend_Db_Table_Abstract implements Conjoon_BeanContext_Decoratable{

    /**
     * The name of the table in the underlying datastore this
     * class represents.
     * @var string
     */
    protected $_name = 'groupware_email_accounts';

    /**
     * The name of the column that denotes the primary key for this table
     * @var string
     */
    protected $_primary = 'id';

    /**
     * Returns the id of the standard account for this user. If no standard
     * account could be found, any other account id will be returned.
     * If no account was found, 0 will be returned.
     *
     * @param int $id The id of the user to get the standard email account for
     *
     * @return integer
     */
    public function getStandardAccountIdForUser($userId)
    {
        $userId = (int)$userId;

        if ($userId <= 0) {
            return 0;
        }

        $row = $this->fetchRow(
            $this->select()
                ->where('user_id=?', $userId)
                ->where('is_deleted=?', 0)
                ->where('is_standard=?', 1)
                // in case there are accidently more than 1 standard accounts
                // configured
                ->limit(1, 0)
        );

        if (!$row) {
            $row = $this->fetchRow(
                $this->select()
                    ->where('user_id=?', $userId)
                    ->where('is_deleted=?', 0)
                    ->limit(1, 0)
            );
        }

        if (!$row) {
            return 0;
        }

        return $row->id;
    }

    /**
     * Updates the is_deleted field in the table to "1" to indicate that this account
     * was deleted and should not be used anymore.
     *
     * @return integer The number of accounts deleted.
     */
    public function deleteAccount($id)
    {
        $id = (int)$id;

        if ($id <= 0) {
            return 0;
        }

        $where    = $this->getAdapter()->quoteInto('id = ?', $id, 'INTEGER');
        $affected = $this->update(array(
            'is_standard' => 0,
            'is_deleted'  => 1
        ), $where);

        return $affected;
    }

    /**
     * Adds an account for the specified user with the specified data.
     *
     * @param integer $userId The userid this account will belong to
     * @param array $data an assoc array with keys as fields and values as data
     * to add
     *
     * Will also take the default folder of the user and copy them so emails
     * for this account will also be stored in this folders.
     *
     * @return integer 0 if the data wasn't added, otherwise the id of the newly added
     * row
     */
    public function addAccount($userId, Array $data)
    {
        $userId = (int)$userId;

        if ($userId <= 0) {
            return 0;
        }

        $whiteList = array(
            'name',
            'address',
            'server_inbox',
            'server_outbox',
            'username_inbox',
            'username_outbox',
            'user_name',
            'is_outbox_auth',
            'password_inbox',
            'password_outbox'
        );

        $addData = array();

        foreach ($data as $key => $value) {
            if (in_array($key, $whiteList)) {
                $addData[$key] = $value;
            }
        }

        if (empty($addData)) {
            return 0;
        }

        $addData['user_id'] = $userId;

        $id = $this->insert($addData);

        if ((int)$id == 0) {
            return $id;
        }

        /**
         * @see Conjoon_Modules_Groupware_Email_Folder_Model_Folder
         */
        require_once 'Conjoon/Modules/Groupware/Email/Folder/Model/Folder.php';

        $folderModel = new Conjoon_Modules_Groupware_Email_Folder_Model_Folder();
        $folderIds   = $folderModel->getFoldersForAccountsRoot($userId);

        if (empty($folderIds)) {
            // user creates his very first email account.
            // create base folder hierarchy and map them to the
            // account later on<commit_msg> - fixed: adding accounts would not check if the current added account should be flagged with "isStandard=1", thus leaving the account storage without any account configured as standard<commit_after><?php
/**
 * conjoon
 * (c) 2002-2009 siteartwork.de/conjoon.org
 * <EMAIL>
 *
 * $Author$
 * $Id$
 * $Date$
 * $Revision$
 * $LastChangedDate$
 * $LastChangedBy$
 * $URL$
 */

/**
 * @see Zend_Db_Table
 */
require_once 'Zend/Db/Table/Abstract.php';

/**
 * @see Conjoon_BeanContext_Decoratable
 */
require_once 'Conjoon/BeanContext/Decoratable.php';

/**
 * Table data gateway. Models the table <tt>groupware_email_accounts</tt>.
 *
 * @uses Zend_Db_Table
 * @package Conjoon_Groupware_Email
 * @subpackage Model
 * @category Model
 *
 * @author <NAME> <<EMAIL>>
 */
class Conjoon_Modules_Groupware_Email_Account_Model_Account
    extends Zend_Db_Table_Abstract implements Conjoon_BeanContext_Decoratable{

    /**
     * The name of the table in the underlying datastore this
     * class represents.
     * @var string
     */
    protected $_name = 'groupware_email_accounts';

    /**
     * The name of the column that denotes the primary key for this table
     * @var string
     */
    protected $_primary = 'id';

    /**
     * Returns the id of the standard account for this user. If no standard
     * account could be found, any other account id will be returned.
     * If no account was found, 0 will be returned.
     *
     * @param int $id The id of the user to get the standard email account for
     *
     * @return integer
     */
    public function getStandardAccountIdForUser($userId)
    {
        $userId = (int)$userId;

        if ($userId <= 0) {
            return 0;
        }

        $row = $this->fetchRow(
            $this->select()
                ->where('user_id=?', $userId)
                ->where('is_deleted=?', 0)
                ->where('is_standard=?', 1)
                // in case there are accidently more than 1 standard accounts
                // configured
                ->limit(1, 0)
        );

        if (!$row) {
            $row = $this->fetchRow(
                $this->select()
                    ->where('user_id=?', $userId)
                    ->where('is_deleted=?', 0)
                    ->limit(1, 0)
            );
        }

        if (!$row) {
            return 0;
        }

        return $row->id;
    }

    /**
     * Updates the is_deleted field in the table to "1" to indicate that this account
     * was deleted and should not be used anymore.
     *
     * @return integer The number of accounts deleted.
     */
    public function deleteAccount($id)
    {
        $id = (int)$id;

        if ($id <= 0) {
            return 0;
        }

        $where    = $this->getAdapter()->quoteInto('id = ?', $id, 'INTEGER');
        $affected = $this->update(array(
            'is_standard' => 0,
            'is_deleted'  => 1
        ), $where);

        return $affected;
    }

    /**
     * Adds an account for the specified user with the specified data.
     *
     * @param integer $userId The userid this account will belong to
     * @param array $data an assoc array with keys as fields and values as data
     * to add
     *
     * Will also take the default folder of the user and copy them so emails
     * for this account will also be stored in this folders.
     *
     * @return integer 0 if the data wasn't added, otherwise the id of the newly added
     * row
     */
    public function addAccount($userId, Array $data)
    {
        $userId = (int)$userId;

        if ($userId <= 0) {
            return 0;
        }

        $whiteList = array(
            'name',
            'address',
            'server_inbox',
            'server_outbox',
            'username_inbox',
            'username_outbox',
            'user_name',
            'is_outbox_auth',
            'password_inbox',
            'password_outbox'
        );

        $addData = array();

        foreach ($data as $key => $value) {
            if (in_array($key, $whiteList)) {
                $addData[$key] = $value;
            }
        }

        if (empty($addData)) {
            return 0;
        }

        // check here if there is currently a standard account
        // configured for the user - if none could be found, set this
        // account as standard
        $standardId = $this->getStandardAccountIdForUser($userId);

        if ($standardId == 0) {
            $addData['is_standard'] = 1;
        }

        $addData['user_id'] = $userId;

        $id = $this->insert($addData);

        if ((int)$id == 0) {
            return $id;
        }

        /**
         * @see Conjoon_Modules_Groupware_Email_Folder_Model_Folder
         */
        require_once 'Conjoon/Modules/Groupware/Email/Folder/Model/Folder.php';

        $folderModel = new Conjoon_Modules_Groupware_Email_Folder_Model_Folder();
        $folderIds   = $folderModel->getFoldersForAccountsRoot($userId);

        if (empty($folderIds)) {
            // user creates his very first email account.
            // create base folder hierarchy and map them to the
            // account later on
{'fraction_non_alphanumeric': 0.11333604391136409, 'fraction_numerical': 0.006403740597682455, 'mean_word_length': 1.81516452074392, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 56, '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': '20675', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2839, 'n_words': 830}