Example #1
0
 /**
  * Prepares "parameters" argument for self::getList() according to the purposes of a REST interface
  * 
  * @param mixed[] Initial parameters
  * @param mixed[] Behaviour flags
  * 
  * 	<li> USER_ID integer Current user id, mandatory.
  * 	<li> ROW_LIMIT integer Row limit on each rest query, optional
  * 
  * @return mixed[]
  */
 public static function getList(array $parameters = array(), $behaviour = array())
 {
     if (!is_array($behaviour)) {
         $behaviour = array();
     }
     $behaviour['USER_ID'] = Assert::expectIntegerPositive($behaviour['USER_ID'], '$behaviour[USER_ID]');
     if (!isset($behaviour['ROW_LIMIT'])) {
         $behaviour['ROW_LIMIT'] = false;
     }
     $runtime = array();
     if (is_array($parameters['order']) && !empty($parameters['order'])) {
         static::parseOutSimpleAggregations(array_keys($parameters['order']), $runtime);
     }
     $parameters['filter']['=TASK.ZOMBIE'] = 'N';
     if (is_array($parameters['select']) && !empty($parameters['select'])) {
         static::parseOutSimpleAggregations($parameters['select'], $runtime);
     }
     $rights = TaskTable::getRuntimeFieldMixins(array('CHECK_RIGHTS'), array('USER_ID' => $behaviour['USER_ID'], 'REF_FIELD' => 'TASK_ID'));
     if (!empty($rights)) {
         $runtime['CHECK_RIGHTS'] = $rights['CHECK_RIGHTS'];
     }
     if (!empty($runtime)) {
         $parameters['runtime'] = $runtime;
     }
     $behaviour['ROW_LIMIT'] = intval($behaviour['ROW_LIMIT']);
     if ($behaviour['ROW_LIMIT'] && (!isset($parameters['limit']) || (int) $parameters['limit'] > $behaviour['ROW_LIMIT'])) {
         $parameters['limit'] = $behaviour['ROW_LIMIT'];
     }
     return parent::getList($parameters);
 }
 public static function beforeViewDataQuery(&$select, &$filter, &$group, &$order, &$limit, &$options, &$runtime)
 {
     parent::beforeViewDataQuery($select, $filter, $group, $order, $limit, $options, $runtime);
     global $USER, $DB, $DBType;
     $permFilter = array('LOGIC' => 'OR');
     // owner permission
     if (isset($_GET['select_my_tasks']) || !isset($_GET['select_my_tasks']) && !isset($_GET['select_depts_tasks']) && !isset($_GET['select_group_tasks'])) {
         $runtime['IS_TASK_COWORKER'] = array('data_type' => 'integer', 'expression' => array("(CASE WHEN EXISTS(" . "SELECT 'x' FROM b_tasks_member TM " . "WHERE TM.TASK_ID = " . $DB->escL . (ToUpper($DBType) === "ORACLE" ? "TASKS_TASK" : "tasks_task") . $DB->escR . ".ID AND TM.USER_ID = " . $USER->GetID() . " AND TM.TYPE = 'A'" . ") THEN 1 ELSE 0 END)"));
         $permFilter[] = array('LOGIC' => 'OR', '=RESPONSIBLE_ID' => $USER->GetID(), '=IS_TASK_COWORKER' => 1);
     }
     // own departments permission
     if (isset($_GET['select_depts_tasks'])) {
         $permFilterDepts = array('LOGIC' => 'OR', '=CREATED_BY' => $USER->GetID());
         $deptsPermSql = CTasks::GetSubordinateSql('__ULTRAUNIQUEPREFIX__');
         if (strlen($deptsPermSql)) {
             $deptsPermSql = "EXISTS(" . $deptsPermSql . ")";
             $deptsPermSql = str_replace('__ULTRAUNIQUEPREFIX__T.', $DB->escL . (ToUpper($DBType) === "ORACLE" ? "TASKS_TASK" : "tasks_task") . $DB->escR . '.', $deptsPermSql);
             $deptsPermSql = str_replace('__ULTRAUNIQUEPREFIX__', '', $deptsPermSql);
             $runtime['IS_SUBORDINATED_TASK'] = array('data_type' => 'integer', 'expression' => array("(CASE WHEN " . $deptsPermSql . " THEN 1 ELSE 0 END)"));
             $permFilterDepts[] = array('!RESPONSIBLE_ID' => $USER->GetID(), '=IS_SUBORDINATED_TASK' => 1);
         }
         $permFilter[] = $permFilterDepts;
     }
     // group permission
     if (isset($_GET['select_group_tasks'])) {
         $allowedGroups = CTasks::GetAllowedGroups();
         $permFilter[] = array('=GROUP_ID' => $allowedGroups);
     }
     // re-aggregate aggregated subquery in DURATION for mssql
     if (\Bitrix\Main\Application::getConnection() instanceof \Bitrix\Main\DB\MssqlConnection) {
         foreach ($select as $k => $v) {
             if (substr($k, -9) == '_DURATION') {
                 // we have aggregated duration
                 $subQuery = new \Bitrix\Main\Entity\Query(\Bitrix\Tasks\ElapsedTimeTable::getEntity());
                 $subQuery->addSelect('TASK_ID');
                 $subQuery->addSelect(new \Bitrix\Main\Entity\ExpressionField('DURATION', 'ROUND(SUM(%s)/60, 0)', 'SECONDS'));
                 $subEntity = \Bitrix\Main\Entity\Base::getInstanceByQuery($subQuery);
                 // make reference
                 $subReferenceName = $k . '_REF';
                 $runtime[$subReferenceName] = array('data_type' => $subEntity, 'reference' => array('=this.ID' => 'ref.TASK_ID'));
                 // rewrite aggregated duration (put it in the end, after refence)
                 $runtimeField = $runtime[$k];
                 unset($runtime[$k]);
                 $runtimeField['expression'][1] = $subReferenceName . '.DURATION';
                 $runtime[$k] = $runtimeField;
             } else {
                 if (substr($k, -20) == '_DURATION_FOR_PERIOD' && isset($options['SQL_TIME_INTERVAL'])) {
                     // we have aggregated DURATION_FOR_PERIOD field
                     $subQuery = new \Bitrix\Main\Entity\Query(\Bitrix\Tasks\ElapsedTimeTable::getEntity());
                     $subQuery->addSelect('TASK_ID');
                     $subQuery->addSelect(new \Bitrix\Main\Entity\ExpressionField('DURATION_FOR_PERIOD', 'ROUND((SUM(CASE WHEN CREATED_DATE ' . $options['SQL_TIME_INTERVAL'] . ' THEN %s ELSE 0 END)/60),0)', 'SECONDS'));
                     $subEntity = \Bitrix\Main\Entity\Base::getInstanceByQuery($subQuery);
                     // make reference
                     $subReferenceName = $k . '_REF';
                     $runtime[$subReferenceName] = array('data_type' => $subEntity, 'reference' => array('=this.ID' => 'ref.TASK_ID'));
                     // rewrite aggregated duration (put it in the end, after refence)
                     $runtimeField = $runtime[$k];
                     unset($runtime[$k]);
                     $runtimeField['expression'][1] = $subReferenceName . '.DURATION_FOR_PERIOD';
                     $runtime[$k] = $runtimeField;
                 }
             }
         }
     }
     // concat permissions with common filter
     $filter[] = $permFilter;
 }