0009768: Use ModelConfig for Timetracker models
[tine20] / tine20 / Timetracker / Model / Timesheet.php
index 6da3932..7805ce9 100644 (file)
@@ -5,7 +5,7 @@
  * @package     Timetracker
  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
  * @author      Philipp Schuele <p.schuele@metaways.de>
- * @copyright   Copyright (c) 2007-2008 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2007-2016 Metaways Infosystems GmbH (http://www.metaways.de)
  * 
  */
 
 class Timetracker_Model_Timesheet extends Tinebase_Record_Abstract implements Sales_Model_Billable_Interface
 {
     /**
-     * key in $_validators/$_properties array for the filed which 
-     * represents the identifier
-     * 
-     * @var string
-     */
-    protected $_identifier = 'id';
-    
-    /**
-     * application the record belongs to
-     *
-     * @var string
-     */
-    protected $_application = 'Timetracker';
-    
-    /**
-     * list of zend validator
-     * 
-     * this validators get used when validating user generated content with Zend_Input_Filter
+     * holds the configuration object (must be declared in the concrete class)
      *
-     * @var array
+     * @var Tinebase_ModelConfiguration
      */
-    protected $_validators = array(
-        'id'                    => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-    // record fields
-        'account_id'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
-        'timeaccount_id'        => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
-        'start_date'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
-        'start_time'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'duration'              => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'description'           => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
-        'is_billable'           => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 1),
-        'is_billable_combined'  => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'billed_in'             => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'invoice_id'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'is_cleared'            => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
-        'is_cleared_combined'   => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-    // custom fields array
-        'customfields'          => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => array()),    
-    // modlog information
-        'created_by'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'creation_time'         => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'last_modified_by'      => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'last_modified_time'    => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'is_deleted'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'deleted_time'          => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'deleted_by'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'seq'                   => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-    // other related data
-        'relations'             => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => NULL),
-        'notes'                 => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        'tags'                  => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-        
-        'attachments'           => array(Zend_Filter_Input::ALLOW_EMPTY => true),
-    );
+    protected static $_configurationObject = NULL;
 
     /**
-     * name of fields containing datetime or an array of datetime information
-     *
-     * @var array list of datetime fields
-     */    
-    protected $_datetimeFields = array(
-        'creation_time',
-        'last_modified_time',
-        'deleted_time',
-    );
-    
-    /**
-     * name of fields containing time information
-     *
-     * @var array list of time fields
-     */
-    protected $_timeFields = array(
-        'start_time'
-    );
-    
-    /**
-     * if foreign Id fields should be resolved on search and get from json
-     * should have this format:
-     *     array('Calendar_Model_Contact' => 'contact_id', ...)
-     * or for more fields:
-     *     array('Calendar_Model_Contact' => array('contact_id', 'customer_id), ...)
-     * (e.g. resolves contact_id with the corresponding Model)
+     * Holds the model configuration (must be assigned in the concrete class)
      *
      * @var array
      */
-    protected static $_resolveForeignIdFields = array(
-        'Sales_Model_Invoice' => array('invoice_id'),
+    protected static $_modelConfiguration = array(
+        'recordName'        => 'Timesheet',
+        'recordsName'       => 'Timesheets', // ngettext('Timesheet', 'Timesheets', n)
+        'hasRelations'      => TRUE,
+        'hasCustomFields'   => TRUE,
+        'hasNotes'          => TRUE,
+        'hasTags'           => TRUE,
+        'modlogActive'      => TRUE,
+        'hasAttachments'    => TRUE,
+        'createModule'      => TRUE,
+        'containerProperty' => NULL,
+
+        'titleProperty'     => 'title',
+        'appName'           => 'Timetracker',
+        'modelName'         => 'Timesheet',
+        'multipleEdit'      => TRUE,
+        'splitButton'       => TRUE,
+
+        'defaultFilter' => 'start_date',
+
+        'fields'            => array(
+            'account_id'            => array(
+                'label'                 => 'Account', //_('Account')
+                'duplicateCheckGroup'   => 'account',
+                'type'                  => 'user',
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
+                // TODO add filter: 'inputFilters'          => array('Zend_Filter_Empty' => CURRENT USER ACCOUNT ID),
+            ),
+            'timeaccount_id'        => array(
+                'label'                 => 'Time Account (Number - Title)', //_('Time Account (Number - Title)')
+                'type'                  => 'record',
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
+                'config'                => array(
+                    'appName'               => 'Timetracker',
+                    'modelName'             => 'Timeaccount',
+                    'idProperty'            => 'id'
+                ),
+                // TODO ?????
+                //'default'               => array('account_grants' => array('bookOwnGrant' => true)),
+                'filterDefinition'      => array(
+                    'filter'                => 'Tinebase_Model_Filter_ForeignId',
+                    'options'               => array(
+                        'filtergroup'           => 'Timetracker_Model_TimeaccountFilter',
+                        'controller'            => 'Timetracker_Controller_Timeaccount',
+                        'useTimesheetAcl'       => TRUE,
+                        'showClosed'            => TRUE,
+                        'appName'               => 'Timetracker',
+                        'modelName'             => 'Timeaccount',
+                    ),
+                    'jsConfig'              => array('filtertype' => 'timetracker.timeaccount')
+                ),
+            ),
+            'is_billable'           => array(
+                'label'                 => NULL,
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 1),
+                'type'                  => 'boolean',
+                'default'               => 1,
+                'shy'                   => TRUE
+            ),
+            'is_billable_combined'  => array(
+                'label'                 => 'Billable', // _('Billable')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'type'                  => 'boolean',
+                'filterDefinition'      => array(
+                    'filter'                => 'Tinebase_Model_Filter_Bool',
+                    'title'                 => 'Billable', // _('Billable')
+                    'options'               => array(
+                        'leftOperand'           => '(timetracker_timesheet.is_billable*timetracker_timeaccount.is_billable)',
+                        'requiredCols'          => array('is_billable_combined')
+                    ),
+                ),
+            ),
+            'billed_in'             => array(
+                'label'                 => 'Cleared in', // _('Cleared in')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'copyOmit'              => TRUE,
+                'shy'                   => TRUE
+            ),
+            'invoice_id'            => array(
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'label'                 => 'Invoice', // _('Invoice')
+                'type'                  => 'record',
+                'inputFilters'          => array('Zend_Filter_Empty' => NULL),
+                'config'                => array(
+                    'appName'               => 'Sales',
+                    'modelName'             => 'Invoice',
+                    'idProperty'            => 'id'
+                ),
+            ),
+            'is_cleared'            => array(
+                'label'                 => NULL,
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
+                'type'                  => 'boolean',
+                'default'               => 0,
+                'copyOmit'              => TRUE,
+                'shy'                   => TRUE
+            ),
+            'is_cleared_combined'   => array(
+                'label'                 => 'Cleared', // _('Cleared')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'type'                  => 'boolean',
+                'filterDefinition'      => array(
+                    'filter'                => 'Tinebase_Model_Filter_Bool',
+                    'options'               => array(
+                        'leftOperand'           => "( (CASE WHEN timetracker_timesheet.is_cleared = '1' THEN 1 ELSE 0 END) | (CASE WHEN timetracker_timeaccount.status = 'billed' THEN 1 ELSE 0 END) )",
+                        'requiredCols'          => array('is_cleared_combined'),
+                    ),
+                ),
+            ),
+            'start_date'            => array(
+                'label'                 => 'Date', // _('Date')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
+                'type'                  => 'date',
+                'default'               => 'today',
+                // strip time information from datetime string
+                'inputFilters'          => array('Zend_Filter_PregReplace' => array('/(\d{4}-\d{2}-\d{2}).*/', '$1'))
+            ),
+            'start_time'            => array(
+                'label'                 => 'Start time', // _('Start time')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'inputFilters'          => array('Zend_Filter_Empty' => NULL),
+                'type'                  => 'time',
+                'shy'                   => TRUE
+            ),
+            'duration'              => array(
+                'label'                 => 'Duration', // _('Duration')
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
+                'type'                  => 'integer',
+                'specialType'           => 'minutes',
+                'default'               => '30'
+            ),
+            'description'           => array(
+                'label'                 => 'Description', // _('Description')
+                'type'                  => 'text',
+                'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
+                'queryFilter'           => TRUE
+            ),
+            // TODO ?????
+            /*
+            'timeaccount_closed' => array(
+                'label' => 'Time Account closed', // _('Time Account closed')
+                'validators' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'type' => 'boolean',
+
+            ),*/
+        )
     );
     
     /**
-     * overwrite constructor to add more filters
-     *
-     * @param mixed $_data
-     * @param bool $_bypassFilters
-     * @param mixed $_convertDates
-     * @return void
-     */
-    public function __construct($_data = NULL, $_bypassFilters = false, $_convertDates = true)
-    {
-        // strip time information from datetime string
-        $this->_filters['start_date'] = new Zend_Filter_PregReplace('/(\d{4}-\d{2}-\d{2}).*/', '$1');
-        // set start_time to NULL if not set
-        $this->_filters['start_time'] = new Zend_Filter_Empty(NULL);
-        
-        $this->_filters['invoice_id'] = new Zend_Filter_Empty(NULL);
-        
-        return parent::__construct($_data, $_bypassFilters, $_convertDates);
-    }
-    
-    /**
      * returns the interval of this billable
      *
      * @return array