Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like calendar_so often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use calendar_so, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 97 | class calendar_so |
||
| 98 | { |
||
| 99 | /** |
||
| 100 | * name of the main calendar table and prefix for all other calendar tables |
||
| 101 | */ |
||
| 102 | var $cal_table = 'egw_cal'; |
||
| 103 | var $extra_table,$repeats_table,$user_table,$dates_table,$all_tables; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * reference to global db-object |
||
| 107 | * |
||
| 108 | * @var Api\Db |
||
| 109 | */ |
||
| 110 | var $db; |
||
| 111 | /** |
||
| 112 | * instance of the async object |
||
| 113 | * |
||
| 114 | * @var Api\Asyncservice |
||
| 115 | */ |
||
| 116 | var $async; |
||
| 117 | /** |
||
| 118 | * SQL to sort by status U, T, A, R |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | const STATUS_SORT = "CASE cal_status WHEN 'U' THEN 1 WHEN 'T' THEN 2 WHEN 'A' THEN 3 WHEN 'R' THEN 4 ELSE 0 END ASC"; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Cached timezone data |
||
| 125 | * |
||
| 126 | * @var array id => data |
||
| 127 | */ |
||
| 128 | protected static $tz_cache = array(); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Constructor of the socal class |
||
| 132 | */ |
||
| 133 | function __construct() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return sql to fetch all events in a given timerange, to be used instead of full table in further sql queries |
||
| 148 | * |
||
| 149 | * @param int $start |
||
| 150 | * @param int $end |
||
| 151 | * @param array $_where =null |
||
| 152 | * @param boolean $deleted =false |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | protected function cal_range_view($start, $end, array $_where=null, $deleted=false) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return sql to fetch all dates in a given timerange, to be used instead of full dates table in further sql queries |
||
| 175 | * |
||
| 176 | * Currently NOT used, as using two views joined together appears slower in my tests (probably because no index) then |
||
| 177 | * joining cal_range_view with real dates table (with index). |
||
| 178 | * |
||
| 179 | * @param int $start |
||
| 180 | * @param int $end |
||
| 181 | * @param array $_where =null |
||
| 182 | * @param boolean $deleted =false |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | protected function dates_range_view($start, $end, array $_where=null, $deleted=false) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return events in a given timespan containing given participants (similar to search but quicker) |
||
| 208 | * |
||
| 209 | * Not all search parameters are currently supported!!! |
||
| 210 | * |
||
| 211 | * @param int $start startdate of the search/list (servertime) |
||
| 212 | * @param int $end enddate of the search/list (servertime) |
||
| 213 | * @param int|array $users user-id or array of user-id's, !$users means all entries regardless of users |
||
| 214 | * @param int|array $cat_id =0 mixed category-id or array of cat-id's (incl. all sub-categories), default 0 = all |
||
| 215 | * @param string $filter ='default' string filter-name: all (not rejected), accepted, unknown, tentative, rejected or everything (incl. rejected, deleted) |
||
| 216 | * @param int|boolean $offset =False offset for a limited query or False (default) |
||
| 217 | * @param int $num_rows =0 number of rows to return if offset set, default 0 = use default in user prefs |
||
| 218 | * @param array $params =array() |
||
| 219 | * @param string|array $params['query'] string: pattern so search for, if unset or empty all matching entries are returned (no search) |
||
| 220 | * Please Note: a search never returns repeating events more then once AND does not honor start+end date !!! |
||
| 221 | * array: everything is directly used as $where |
||
| 222 | * @param string $params['order'] ='cal_start' column-names plus optional DESC|ASC separted by comma |
||
| 223 | * @param string $params['sql_filter'] sql to be and'ed into query (fully quoted) |
||
| 224 | * @param string|array $params['cols'] what to select, default "$this->repeats_table.*,$this->cal_table.*,cal_start,cal_end,cal_recur_date", |
||
| 225 | * if specified and not false an iterator for the rows is returned |
||
| 226 | * @param string $params['append'] SQL to append to the query before $order, eg. for a GROUP BY clause |
||
| 227 | * @param array $params['cfs'] custom fields to query, null = none, array() = all, or array with cfs names |
||
| 228 | * @param array $params['users'] raw parameter as passed to calendar_bo::search() no memberships resolved! |
||
| 229 | * @param boolean $params['master_only'] =false, true only take into account participants/status from master (for AS) |
||
| 230 | * @param boolean $params['enum_recuring'] =true enumerate recuring events |
||
| 231 | * @param int $remove_rejected_by_user =null add join to remove entry, if given user has rejected it |
||
| 232 | * @return array of events |
||
| 233 | */ |
||
| 234 | function &events($start,$end,$users,$cat_id=0,$filter='all',$offset=False,$num_rows=0,array $params=array(),$remove_rejected_by_user=null) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * reads one or more calendar entries |
||
| 305 | * |
||
| 306 | * All times (start, end and modified) are returned as timesstamps in servertime! |
||
| 307 | * |
||
| 308 | * @param int|array|string $ids id or array of id's of the entries to read, or string with a single uid |
||
| 309 | * @param int $recur_date =0 if set read the next recurrence at or after the timestamp, default 0 = read the initital one |
||
| 310 | * @return array|boolean array with cal_id => event array pairs or false if entry not found |
||
| 311 | */ |
||
| 312 | function read($ids,$recur_date=0) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get full event information from an iterator of a select on egw_cal |
||
| 363 | * |
||
| 364 | * @param array|Iterator $rs |
||
| 365 | * @param int $recur_date =0 |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | protected function &get_events($rs, $recur_date=0) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Maximum time a ctag get cached, as ActiveSync ping requests can run for a long time |
||
| 504 | */ |
||
| 505 | const MAX_CTAG_CACHE_TIME = 29; |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get maximum modification time of events for given participants and optional owned by them |
||
| 509 | * |
||
| 510 | * This includes ALL recurences of an event series |
||
| 511 | * |
||
| 512 | * @param int|string|array $users one or mulitple calendar users |
||
| 513 | * @param booelan $owner_too =false if true return also events owned by given users |
||
| 514 | * @param boolean $master_only =false only check recurance master (egw_cal_user.recur_date=0) |
||
| 515 | * @return int maximum modification timestamp |
||
| 516 | */ |
||
| 517 | function get_ctag($users, $owner_too=false,$master_only=false) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Query calendar main table and return iterator of query |
||
| 573 | * |
||
| 574 | * Use as: foreach(get_cal_data() as $data) { $data = Api\Db::strip_array_keys($data, 'cal_'); // do something with $data |
||
| 575 | * |
||
| 576 | * @param array $query filter, keys have to use 'cal_' prefix |
||
| 577 | * @param string|array $cols ='cal_id,cal_reference,cal_etag,cal_modified,cal_user_modified' cols to query |
||
| 578 | * @return Iterator as Api\Db::select |
||
| 579 | */ |
||
| 580 | function get_cal_data(array $query, $cols='cal_id,cal_reference,cal_etag,cal_modified,cal_user_modified') |
||
| 593 | |||
| 594 | /** |
||
| 595 | * generate SQL to filter after a given category (incl. subcategories) |
||
| 596 | * |
||
| 597 | * @param array|int $cat_id cat-id or array of cat-ids, or !$cat_id for none |
||
| 598 | * @return string SQL to include in the query |
||
| 599 | */ |
||
| 600 | function cat_filter($cat_id) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Return filters to filter by given status |
||
| 620 | * |
||
| 621 | * @param string $filter "default", "all", ... |
||
| 622 | * @param boolean $enum_recuring are recuring events enumerated or not |
||
| 623 | * @param array $where =array() array to add filters too |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | protected function status_filter($filter, $enum_recuring=true, array $where=array()) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Searches / lists calendar entries, including repeating ones |
||
| 681 | * |
||
| 682 | * @param int $start startdate of the search/list (servertime) |
||
| 683 | * @param int $end enddate of the search/list (servertime) |
||
| 684 | * @param int|array $users user-id or array of user-id's, !$users means all entries regardless of users |
||
| 685 | * @param int|array $cat_id =0 mixed category-id or array of cat-id's (incl. all sub-categories), default 0 = all |
||
| 686 | * @param string $filter ='all' string filter-name: all (not rejected), accepted, unknown, tentative, rejected or everything (incl. rejected, deleted) |
||
| 687 | * @param int|boolean $offset =False offset for a limited query or False (default) |
||
| 688 | * @param int $num_rows =0 number of rows to return if offset set, default 0 = use default in user prefs |
||
| 689 | * @param array $params =array() |
||
| 690 | * @param string|array $params['query'] string: pattern so search for, if unset or empty all matching entries are returned (no search) |
||
| 691 | * Please Note: a search never returns repeating events more then once AND does not honor start+end date !!! |
||
| 692 | * array: everything is directly used as $where |
||
| 693 | * @param string $params['order'] ='cal_start' column-names plus optional DESC|ASC separted by comma |
||
| 694 | * @param string|array $params['sql_filter'] sql to be and'ed into query (fully quoted), or usual filter array |
||
| 695 | * @param string|array $params['cols'] what to select, default "$this->repeats_table.*,$this->cal_table.*,cal_start,cal_end,cal_recur_date", |
||
| 696 | * if specified and not false an iterator for the rows is returned |
||
| 697 | * @param string $params['append'] SQL to append to the query before $order, eg. for a GROUP BY clause |
||
| 698 | * @param array $params['cfs'] custom fields to query, null = none, array() = all, or array with cfs names |
||
| 699 | * @param array $params['users'] raw parameter as passed to calendar_bo::search() no memberships resolved! |
||
| 700 | * @param boolean $params['master_only'] =false, true only take into account participants/status from master (for AS) |
||
| 701 | * @param boolean $params['enum_recuring'] =true enumerate recuring events |
||
| 702 | * @param boolean $params['use_so_events'] =false, true return result of new $this->events() |
||
| 703 | * @param int $remove_rejected_by_user =null add join to remove entry, if given user has rejected it |
||
| 704 | * @return Iterator|array of events |
||
| 705 | */ |
||
| 706 | function &search($start,$end,$users,$cat_id=0,$filter='all',$offset=False,$num_rows=0,array $params=array(),$remove_rejected_by_user=null) |
||
| 707 | { |
||
| 708 | //error_log(__METHOD__.'('.($start ? date('Y-m-d H:i',$start) : '').','.($end ? date('Y-m-d H:i',$end) : '').','.array2string($users).','.array2string($cat_id).",'$filter',".array2string($offset).",$num_rows,".array2string($params).') '.function_backtrace()); |
||
| 709 | |||
| 710 | /* not using new events method currently, as it not yet fully working and |
||
| 711 | using time-range views in old code gives simmilar improvments |
||
| 712 | // uncomment to use new events method for supported parameters |
||
| 713 | //if (!isset($params['use_so_events'])) $params['use_so_events'] = $params['use_so_events'] || $start && $end && !in_array($filter, array('owner', 'deleted')) && $params['enum_recuring']!==false; |
||
| 714 | |||
| 715 | // use new events method only if explicit requested |
||
| 716 | if ($params['use_so_events']) |
||
| 717 | { |
||
| 718 | return call_user_func_array(array($this,'events'), func_get_args()); |
||
| 719 | } |
||
| 720 | */ |
||
| 721 | if (isset($params['cols'])) |
||
| 722 | { |
||
| 723 | $cols = $params['cols']; |
||
| 724 | } |
||
| 725 | else |
||
| 726 | { |
||
| 727 | $all_cols = self::get_columns('calendar', $this->cal_table); |
||
| 728 | $all_cols[0] = $this->db->to_varchar($this->cal_table.'.cal_id'); |
||
| 729 | $cols = "$this->repeats_table.recur_type,$this->repeats_table.recur_interval,$this->repeats_table.recur_data,range_end AS recur_enddate,".implode(',',$all_cols).",cal_start,cal_end,$this->user_table.cal_recur_date"; |
||
| 730 | } |
||
| 731 | $where = array(); |
||
| 732 | if (is_array($params['query'])) |
||
| 733 | { |
||
| 734 | $where = $params['query']; |
||
| 735 | } |
||
| 736 | elseif ($params['query']) |
||
| 737 | { |
||
| 738 | if(is_numeric($params['query'])) |
||
| 739 | { |
||
| 740 | $where[] = $this->cal_table.'.cal_id = ' . (int)$params['query']; |
||
| 741 | } |
||
| 742 | else |
||
| 743 | { |
||
| 744 | foreach(array('cal_title','cal_description','cal_location') as $col) |
||
| 745 | { |
||
| 746 | $to_or[] = $col.' '.$this->db->capabilities[Api\Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote('%'.$params['query'].'%'); |
||
| 747 | } |
||
| 748 | $where[] = '('.implode(' OR ',$to_or).')'; |
||
| 749 | } |
||
| 750 | |||
| 751 | // Searching - restrict private to own or private grant |
||
| 752 | if (!isset($params['private_grants'])) |
||
| 753 | { |
||
| 754 | $params['private_grants'] = $GLOBALS['egw']->acl->get_ids_for_location($GLOBALS['egw_info']['user']['account_id'], Acl::PRIVAT, 'calendar'); |
||
| 755 | $params['private_grants'][] = $GLOBALS['egw_info']['user']['account_id']; // db query does NOT return current user |
||
| 756 | } |
||
| 757 | $private_filter = '(cal_public=1 OR cal_public=0 AND '.$this->db->expression($this->cal_table, array('cal_owner' => $params['private_grants'])) . ')'; |
||
| 758 | $where[] = $private_filter; |
||
| 759 | } |
||
| 760 | if (!empty($params['sql_filter'])) |
||
| 761 | { |
||
| 762 | if (is_string($params['sql_filter'])) |
||
| 763 | { |
||
| 764 | $where[] = $params['sql_filter']; |
||
| 765 | } |
||
| 766 | elseif(is_array($params['sql_filter'])) |
||
| 767 | { |
||
| 768 | $where = array_merge($where, $params['sql_filter']); |
||
| 769 | } |
||
| 770 | } |
||
| 771 | $useUnionQuery = $this->db->capabilities['distinct_on_text'] && $this->db->capabilities['union']; |
||
| 772 | if ($users) |
||
| 773 | { |
||
| 774 | $users_by_type = array(); |
||
| 775 | foreach((array)$users as $user) |
||
| 776 | { |
||
| 777 | if (is_numeric($user)) |
||
| 778 | { |
||
| 779 | $users_by_type['u'][] = (int) $user; |
||
| 780 | } |
||
| 781 | else |
||
| 782 | { |
||
| 783 | $user_type = $user_id = null; |
||
| 784 | self::split_user($user, $user_type, $user_id, true); |
||
| 785 | $users_by_type[$user_type][] = $user_id; |
||
| 786 | } |
||
| 787 | } |
||
| 788 | $to_or = $user_or = array(); |
||
| 789 | $owner_or = null; |
||
| 790 | $table_def = $this->db->get_table_definitions('calendar',$this->user_table); |
||
| 791 | foreach($users_by_type as $type => $ids) |
||
| 792 | { |
||
| 793 | // when we are able to use Union Querys, we do not OR our query, we save the needed parts for later construction of the union |
||
| 794 | if ($useUnionQuery) |
||
| 795 | { |
||
| 796 | $user_or[] = $this->db->expression($table_def,$this->user_table.'.',array( |
||
| 797 | 'cal_user_type' => $type, |
||
| 798 | ),' AND '.$this->user_table.'.',array( |
||
| 799 | 'cal_user_id' => $ids, |
||
| 800 | )); |
||
| 801 | if ($type == 'u' && $filter == 'owner') |
||
| 802 | { |
||
| 803 | $cal_table_def = $this->db->get_table_definitions('calendar',$this->cal_table); |
||
| 804 | // only users can be owners, no need to add groups |
||
| 805 | $user_ids = array(); |
||
| 806 | foreach($ids as $user_id) |
||
| 807 | { |
||
| 808 | if ($GLOBALS['egw']->accounts->get_type($user_id) === 'u') $user_ids[] = $user_id; |
||
| 809 | } |
||
| 810 | $owner_or = $this->db->expression($cal_table_def,array('cal_owner' => $user_ids)); |
||
| 811 | } |
||
| 812 | } |
||
| 813 | else |
||
| 814 | { |
||
| 815 | $to_or[] = $this->db->expression($table_def,$this->user_table.'.',array( |
||
| 816 | 'cal_user_type' => $type, |
||
| 817 | ),' AND '.$this->user_table.'.',array( |
||
| 818 | 'cal_user_id' => $ids, |
||
| 819 | )); |
||
| 820 | if ($type == 'u' && $filter == 'owner') |
||
| 821 | { |
||
| 822 | $cal_table_def = $this->db->get_table_definitions('calendar',$this->cal_table); |
||
| 823 | $to_or[] = $this->db->expression($cal_table_def,array('cal_owner' => $ids)); |
||
| 824 | } |
||
| 825 | } |
||
| 826 | } |
||
| 827 | // this is only used, when we cannot use UNIONS |
||
| 828 | if (!$useUnionQuery) $where[] = '('.implode(' OR ',$to_or).')'; |
||
| 829 | |||
| 830 | $where = $this->status_filter($filter, $params['enum_recuring'], $where); |
||
| 831 | } |
||
| 832 | if ($cat_id) |
||
| 833 | { |
||
| 834 | $where[] = $this->cat_filter($cat_id); |
||
| 835 | } |
||
| 836 | if ($start) |
||
| 837 | { |
||
| 838 | if ($params['enum_recuring']) |
||
| 839 | { |
||
| 840 | $where[] = (int)$start.' < cal_end'; |
||
| 841 | } |
||
| 842 | else |
||
| 843 | { |
||
| 844 | $where[] = '('.((int)$start).' < range_end OR range_end IS NULL)'; |
||
| 845 | } |
||
| 846 | } |
||
| 847 | if (!preg_match('/^[a-z_ ,c]+$/i',$params['order'])) $params['order'] = 'cal_start'; // gard against SQL injection |
||
| 848 | |||
| 849 | // if not enum recuring events, we have to use minimum start- AND end-dates, otherwise we get more then one event per cal_id! |
||
| 850 | if (!$params['enum_recuring']) |
||
| 851 | { |
||
| 852 | $where[] = "$this->user_table.cal_recur_date=0"; |
||
| 853 | $cols = str_replace(array('cal_start','cal_end'),array('range_start AS cal_start','(SELECT MIN(cal_end) FROM egw_cal_dates WHERE egw_cal.cal_id=egw_cal_dates.cal_id) AS cal_end'),$cols); |
||
| 854 | // in case cal_start is used in a query, eg. calendar_ical::find_event |
||
| 855 | $where = str_replace(array('cal_start','cal_end'), array('range_start','(SELECT MIN(cal_end) FROM egw_cal_dates WHERE egw_cal.cal_id=egw_cal_dates.cal_id)'), $where); |
||
| 856 | $params['order'] = str_replace('cal_start', 'range_start', $params['order']); |
||
| 857 | if ($end) $where[] = (int)$end.' > range_start'; |
||
| 858 | } |
||
| 859 | elseif ($end) $where[] = (int)$end.' > cal_start'; |
||
| 860 | |||
| 861 | if ($remove_rejected_by_user && $filter != 'everything') |
||
| 862 | { |
||
| 863 | $rejected_by_user_join = "LEFT JOIN $this->user_table rejected_by_user". |
||
| 864 | " ON $this->cal_table.cal_id=rejected_by_user.cal_id". |
||
| 865 | " AND rejected_by_user.cal_user_type='u'". |
||
| 866 | " AND rejected_by_user.cal_user_id=".$this->db->quote($remove_rejected_by_user). |
||
| 867 | " AND ".(!$params['enum_recuring'] ? 'rejected_by_user.cal_recur_date=0' : |
||
| 868 | '(recur_type IS NULL AND rejected_by_user.cal_recur_date=0 OR cal_start=rejected_by_user.cal_recur_date)'); |
||
| 869 | $or_required = array( |
||
| 870 | 'rejected_by_user.cal_status IS NULL', |
||
| 871 | "rejected_by_user.cal_status NOT IN ('R','X')", |
||
| 872 | ); |
||
| 873 | if ($filter == 'owner') $or_required[] = 'cal_owner='.(int)$remove_rejected_by_user; |
||
| 874 | $where[] = '('.implode(' OR ',$or_required).')'; |
||
| 875 | } |
||
| 876 | // using a time-range and deleted attribute limited view instead of full table |
||
| 877 | $cal_table = $this->cal_range_view($start, $end, null, $filter == 'everything' ? null : $filter != 'deleted'); |
||
| 878 | $cal_table_def = $this->db->get_table_definitions('calendar', $this->cal_table); |
||
| 879 | |||
| 880 | $join = "JOIN $this->user_table ON $this->cal_table.cal_id=$this->user_table.cal_id ". |
||
| 881 | "LEFT JOIN $this->repeats_table ON $this->cal_table.cal_id=$this->repeats_table.cal_id ". |
||
| 882 | $rejected_by_user_join; |
||
| 883 | // dates table join only needed to enum recuring events, we use a time-range limited view here too |
||
| 884 | if ($params['enum_recuring']) |
||
| 885 | { |
||
| 886 | $join = "JOIN ".$this->dates_table. // using dates_table direct seems quicker then an other view |
||
| 887 | //$this->dates_range_view($start, $end, null, $filter == 'everything' ? null : $filter == 'deleted'). |
||
| 888 | " ON $this->cal_table.cal_id=$this->dates_table.cal_id ".$join; |
||
| 889 | } |
||
| 890 | |||
| 891 | // Check for some special sorting, used by planner views |
||
| 892 | if($params['order'] == 'participants , cal_non_blocking DESC') |
||
| 893 | { |
||
| 894 | $order = ($GLOBALS['egw_info']['user']['preferences']['common']['account_display'] == 'lastname' ? 'n_family' : 'n_fileas'); |
||
| 895 | $cols .= ",egw_addressbook.{$order}"; |
||
| 896 | $join .= "LEFT JOIN egw_addressbook ON ". |
||
| 897 | ($this->db->Type == 'pgsql'? "egw_addressbook.account_id::varchar = ":"egw_addressbook.account_id = "). |
||
| 898 | "{$this->user_table}.cal_user_id"; |
||
| 899 | $params['order'] = "$order, cal_non_blocking DESC"; |
||
| 900 | } |
||
| 901 | else if ($params['order'] == 'categories , cal_non_blocking DESC') |
||
| 902 | { |
||
| 903 | $params['order'] = 'cat_name, cal_non_blocking DESC'; |
||
| 904 | $cols .= ',egw_categories.cat_name'; |
||
| 905 | $join .= "LEFT JOIN egw_categories ON egw_categories.cat_id = {$this->cal_table}.cal_category"; |
||
| 906 | } |
||
| 907 | |||
| 908 | //$starttime = microtime(true); |
||
| 909 | if ($useUnionQuery) |
||
| 910 | { |
||
| 911 | // allow apps to supply participants and/or icons |
||
| 912 | if (!isset($params['cols'])) $cols .= ',NULL AS participants,NULL AS icons'; |
||
| 913 | |||
| 914 | // changed the original OR in the query into a union, to speed up the query execution under MySQL 5 |
||
| 915 | // with time-range views benefit is now at best slim for huge tables or none at all! |
||
| 916 | $select = array( |
||
| 917 | 'table' => $cal_table, |
||
| 918 | 'join' => $join, |
||
| 919 | 'cols' => $cols, |
||
| 920 | 'where' => $where, |
||
| 921 | 'app' => 'calendar', |
||
| 922 | 'append'=> $params['append'], |
||
| 923 | 'table_def' => $cal_table_def, |
||
| 924 | ); |
||
| 925 | $selects = array(); |
||
| 926 | // we check if there are parts to use for the construction of our UNION query, |
||
| 927 | // as replace the OR by construction of a suitable UNION for performance reasons |
||
| 928 | if ($owner_or || $user_or) |
||
| 929 | { |
||
| 930 | foreach($user_or as $user_sql) |
||
| 931 | { |
||
| 932 | $selects[] = $select; |
||
| 933 | $selects[count($selects)-1]['where'][] = $user_sql; |
||
| 934 | View Code Duplication | if ($params['enum_recuring']) |
|
| 935 | { |
||
| 936 | $selects[count($selects)-1]['where'][] = "recur_type IS NULL AND $this->user_table.cal_recur_date=0"; |
||
| 937 | $selects[] = $select; |
||
| 938 | $selects[count($selects)-1]['where'][] = $user_sql; |
||
| 939 | $selects[count($selects)-1]['where'][] = "$this->user_table.cal_recur_date=cal_start"; |
||
| 940 | } |
||
| 941 | } |
||
| 942 | // if the query is to be filtered by owner we need to add more selects for the union |
||
| 943 | if ($owner_or) |
||
| 944 | { |
||
| 945 | $selects[] = $select; |
||
| 946 | $selects[count($selects)-1]['where'][] = $owner_or; |
||
| 947 | View Code Duplication | if ($params['enum_recuring']) |
|
| 948 | { |
||
| 949 | $selects[count($selects)-1]['where'][] = "recur_type IS NULL AND $this->user_table.cal_recur_date=0"; |
||
| 950 | $selects[] = $select; |
||
| 951 | $selects[count($selects)-1]['where'][] = $owner_or; |
||
| 952 | $selects[count($selects)-1]['where'][] = "$this->user_table.cal_recur_date=cal_start"; |
||
| 953 | } |
||
| 954 | } |
||
| 955 | } |
||
| 956 | else |
||
| 957 | { |
||
| 958 | // if the query is to be filtered by neither by user nor owner (should not happen?) we need 2 selects for the union |
||
| 959 | $selects[] = $select; |
||
| 960 | if ($params['enum_recuring']) |
||
| 961 | { |
||
| 962 | $selects[count($selects)-1]['where'][] = "recur_type IS NULL AND $this->user_table.cal_recur_date=0"; |
||
| 963 | $selects[] = $select; |
||
| 964 | $selects[count($selects)-1]['where'][] = "$this->user_table.cal_recur_date=cal_start"; |
||
| 965 | } |
||
| 966 | } |
||
| 967 | if (is_numeric($offset) && !$params['no_total']) // get the total too |
||
| 968 | { |
||
| 969 | $save_selects = $selects; |
||
| 970 | // we only select cal_table.cal_id (and not cal_table.*) to be able to use DISTINCT (eg. MsSQL does not allow it for text-columns) |
||
| 971 | foreach(array_keys($selects) as $key) |
||
| 972 | { |
||
| 973 | $selects[$key]['cols'] = "$this->repeats_table.recur_type,range_end AS recur_enddate,$this->repeats_table.recur_interval,$this->repeats_table.recur_data,".$this->db->to_varchar($this->cal_table.'.cal_id').",cal_start,cal_end,$this->user_table.cal_recur_date"; |
||
| 974 | if (!$params['enum_recuring']) |
||
| 975 | { |
||
| 976 | $selects[$key]['cols'] = str_replace(array('cal_start','cal_end'), |
||
| 977 | array('range_start AS cal_start','range_end AS cal_end'), $selects[$key]['cols']); |
||
| 978 | } |
||
| 979 | } |
||
| 980 | if (!isset($params['cols']) && !$params['no_integration']) self::get_union_selects($selects,$start,$end,$users,$cat_id,$filter,$params['query'],$params['users']); |
||
| 981 | |||
| 982 | $this->total = $this->db->union($selects,__LINE__,__FILE__)->NumRows(); |
||
| 983 | |||
| 984 | // restore original cols / selects |
||
| 985 | $selects = $save_selects; unset($save_selects); |
||
| 986 | } |
||
| 987 | if (!isset($params['cols']) && !$params['no_integration']) self::get_union_selects($selects,$start,$end,$users,$cat_id,$filter,$params['query'],$params['users']); |
||
| 988 | |||
| 989 | $rs = $this->db->union($selects,__LINE__,__FILE__,$params['order'],$offset,$num_rows); |
||
| 990 | } |
||
| 991 | else // MsSQL oder MySQL 3.23 |
||
| 992 | { |
||
| 993 | $where[] = "(recur_type IS NULL AND $this->user_table.cal_recur_date=0 OR $this->user_table.cal_recur_date=cal_start)"; |
||
| 994 | |||
| 995 | $selects = array(array( |
||
| 996 | 'table' => $cal_table, |
||
| 997 | 'join' => $join, |
||
| 998 | 'cols' => $cols, |
||
| 999 | 'where' => $where, |
||
| 1000 | 'app' => 'calendar', |
||
| 1001 | 'append'=> $params['append'], |
||
| 1002 | 'table_def' => $cal_table_def, |
||
| 1003 | )); |
||
| 1004 | |||
| 1005 | if (is_numeric($offset) && !$params['no_total']) // get the total too |
||
| 1006 | { |
||
| 1007 | $save_selects = $selects; |
||
| 1008 | // we only select cal_table.cal_id (and not cal_table.*) to be able to use DISTINCT (eg. MsSQL does not allow it for text-columns) |
||
| 1009 | $selects[0]['cols'] = "$this->cal_table.cal_id,cal_start"; |
||
| 1010 | View Code Duplication | if (!isset($params['cols']) && !$params['no_integration'] && $this->db->capabilities['union']) |
|
| 1011 | { |
||
| 1012 | self::get_union_selects($selects,$start,$end,$users,$cat_id,$filter,$params['query'],$params['users']); |
||
| 1013 | } |
||
| 1014 | $this->total = $this->db->union($selects, __LINE__, __FILE__)->NumRows(); |
||
| 1015 | $selects = $save_selects; |
||
| 1016 | } |
||
| 1017 | View Code Duplication | if (!isset($params['cols']) && !$params['no_integration'] && $this->db->capabilities['union']) |
|
| 1018 | { |
||
| 1019 | self::get_union_selects($selects,$start,$end,$users,$cat_id,$filter,$params['query'],$params['users']); |
||
| 1020 | } |
||
| 1021 | $rs = $this->db->union($selects,__LINE__,__FILE__,$params['order'],$offset,$num_rows); |
||
| 1022 | } |
||
| 1023 | //error_log(__METHOD__."() useUnionQuery=$useUnionQuery --> query took ".(microtime(true)-$starttime).'s '.$rs->sql); |
||
| 1024 | |||
| 1025 | if (isset($params['cols'])) |
||
| 1026 | { |
||
| 1027 | return $rs; // if colums are specified we return the recordset / iterator |
||
| 1028 | } |
||
| 1029 | // Todo: return $this->get_events($rs); |
||
| 1030 | |||
| 1031 | $events = $ids = $recur_dates = $recur_ids = array(); |
||
| 1032 | foreach($rs as $row) |
||
| 1033 | { |
||
| 1034 | $id = $row['cal_id']; |
||
| 1035 | if (is_numeric($id)) $ids[] = $id; |
||
| 1036 | |||
| 1037 | if ($row['cal_recur_date']) |
||
| 1038 | { |
||
| 1039 | $id .= '-'.$row['cal_recur_date']; |
||
| 1040 | $recur_dates[] = $row['cal_recur_date']; |
||
| 1041 | } |
||
| 1042 | if ($row['participants']) |
||
| 1043 | { |
||
| 1044 | $row['participants'] = explode(',',$row['participants']); |
||
| 1045 | $row['participants'] = array_combine($row['participants'], |
||
| 1046 | array_fill(0,count($row['participants']),'')); |
||
| 1047 | } |
||
| 1048 | else |
||
| 1049 | { |
||
| 1050 | $row['participants'] = array(); |
||
| 1051 | } |
||
| 1052 | $row['recur_exception'] = $row['alarm'] = array(); |
||
| 1053 | |||
| 1054 | // compile a list of recurrences per cal_id |
||
| 1055 | if (!in_array($id,(array)$recur_ids[$row['cal_id']])) $recur_ids[$row['cal_id']][] = $id; |
||
| 1056 | |||
| 1057 | $events[$id] = Api\Db::strip_array_keys($row,'cal_'); |
||
| 1058 | } |
||
| 1059 | //_debug_array($events); |
||
| 1060 | if (count($ids)) |
||
| 1061 | { |
||
| 1062 | $ids = array_unique($ids); |
||
| 1063 | |||
| 1064 | // now ready all users with the given cal_id AND (cal_recur_date=0 or the fitting recur-date) |
||
| 1065 | // This will always read the first entry of each recuring event too, we eliminate it later |
||
| 1066 | $recur_dates[] = 0; |
||
| 1067 | $utcal_id_view = " (SELECT * FROM ".$this->user_table." WHERE cal_id IN (".implode(',',$ids).")". |
||
| 1068 | ($filter != 'everything' ? " AND cal_status NOT IN ('X','E')" : '').") utcalid "; |
||
| 1069 | //$utrecurdate_view = " (select * from ".$this->user_table." where cal_recur_date in (".implode(',',array_unique($recur_dates)).")) utrecurdates "; |
||
| 1070 | foreach($this->db->select($utcal_id_view,'*',array( |
||
| 1071 | //'cal_id' => array_unique($ids), |
||
| 1072 | 'cal_recur_date' => $recur_dates, |
||
| 1073 | ),__LINE__,__FILE__,false,'ORDER BY cal_id,cal_user_type DESC,'.self::STATUS_SORT,'calendar',-1,$join='', |
||
| 1074 | $this->db->get_table_definitions('calendar',$this->user_table)) as $row) // DESC puts users before resources and contacts |
||
| 1075 | { |
||
| 1076 | $id = $row['cal_id']; |
||
| 1077 | if ($row['cal_recur_date']) $id .= '-'.$row['cal_recur_date']; |
||
| 1078 | |||
| 1079 | // combine all participant data in uid and status values |
||
| 1080 | $uid = self::combine_user($row['cal_user_type'], $row['cal_user_id'], $row['cal_user_attendee']); |
||
| 1081 | $status = self::combine_status($row['cal_status'],$row['cal_quantity'],$row['cal_role']); |
||
| 1082 | |||
| 1083 | // set accept/reject/tentative of series for all recurrences |
||
| 1084 | if (!$row['cal_recur_date']) |
||
| 1085 | { |
||
| 1086 | foreach((array)$recur_ids[$row['cal_id']] as $i) |
||
| 1087 | { |
||
| 1088 | if (isset($events[$i]) && !isset($events[$i]['participants'][$uid])) |
||
| 1089 | { |
||
| 1090 | $events[$i]['participants'][$uid] = $status; |
||
| 1091 | } |
||
| 1092 | } |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | // set data, if recurrence is requested |
||
| 1096 | if (isset($events[$id])) $events[$id]['participants'][$uid] = $status; |
||
| 1097 | } |
||
| 1098 | // query recurrance exceptions, if needed: enum_recuring && !daywise is used in calendar_groupdav::get_series($uid,...) |
||
| 1099 | if (!$params['enum_recuring'] || !$params['daywise']) |
||
| 1100 | { |
||
| 1101 | foreach($this->db->select($this->dates_table, 'cal_id,cal_start', array( |
||
| 1102 | 'cal_id' => $ids, |
||
| 1103 | 'recur_exception' => true, |
||
| 1104 | ), __LINE__, __FILE__, false, 'ORDER BY cal_id,cal_start', 'calendar') as $row) |
||
| 1105 | { |
||
| 1106 | // for enum_recurring events are not indexed by cal_id, but $cal_id.'-'.$cal_start |
||
| 1107 | // find master, which is first recurrence |
||
| 1108 | if (!isset($events[$id=$row['cal_id']])) |
||
| 1109 | { |
||
| 1110 | foreach($events as $id => $event) |
||
| 1111 | { |
||
| 1112 | if ($event['id'] == $row['cal_id']) break; |
||
| 1113 | } |
||
| 1114 | } |
||
| 1115 | $events[$id]['recur_exception'][] = $row['cal_start']; |
||
| 1116 | } |
||
| 1117 | } |
||
| 1118 | //custom fields are not shown in the regular views, so we only query them, if explicitly required |
||
| 1119 | if (!is_null($params['cfs'])) |
||
| 1120 | { |
||
| 1121 | $where = array('cal_id' => $ids); |
||
| 1122 | if ($params['cfs']) $where['cal_extra_name'] = $params['cfs']; |
||
| 1123 | foreach($this->db->select($this->extra_table,'*',$where, |
||
| 1124 | __LINE__,__FILE__,false,'','calendar') as $row) |
||
| 1125 | { |
||
| 1126 | foreach((array)$recur_ids[$row['cal_id']] as $id) |
||
| 1127 | { |
||
| 1128 | if (isset($events[$id])) |
||
| 1129 | { |
||
| 1130 | $events[$id]['#'.$row['cal_extra_name']] = $row['cal_extra_value']; |
||
| 1131 | } |
||
| 1132 | } |
||
| 1133 | } |
||
| 1134 | } |
||
| 1135 | // alarms |
||
| 1136 | foreach($this->read_alarms($ids) as $cal_id => $alarms) |
||
| 1137 | { |
||
| 1138 | foreach($alarms as $id => $alarm) |
||
| 1139 | { |
||
| 1140 | $event_start = $alarm['time'] + $alarm['offset']; |
||
| 1141 | |||
| 1142 | if (isset($events[$cal_id])) // none recuring event |
||
| 1143 | { |
||
| 1144 | $events[$cal_id]['alarm'][$id] = $alarm; |
||
| 1145 | } |
||
| 1146 | elseif (isset($events[$cal_id.'-'.$event_start])) // recuring event |
||
| 1147 | { |
||
| 1148 | $events[$cal_id.'-'.$event_start]['alarm'][$id] = $alarm; |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | //echo "<p>socal::search\n"; _debug_array($events); |
||
| 1154 | //error_log(__METHOD__."(,filter=".array2string($params['query']).",offset=$offset, num_rows=$num_rows) returning ".count($events)." entries".($offset!==false?" total=$this->total":'').' '.function_backtrace()); |
||
| 1155 | return $events; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Data returned by calendar_search_union hook |
||
| 1160 | */ |
||
| 1161 | private static $integration_data; |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * Ask other apps if they want to participate in calendar search / display |
||
| 1165 | * |
||
| 1166 | * @param &$selects parts of union query |
||
| 1167 | * @param $start see search() |
||
| 1168 | * @param $end |
||
| 1169 | * @param $users as used in calendar_so ($users_raw plus all members and memberships added by calendar_bo) |
||
| 1170 | * @param $cat_id |
||
| 1171 | * @param $filter |
||
| 1172 | * @param $query |
||
| 1173 | * @param $users_raw as passed to calendar_bo::search (no members and memberships added) |
||
| 1174 | */ |
||
| 1175 | private static function get_union_selects(array &$selects,$start,$end,$users,$cat_id,$filter,$query,$users_raw) |
||
| 1176 | { |
||
| 1177 | if (in_array(basename($_SERVER['SCRIPT_FILENAME']),array('groupdav.php','rpc.php','xmlrpc.php','/activesync/index.php')) || |
||
| 1178 | !in_array($GLOBALS['egw_info']['flags']['currentapp'],array('calendar','home'))) |
||
| 1179 | { |
||
| 1180 | return; // disable integration for GroupDAV, SyncML, ... |
||
| 1181 | } |
||
| 1182 | self::$integration_data = Api\Hooks::process(array( |
||
| 1183 | 'location' => 'calendar_search_union', |
||
| 1184 | 'cols' => $selects[0]['cols'], // cols to return |
||
| 1185 | 'start' => $start, |
||
| 1186 | 'end' => $end, |
||
| 1187 | 'users' => $users, |
||
| 1188 | 'users_raw' => $users_raw, |
||
| 1189 | 'cat_id'=> $cat_id, |
||
| 1190 | 'filter'=> $filter, |
||
| 1191 | 'query' => $query, |
||
| 1192 | )); |
||
| 1193 | foreach(self::$integration_data as $data) |
||
| 1194 | { |
||
| 1195 | if (is_array($data['selects'])) |
||
| 1196 | { |
||
| 1197 | //echo $app; _debug_array($data); |
||
| 1198 | $selects = array_merge($selects,$data['selects']); |
||
| 1199 | } |
||
| 1200 | } |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Get data from last 'calendar_search_union' hook call |
||
| 1205 | * |
||
| 1206 | * @return array |
||
| 1207 | */ |
||
| 1208 | public static function get_integration_data() |
||
| 1209 | { |
||
| 1210 | return self::$integration_data; |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Return union cols constructed from application cols and required cols |
||
| 1215 | * |
||
| 1216 | * Every col not supplied in $app_cols get returned as NULL. |
||
| 1217 | * |
||
| 1218 | * @param array $app_cols required name => own name pairs |
||
| 1219 | * @param string|array $required array or comma separated column names or table.* |
||
| 1220 | * @param string $required_app ='calendar' |
||
| 1221 | * @return string cols for union query to match ones supplied in $required |
||
| 1222 | */ |
||
| 1223 | public static function union_cols(array $app_cols,$required,$required_app='calendar') |
||
| 1224 | { |
||
| 1225 | // remove evtl. used DISTINCT, we currently dont need it |
||
| 1226 | if (($distinct = substr($required,0,9) == 'DISTINCT ')) |
||
| 1227 | { |
||
| 1228 | $required = substr($required,9); |
||
| 1229 | } |
||
| 1230 | $return_cols = array(); |
||
| 1231 | foreach(is_array($required) ? $required : explode(',',$required) as $cols) |
||
| 1232 | { |
||
| 1233 | $matches = null; |
||
| 1234 | if (substr($cols,-2) == '.*') |
||
| 1235 | { |
||
| 1236 | $cols = self::get_columns($required_app,substr($cols,0,-2)); |
||
| 1237 | } |
||
| 1238 | // remove CAST added for PostgreSQL from eg. "CAST(egw_cal.cal_id AS varchar)" |
||
| 1239 | elseif (preg_match('/CAST\(([a-z0-9_.]+) AS [a-z0-9_]+\)/i', $cols, $matches)) |
||
| 1240 | { |
||
| 1241 | $cols = $matches[1]; |
||
| 1242 | } |
||
| 1243 | elseif (strpos($cols,' AS ') !== false) |
||
| 1244 | { |
||
| 1245 | list(,$cols) = explode(' AS ',$cols); |
||
| 1246 | } |
||
| 1247 | foreach((array)$cols as $col) |
||
| 1248 | { |
||
| 1249 | if (substr($col,0,7) == 'egw_cal') // remove table name |
||
| 1250 | { |
||
| 1251 | $col = preg_replace('/^egw_cal[a-z_]*\./','',$col); |
||
| 1252 | } |
||
| 1253 | if (isset($app_cols[$col])) |
||
| 1254 | { |
||
| 1255 | $return_cols[] = $app_cols[$col]; |
||
| 1256 | } |
||
| 1257 | else |
||
| 1258 | { |
||
| 1259 | $return_cols[] = 'NULL'; |
||
| 1260 | } |
||
| 1261 | } |
||
| 1262 | } |
||
| 1263 | //error_log(__METHOD__."(".array2string($app_cols).", ".array2string($required).", '$required_app') returning ".array2string(implode(',',$return_cols))); |
||
| 1264 | return implode(',',$return_cols); |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Get columns of given table, taking into account historically different column order of egw_cal table |
||
| 1269 | * |
||
| 1270 | * @param string $app |
||
| 1271 | * @param string $table |
||
| 1272 | * @return array of column names |
||
| 1273 | */ |
||
| 1274 | static private function get_columns($app,$table) |
||
| 1275 | { |
||
| 1276 | if ($table != 'egw_cal') |
||
| 1277 | { |
||
| 1278 | $table_def = $GLOBALS['egw']->db->get_table_definitions($app,$table); |
||
| 1279 | $cols = array_keys($table_def['fd']); |
||
| 1280 | } |
||
| 1281 | else |
||
| 1282 | { |
||
| 1283 | // special handling for egw_cal, as old databases have a different column order!!! |
||
| 1284 | $cols =& Api\Cache::getSession(__CLASS__,$table); |
||
| 1285 | |||
| 1286 | if (is_null($cols)) |
||
| 1287 | { |
||
| 1288 | $meta = $GLOBALS['egw']->db->metadata($table,true); |
||
| 1289 | $cols = array_keys($meta['meta']); |
||
| 1290 | } |
||
| 1291 | } |
||
| 1292 | return $cols; |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Checks for conflicts |
||
| 1297 | */ |
||
| 1298 | |||
| 1299 | /* folowing SQL checks for conflicts completly on DB level |
||
| 1300 | |||
| 1301 | SELECT cal_user_type, cal_user_id, SUM( cal_quantity ) |
||
| 1302 | FROM egw_cal, egw_cal_dates, egw_cal_user |
||
| 1303 | LEFT JOIN egw_cal_repeats ON egw_cal.cal_id = egw_cal_repeats.cal_id |
||
| 1304 | WHERE egw_cal.cal_id = egw_cal_dates.cal_id |
||
| 1305 | AND egw_cal.cal_id = egw_cal_user.cal_id |
||
| 1306 | AND ( |
||
| 1307 | recur_type IS NULL |
||
| 1308 | AND cal_recur_date =0 |
||
| 1309 | OR cal_recur_date = cal_start |
||
| 1310 | ) |
||
| 1311 | AND ( |
||
| 1312 | ( |
||
| 1313 | cal_user_type = 'u' # user of the checked event |
||
| 1314 | AND cal_user_id |
||
| 1315 | IN ( 7, 5 ) |
||
| 1316 | ) |
||
| 1317 | AND 1118822400 < cal_end # start- and end-time of the checked event |
||
| 1318 | AND cal_start <1118833200 |
||
| 1319 | ) |
||
| 1320 | AND egw_cal.cal_id !=26 # id of the checked event |
||
| 1321 | AND cal_non_blocking !=1 |
||
| 1322 | AND cal_status != 'R' |
||
| 1323 | GROUP BY cal_user_type, cal_user_id |
||
| 1324 | ORDER BY cal_user_type, cal_usre_id |
||
| 1325 | |||
| 1326 | */ |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Saves or creates an event |
||
| 1330 | * |
||
| 1331 | * We always set cal_modified and cal_modifier and for new events cal_uid. |
||
| 1332 | * All other column are only written if they are set in the $event parameter! |
||
| 1333 | * |
||
| 1334 | * @param array $event |
||
| 1335 | * @param boolean &$set_recurrences on return: true if the recurrences need to be written, false otherwise |
||
| 1336 | * @param int &$set_recurrences_start=0 on return: time from which on the recurrences should be rebuilt, default 0=all |
||
| 1337 | * @param int $change_since =0 time from which on the repetitions should be changed, default 0=all |
||
| 1338 | * @param int &$etag etag=null etag to check or null, on return new etag |
||
| 1339 | * @return boolean|int false on error, 0 if etag does not match, cal_id otherwise |
||
| 1340 | */ |
||
| 1341 | function save($event,&$set_recurrences,&$set_recurrences_start=0,$change_since=0,&$etag=null) |
||
| 1342 | { |
||
| 1343 | if (isset($GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length'])) |
||
| 1344 | { |
||
| 1345 | $minimum_uid_length = $GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length']; |
||
| 1346 | if (empty($minimum_uid_length) || $minimum_uid_length<=1) $minimum_uid_length = 8; // we just do not accept no uid, or uid way to short! |
||
| 1347 | } |
||
| 1348 | else |
||
| 1349 | { |
||
| 1350 | $minimum_uid_length = 8; |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | $old_min = $old_duration = 0; |
||
| 1354 | |||
| 1355 | //error_log(__METHOD__.'('.array2string($event).",$set_recurrences,$change_since,$etag) ".function_backtrace()); |
||
| 1356 | |||
| 1357 | $cal_id = (int) $event['id']; |
||
| 1358 | unset($event['id']); |
||
| 1359 | $set_recurrences = $set_recurrences || !$cal_id && $event['recur_type'] != MCAL_RECUR_NONE; |
||
| 1360 | |||
| 1361 | if ($event['recur_type'] != MCAL_RECUR_NONE && |
||
| 1362 | !(int)$event['recur_interval']) |
||
| 1363 | { |
||
| 1364 | $event['recur_interval'] = 1; |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | // add colum prefix 'cal_' if there's not already a 'recur_' prefix |
||
| 1368 | foreach($event as $col => $val) |
||
| 1369 | { |
||
| 1370 | if ($col[0] != '#' && substr($col,0,6) != 'recur_' && substr($col,0,6) != 'range_' && $col != 'alarm' && $col != 'tz_id' && $col != 'caldav_name') |
||
| 1371 | { |
||
| 1372 | $event['cal_'.$col] = $val; |
||
| 1373 | unset($event[$col]); |
||
| 1374 | } |
||
| 1375 | } |
||
| 1376 | // set range_start/_end, but only if we have cal_start/_end, as otherwise we destroy present values! |
||
| 1377 | if (isset($event['cal_start'])) $event['range_start'] = $event['cal_start']; |
||
| 1378 | if (isset($event['cal_end'])) |
||
| 1379 | { |
||
| 1380 | $event['range_end'] = $event['recur_type'] == MCAL_RECUR_NONE ? $event['cal_end'] : |
||
| 1381 | ($event['recur_enddate'] ? $event['recur_enddate'] : null); |
||
| 1382 | } |
||
| 1383 | // ensure that we find mathing entries later on |
||
| 1384 | if (!is_array($event['cal_category'])) |
||
| 1385 | { |
||
| 1386 | $categories = array_unique(explode(',',$event['cal_category'])); |
||
| 1387 | sort($categories); |
||
| 1388 | } |
||
| 1389 | else |
||
| 1390 | { |
||
| 1391 | $categories = array_unique($event['cal_category']); |
||
| 1392 | } |
||
| 1393 | sort($categories, SORT_NUMERIC); |
||
| 1394 | |||
| 1395 | $event['cal_category'] = implode(',',$categories); |
||
| 1396 | |||
| 1397 | // make sure recurring events never reference to an other recurrent event |
||
| 1398 | if ($event['recur_type'] != MCAL_RECUR_NONE) $event['cal_reference'] = 0; |
||
| 1399 | |||
| 1400 | if ($cal_id) |
||
| 1401 | { |
||
| 1402 | // query old recurrance information, before updating main table, where recur_endate is now stored |
||
| 1403 | if ($event['recur_type'] != MCAL_RECUR_NONE) |
||
| 1404 | { |
||
| 1405 | $old_repeats = $this->db->select($this->repeats_table, "$this->repeats_table.*,range_end AS recur_enddate", |
||
| 1406 | "$this->repeats_table.cal_id=".(int)$cal_id, __LINE__, __FILE__, |
||
| 1407 | false, '', 'calendar', 0, "JOIN $this->cal_table ON $this->repeats_table.cal_id=$this->cal_table.cal_id")->fetch(); |
||
| 1408 | } |
||
| 1409 | $where = array('cal_id' => $cal_id); |
||
| 1410 | // read only timezone id, to check if it is changed |
||
| 1411 | if ($event['recur_type'] != MCAL_RECUR_NONE) |
||
| 1412 | { |
||
| 1413 | $old_tz_id = $this->db->select($this->cal_table,'tz_id',$where,__LINE__,__FILE__,'calendar')->fetchColumn(); |
||
| 1414 | } |
||
| 1415 | if (!is_null($etag)) $where['cal_etag'] = $etag; |
||
| 1416 | |||
| 1417 | unset($event['cal_etag']); |
||
| 1418 | $event[] = 'cal_etag=cal_etag+1'; // always update the etag, even if none given to check |
||
| 1419 | |||
| 1420 | $this->db->update($this->cal_table,$event,$where,__LINE__,__FILE__,'calendar'); |
||
| 1421 | |||
| 1422 | if (!is_null($etag) && $this->db->affected_rows() < 1) |
||
| 1423 | { |
||
| 1424 | return 0; // wrong etag, someone else updated the entry |
||
| 1425 | } |
||
| 1426 | if (!is_null($etag)) ++$etag; |
||
| 1427 | } |
||
| 1428 | else |
||
| 1429 | { |
||
| 1430 | // new event |
||
| 1431 | if (!$event['cal_owner']) $event['cal_owner'] = $GLOBALS['egw_info']['user']['account_id']; |
||
| 1432 | |||
| 1433 | if (!$event['cal_id'] && !isset($event['cal_uid'])) $event['cal_uid'] = ''; // uid is NOT NULL! |
||
| 1434 | |||
| 1435 | $this->db->insert($this->cal_table,$event,false,__LINE__,__FILE__,'calendar'); |
||
| 1436 | if (!($cal_id = $this->db->get_last_insert_id($this->cal_table,'cal_id'))) |
||
| 1437 | { |
||
| 1438 | return false; |
||
| 1439 | } |
||
| 1440 | $etag = 0; |
||
| 1441 | } |
||
| 1442 | $update = array(); |
||
| 1443 | // event without uid or not strong enough uid |
||
| 1444 | if (!isset($event['cal_uid']) || strlen($event['cal_uid']) < $minimum_uid_length) |
||
| 1445 | { |
||
| 1446 | $update['cal_uid'] = $event['cal_uid'] = Api\CalDAV::generate_uid('calendar',$cal_id); |
||
| 1447 | } |
||
| 1448 | // set caldav_name, if not given by caller |
||
| 1449 | if (empty($event['caldav_name']) && version_compare($GLOBALS['egw_info']['apps']['calendar']['version'], '1.9.003', '>=')) |
||
| 1450 | { |
||
| 1451 | $update['caldav_name'] = $event['caldav_name'] = $cal_id.'.ics'; |
||
| 1452 | } |
||
| 1453 | if ($update) |
||
| 1454 | { |
||
| 1455 | $this->db->update($this->cal_table, $update, array('cal_id' => $cal_id),__LINE__,__FILE__,'calendar'); |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | if ($event['recur_type'] == MCAL_RECUR_NONE) |
||
| 1459 | { |
||
| 1460 | $this->db->delete($this->dates_table,array( |
||
| 1461 | 'cal_id' => $cal_id), |
||
| 1462 | __LINE__,__FILE__,'calendar'); |
||
| 1463 | |||
| 1464 | // delete all user-records, with recur-date != 0 |
||
| 1465 | $this->db->delete($this->user_table,array( |
||
| 1466 | 'cal_id' => $cal_id, 'cal_recur_date != 0'), |
||
| 1467 | __LINE__,__FILE__,'calendar'); |
||
| 1468 | |||
| 1469 | $this->db->delete($this->repeats_table,array( |
||
| 1470 | 'cal_id' => $cal_id), |
||
| 1471 | __LINE__,__FILE__,'calendar'); |
||
| 1472 | |||
| 1473 | // add exception marker to master, so participants added to exceptions *only* get found |
||
| 1474 | if ($event['cal_reference']) |
||
| 1475 | { |
||
| 1476 | $master_participants = array(); |
||
| 1477 | foreach($this->db->select($this->user_table, 'cal_user_type,cal_user_id,cal_user_attendee', array( |
||
| 1478 | 'cal_id' => $event['cal_reference'], |
||
| 1479 | 'cal_recur_date' => 0, |
||
| 1480 | "cal_status != 'X'", // deleted need to be replaced with exception marker too |
||
| 1481 | ), __LINE__, __FILE__, 'calendar') as $row) |
||
| 1482 | { |
||
| 1483 | $master_participants[] = self::combine_user($row['cal_user_type'], $row['cal_user_id'], $row['cal_user_attendee']); |
||
| 1484 | } |
||
| 1485 | foreach(array_diff(array_keys((array)$event['cal_participants']), $master_participants) as $uid) |
||
| 1486 | { |
||
| 1487 | $user_type = $user_id = null; |
||
| 1488 | self::split_user($uid, $user_type, $user_id, true); |
||
| 1489 | $this->db->insert($this->user_table, array( |
||
| 1490 | 'cal_status' => 'E', |
||
| 1491 | 'cal_user_attendee' => $user_type == 'e' ? substr($uid, 1) : null, |
||
| 1492 | ), array( |
||
| 1493 | 'cal_id' => $event['cal_reference'], |
||
| 1494 | 'cal_recur_date' => 0, |
||
| 1495 | 'cal_user_type' => $user_type, |
||
| 1496 | 'cal_user_id' => $user_id, |
||
| 1497 | ), __LINE__, __FILE__, 'calendar'); |
||
| 1498 | } |
||
| 1499 | } |
||
| 1500 | } |
||
| 1501 | else // write information about recuring event, if recur_type is present in the array |
||
| 1502 | { |
||
| 1503 | // fetch information about the currently saved (old) event |
||
| 1504 | $old_min = (int) $this->db->select($this->dates_table,'MIN(cal_start)',array('cal_id'=>$cal_id),__LINE__,__FILE__,false,'','calendar')->fetchColumn(); |
||
| 1505 | $old_duration = (int) $this->db->select($this->dates_table,'MIN(cal_end)',array('cal_id'=>$cal_id),__LINE__,__FILE__,false,'','calendar')->fetchColumn() - $old_min; |
||
| 1506 | $old_exceptions = array(); |
||
| 1507 | foreach($this->db->select($this->dates_table, 'cal_start', array( |
||
| 1508 | 'cal_id' => $cal_id, |
||
| 1509 | 'recur_exception' => true |
||
| 1510 | ), __LINE__, __FILE__, false, 'ORDER BY cal_start', 'calendar') as $row) |
||
| 1511 | { |
||
| 1512 | $old_exceptions[] = $row['cal_start']; |
||
| 1513 | } |
||
| 1514 | |||
| 1515 | $event['recur_exception'] = is_array($event['recur_exception']) ? $event['recur_exception'] : array(); |
||
| 1516 | if (!empty($event['recur_exception'])) |
||
| 1517 | { |
||
| 1518 | sort($event['recur_exception']); |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | $where = array( |
||
| 1522 | 'cal_id' => $cal_id, |
||
| 1523 | 'cal_recur_date' => 0, |
||
| 1524 | ); |
||
| 1525 | $old_participants = array(); |
||
| 1526 | foreach ($this->db->select($this->user_table,'cal_user_type,cal_user_id,cal_user_attendee,cal_status,cal_quantity,cal_role', $where, |
||
| 1527 | __LINE__,__FILE__,false,'','calendar') as $row) |
||
| 1528 | { |
||
| 1529 | $uid = self::combine_user($row['cal_user_type'], $row['cal_user_id'], $row['cal_user_attendee']); |
||
| 1530 | $status = self::combine_status($row['cal_status'], $row['cal_quantity'], $row['cal_role']); |
||
| 1531 | $old_participants[$uid] = $status; |
||
| 1532 | } |
||
| 1533 | |||
| 1534 | // re-check: did so much recurrence data change that we have to rebuild it from scratch? |
||
| 1535 | if (!$set_recurrences) |
||
| 1536 | { |
||
| 1537 | $set_recurrences = (isset($event['cal_start']) && (int)$old_min != (int) $event['cal_start']) || |
||
| 1538 | $event['recur_type'] != $old_repeats['recur_type'] || $event['recur_data'] != $old_repeats['recur_data'] || |
||
| 1539 | (int)$event['recur_interval'] != (int)$old_repeats['recur_interval'] || $event['tz_id'] != $old_tz_id; |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | if ($set_recurrences) |
||
| 1543 | { |
||
| 1544 | // too much recurrence data has changed, we have to do a rebuild from scratch |
||
| 1545 | // delete all, but the lowest dates record |
||
| 1546 | $this->db->delete($this->dates_table,array( |
||
| 1547 | 'cal_id' => $cal_id, |
||
| 1548 | 'cal_start > '.(int)$old_min, |
||
| 1549 | ),__LINE__,__FILE__,'calendar'); |
||
| 1550 | |||
| 1551 | // delete all user-records, with recur-date != 0 |
||
| 1552 | $this->db->delete($this->user_table,array( |
||
| 1553 | 'cal_id' => $cal_id, |
||
| 1554 | 'cal_recur_date != 0', |
||
| 1555 | ),__LINE__,__FILE__,'calendar'); |
||
| 1556 | } |
||
| 1557 | else |
||
| 1558 | { |
||
| 1559 | // we adjust some possibly changed recurrences manually |
||
| 1560 | // deleted exceptions: re-insert recurrences into the user and dates table |
||
| 1561 | if (count($deleted_exceptions = array_diff($old_exceptions,$event['recur_exception']))) |
||
| 1562 | { |
||
| 1563 | if (isset($event['cal_participants'])) |
||
| 1564 | { |
||
| 1565 | $participants = $event['cal_participants']; |
||
| 1566 | } |
||
| 1567 | else |
||
| 1568 | { |
||
| 1569 | // use old default |
||
| 1570 | $participants = $old_participants; |
||
| 1571 | } |
||
| 1572 | foreach($deleted_exceptions as $id => $deleted_exception) |
||
| 1573 | { |
||
| 1574 | // rebuild participants for the re-inserted recurrence |
||
| 1575 | $this->recurrence($cal_id, $deleted_exception, $deleted_exception + $old_duration, $participants); |
||
| 1576 | } |
||
| 1577 | } |
||
| 1578 | |||
| 1579 | // check if recurrence enddate was adjusted |
||
| 1580 | if(isset($event['recur_enddate'])) |
||
| 1581 | { |
||
| 1582 | // recurrences need to be truncated |
||
| 1583 | if((int)$event['recur_enddate'] > 0 && |
||
| 1584 | ((int)$old_repeats['recur_enddate'] == 0 || (int)$old_repeats['recur_enddate'] > (int)$event['recur_enddate']) |
||
| 1585 | ) |
||
| 1586 | { |
||
| 1587 | $this->db->delete($this->user_table,array('cal_id' => $cal_id,'cal_recur_date >= '.($event['recur_enddate'] + 1*DAY_s)),__LINE__,__FILE__,'calendar'); |
||
| 1588 | $this->db->delete($this->dates_table,array('cal_id' => $cal_id,'cal_start >= '.($event['recur_enddate'] + 1*DAY_s)),__LINE__,__FILE__,'calendar'); |
||
| 1589 | } |
||
| 1590 | |||
| 1591 | // recurrences need to be expanded |
||
| 1592 | if(((int)$event['recur_enddate'] == 0 && (int)$old_repeats['recur_enddate'] > 0) |
||
| 1593 | || ((int)$event['recur_enddate'] > 0 && (int)$old_repeats['recur_enddate'] > 0 && (int)$old_repeats['recur_enddate'] < (int)$event['recur_enddate']) |
||
| 1594 | ) |
||
| 1595 | { |
||
| 1596 | $set_recurrences = true; |
||
| 1597 | $set_recurrences_start = ($old_repeats['recur_enddate'] + 1*DAY_s); |
||
| 1598 | } |
||
| 1599 | //error_log(__METHOD__."() event[recur_enddate]=$event[recur_enddate], old_repeats[recur_enddate]=$old_repeats[recur_enddate] --> set_recurrences=".array2string($set_recurrences).", set_recurrences_start=$set_recurrences_start"); |
||
| 1600 | } |
||
| 1601 | |||
| 1602 | // truncate recurrences by given exceptions |
||
| 1603 | if (count($event['recur_exception'])) |
||
| 1604 | { |
||
| 1605 | // added and existing exceptions: delete the execeptions from the user table, it could be the first time |
||
| 1606 | $this->db->delete($this->user_table,array('cal_id' => $cal_id,'cal_recur_date' => $event['recur_exception']),__LINE__,__FILE__,'calendar'); |
||
| 1607 | // update recur_exception flag based on current exceptions |
||
| 1608 | $this->db->update($this->dates_table, 'recur_exception='.$this->db->expression($this->dates_table,array( |
||
| 1609 | 'cal_start' => $event['recur_exception'], |
||
| 1610 | )), array( |
||
| 1611 | 'cal_id' => $cal_id, |
||
| 1612 | ), __LINE__, __FILE__, 'calendar'); |
||
| 1613 | } |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | // write the repeats table |
||
| 1617 | unset($event[0]); // unset the 'etag=etag+1', as it's not in the repeats table |
||
| 1618 | $this->db->insert($this->repeats_table,$event,array('cal_id' => $cal_id),__LINE__,__FILE__,'calendar'); |
||
| 1619 | } |
||
| 1620 | // update start- and endtime if present in the event-array, evtl. we need to move all recurrences |
||
| 1621 | if (isset($event['cal_start']) && isset($event['cal_end'])) |
||
| 1622 | { |
||
| 1623 | $this->move($cal_id,$event['cal_start'],$event['cal_end'],!$cal_id ? false : $change_since, $old_min, $old_min + $old_duration); |
||
| 1624 | } |
||
| 1625 | // update participants if present in the event-array |
||
| 1626 | if (isset($event['cal_participants'])) |
||
| 1627 | { |
||
| 1628 | $this->participants($cal_id,$event['cal_participants'],!$cal_id ? false : $change_since); |
||
| 1629 | } |
||
| 1630 | // Custom fields |
||
| 1631 | foreach($event as $name => $value) |
||
| 1632 | { |
||
| 1633 | if ($name[0] == '#') |
||
| 1634 | { |
||
| 1635 | if (is_array($value) && array_key_exists('id',$value)) |
||
| 1636 | { |
||
| 1637 | //error_log(__METHOD__.__LINE__."$name => ".array2string($value).function_backtrace()); |
||
| 1638 | $value = $value['id']; |
||
| 1639 | //error_log(__METHOD__.__LINE__."$name => ".array2string($value)); |
||
| 1640 | } |
||
| 1641 | if ($value) |
||
| 1642 | { |
||
| 1643 | $this->db->insert($this->extra_table,array( |
||
| 1644 | 'cal_extra_value' => is_array($value) ? implode(',',$value) : $value, |
||
| 1645 | ),array( |
||
| 1646 | 'cal_id' => $cal_id, |
||
| 1647 | 'cal_extra_name' => substr($name,1), |
||
| 1648 | ),__LINE__,__FILE__,'calendar'); |
||
| 1649 | } |
||
| 1650 | else |
||
| 1651 | { |
||
| 1652 | $this->db->delete($this->extra_table,array( |
||
| 1653 | 'cal_id' => $cal_id, |
||
| 1654 | 'cal_extra_name' => substr($name,1), |
||
| 1655 | ),__LINE__,__FILE__,'calendar'); |
||
| 1656 | } |
||
| 1657 | } |
||
| 1658 | } |
||
| 1659 | // updating or saving the alarms; new alarms have a temporary numeric id! |
||
| 1660 | if (is_array($event['alarm'])) |
||
| 1661 | { |
||
| 1662 | foreach ($event['alarm'] as $id => $alarm) |
||
| 1663 | { |
||
| 1664 | if ($alarm['id'] && strpos($alarm['id'], 'cal:'.$cal_id.':') !== 0) |
||
| 1665 | { |
||
| 1666 | unset($alarm['id']); // unset the temporary id to add the alarm |
||
| 1667 | } |
||
| 1668 | if(!isset($alarm['offset'])) |
||
| 1669 | { |
||
| 1670 | $alarm['offset'] = $event['cal_start'] - $alarm['time']; |
||
| 1671 | } |
||
| 1672 | elseif (!isset($alarm['time'])) |
||
| 1673 | { |
||
| 1674 | $alarm['time'] = $event['cal_start'] - $alarm['offset']; |
||
| 1675 | } |
||
| 1676 | |||
| 1677 | if ($alarm['time'] < time() && !self::shift_alarm($event, $alarm)) |
||
| 1678 | { |
||
| 1679 | continue; // pgoerzen: don't add alarm in the past |
||
| 1680 | } |
||
| 1681 | $this->save_alarm($cal_id, $alarm, false); // false: not update modified, we do it anyway |
||
| 1682 | } |
||
| 1683 | } |
||
| 1684 | if (is_null($etag)) |
||
| 1685 | { |
||
| 1686 | $etag = $this->db->select($this->cal_table,'cal_etag',array('cal_id' => $cal_id),__LINE__,__FILE__,false,'','calendar')->fetchColumn(); |
||
| 1687 | } |
||
| 1688 | |||
| 1689 | // if event is an exception: update modified of master, to force etag, ctag and sync-token change |
||
| 1690 | if ($event['cal_reference']) |
||
| 1691 | { |
||
| 1692 | $this->updateModified($event['cal_reference']); |
||
| 1693 | } |
||
| 1694 | return $cal_id; |
||
| 1695 | } |
||
| 1696 | |||
| 1697 | /** |
||
| 1698 | * Shift alarm on recurring events to next future recurrence |
||
| 1699 | * |
||
| 1700 | * @param array $_event event with optional 'cal_' prefix in keys |
||
| 1701 | * @param array &$alarm |
||
| 1702 | * @param int $timestamp For recurring events, this is the date we |
||
| 1703 | * are dealing with, default is now. |
||
| 1704 | * @return boolean true if alarm could be shifted, false if not |
||
| 1705 | */ |
||
| 1706 | public static function shift_alarm(array $_event, array &$alarm, $timestamp=null) |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * moves an event to an other start- and end-time taken into account the evtl. recurrences of the event(!) |
||
| 1728 | * |
||
| 1729 | * @param int $cal_id |
||
| 1730 | * @param int $start new starttime |
||
| 1731 | * @param int $end new endtime |
||
| 1732 | * @param int|boolean $change_since =0 false=new entry, > 0 time from which on the repetitions should be changed, default 0=all |
||
| 1733 | * @param int $old_start =0 old starttime or (default) 0, to query it from the db |
||
| 1734 | * @param int $old_end =0 old starttime or (default) 0 |
||
| 1735 | * @todo Recalculate recurrences, if timezone changes |
||
| 1736 | * @return int|boolean number of moved recurrences or false on error |
||
| 1737 | */ |
||
| 1738 | function move($cal_id,$start,$end,$change_since=0,$old_start=0,$old_end=0) |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * Format attendee as email |
||
| 1786 | * |
||
| 1787 | * @param string|array $attendee attendee information: email, json or array with attr cn and url |
||
| 1788 | * @return type |
||
| 1789 | */ |
||
| 1790 | static function attendee2email($attendee) |
||
| 1804 | /** |
||
| 1805 | * combines user_type and user_id into a single string or integer (for users) |
||
| 1806 | * |
||
| 1807 | * @param string $user_type 1-char type: 'u' = user, ... |
||
| 1808 | * @param string|int $user_id id |
||
| 1809 | * @param string|array $attendee attendee information: email, json or array with attr cn and url |
||
| 1810 | * @return string|int combined id |
||
| 1811 | */ |
||
| 1812 | static function combine_user($user_type, $user_id, $attendee=null) |
||
| 1824 | |||
| 1825 | /** |
||
| 1826 | * splits the combined user_type and user_id into a single values |
||
| 1827 | * |
||
| 1828 | * This is the only method building (normalized) md5 hashes for user_type="e", |
||
| 1829 | * if called with $md5_email=true parameter! |
||
| 1830 | * |
||
| 1831 | * @param string|int $uid |
||
| 1832 | * @param string &$user_type 1-char type: 'u' = user, ... |
||
| 1833 | * @param string|int &$user_id id |
||
| 1834 | * @param boolean $md5_email =false md5 hash user_id for email / user_type=="e" |
||
| 1835 | */ |
||
| 1836 | static function split_user($uid, &$user_type, &$user_id, $md5_email=false) |
||
| 1858 | |||
| 1859 | /** |
||
| 1860 | * Combine status, quantity and role into one value |
||
| 1861 | * |
||
| 1862 | * @param string $status status letter: U, T, A, R |
||
| 1863 | * @param int $quantity =1 |
||
| 1864 | * @param string $role ='REQ-PARTICIPANT' |
||
| 1865 | * @return string |
||
| 1866 | */ |
||
| 1867 | static function combine_status($status,$quantity=1,$role='REQ-PARTICIPANT') |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * splits the combined status, quantity and role |
||
| 1877 | * |
||
| 1878 | * @param string &$status I: combined value, O: status letter: U, T, A, R |
||
| 1879 | * @param int &$quantity=null only O: quantity |
||
| 1880 | * @param string &$role=null only O: role |
||
| 1881 | * @return string status U, T, A or R, same as $status parameter on return |
||
| 1882 | */ |
||
| 1883 | static function split_status(&$status,&$quantity=null,&$role=null) |
||
| 1901 | |||
| 1902 | /** |
||
| 1903 | * updates the participants of an event, taken into account the evtl. recurrences of the event(!) |
||
| 1904 | * this method just adds new participants or removes not longer set participants |
||
| 1905 | * this method does never overwrite existing entries (except the 0-recurrence and for delete) |
||
| 1906 | * |
||
| 1907 | * @param int $cal_id |
||
| 1908 | * @param array $participants uid => status pairs |
||
| 1909 | * @param int|boolean $change_since =0, false=new event, |
||
| 1910 | * 0=all, > 0 time from which on the repetitions should be changed |
||
| 1911 | * @param boolean $add_only =false |
||
| 1912 | * false = add AND delete participants if needed (full list of participants required in $participants) |
||
| 1913 | * true = only add participants if needed, no participant will be deleted (participants to check/add required in $participants) |
||
| 1914 | * @return int|boolean number of updated recurrences or false on error |
||
| 1915 | */ |
||
| 1916 | function participants($cal_id,$participants,$change_since=0,$add_only=false) |
||
| 2050 | |||
| 2051 | /** |
||
| 2052 | * set the status of one participant for a given recurrence or for all recurrences since now (includes recur_date=0) |
||
| 2053 | * |
||
| 2054 | * @param int $cal_id |
||
| 2055 | * @param char $user_type 'u' regular user, 'r' resource, 'c' contact |
||
| 2056 | * @param int|string $user_id |
||
| 2057 | * @param int|char $status numeric status (defines) or 1-char code: 'R', 'U', 'T' or 'A' |
||
| 2058 | * @param int $recur_date =0 date to change, or 0 = all since now |
||
| 2059 | * @param string $role =null role to set if !is_null($role) |
||
| 2060 | * @param string $attendee =null extra attendee information to set for all types (incl. accounts!) |
||
| 2061 | * @return int number of changed recurrences |
||
| 2062 | */ |
||
| 2063 | function set_status($cal_id,$user_type,$user_id,$status,$recur_date=0,$role=null,$attendee=null) |
||
| 2125 | |||
| 2126 | /** |
||
| 2127 | * creates or update a recurrence in the dates and users table |
||
| 2128 | * |
||
| 2129 | * @param int $cal_id |
||
| 2130 | * @param int $start |
||
| 2131 | * @param int $end |
||
| 2132 | * @param array $participants uid => status pairs |
||
| 2133 | * @param boolean $exception =null true or false to set recure_exception flag, null leave it unchanged (new are by default no exception) |
||
| 2134 | */ |
||
| 2135 | function recurrence($cal_id,$start,$end,$participants,$exception=null) |
||
| 2175 | |||
| 2176 | /** |
||
| 2177 | * Get all unfinished recuring events (or all users) after a given time |
||
| 2178 | * |
||
| 2179 | * @param int $time |
||
| 2180 | * @return array with cal_id => max(cal_start) pairs |
||
| 2181 | */ |
||
| 2182 | function unfinished_recuring($time) |
||
| 2196 | |||
| 2197 | /** |
||
| 2198 | * deletes an event incl. all recurrences, participants and alarms |
||
| 2199 | * |
||
| 2200 | * @param int $cal_id |
||
| 2201 | */ |
||
| 2202 | function delete($cal_id) |
||
| 2216 | |||
| 2217 | /** |
||
| 2218 | * Delete all events that were before the given date. |
||
| 2219 | * |
||
| 2220 | * Recurring events that finished before the date will be deleted. |
||
| 2221 | * Recurring events that span the date will be ignored. Non-recurring |
||
| 2222 | * events before the date will be deleted. |
||
| 2223 | * |
||
| 2224 | * @param int $date |
||
| 2225 | */ |
||
| 2226 | function purge($date) |
||
| 2247 | |||
| 2248 | /** |
||
| 2249 | * Caches all alarms read from async table to not re-read them in same request |
||
| 2250 | * |
||
| 2251 | * @var array cal_id => array(async_id => data) |
||
| 2252 | */ |
||
| 2253 | static $alarm_cache; |
||
| 2254 | |||
| 2255 | /** |
||
| 2256 | * read the alarms of one or more calendar-event(s) specified by $cal_id |
||
| 2257 | * |
||
| 2258 | * alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too |
||
| 2259 | * |
||
| 2260 | * @param int|array $cal_id |
||
| 2261 | * @param boolean $update_cache =null true: re-read given $cal_id, false: delete given $cal_id |
||
| 2262 | * @return array of (cal_id => array of) alarms with alarm-id as key |
||
| 2263 | */ |
||
| 2264 | function read_alarms($cal_id, $update_cache=null) |
||
| 2316 | |||
| 2317 | private function read_alarms_nocache($cal_id) |
||
| 2318 | { |
||
| 2319 | if (($jobs = $this->async->read('cal:'.(int)$cal_id.':%'))) |
||
| 2320 | { |
||
| 2321 | foreach($jobs as $id => $job) |
||
| 2322 | { |
||
| 2323 | $alarm = $job['data']; // text, enabled |
||
| 2324 | $alarm['id'] = $id; |
||
| 2325 | $alarm['time'] = $job['next']; |
||
| 2326 | |||
| 2327 | $alarms[$id] = $alarm; |
||
| 2328 | } |
||
| 2329 | } |
||
| 2330 | //error_log(__METHOD__."(".array2string($cal_id).") returning ".array2string($alarms)); |
||
| 2331 | return $alarms ? $alarms : array(); |
||
| 2332 | } |
||
| 2333 | |||
| 2334 | /** |
||
| 2335 | * read a single alarm specified by it's $id |
||
| 2336 | * |
||
| 2337 | * @param string $id alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too |
||
| 2338 | * @return array with data of the alarm |
||
| 2339 | */ |
||
| 2340 | function read_alarm($id) |
||
| 2354 | |||
| 2355 | /** |
||
| 2356 | * saves a new or updated alarm |
||
| 2357 | * |
||
| 2358 | * @param int $cal_id Id of the calendar-entry |
||
| 2359 | * @param array $alarm array with fields: text, owner, enabled, .. |
||
| 2360 | * @param boolean $update_modified =true call update modified, default true |
||
| 2361 | * @return string id of the alarm |
||
| 2362 | */ |
||
| 2363 | function save_alarm($cal_id, $alarm, $update_modified=true) |
||
| 2399 | |||
| 2400 | /** |
||
| 2401 | * Delete all alarms of a calendar-entry |
||
| 2402 | * |
||
| 2403 | * Does not update timestamps of series master, therefore private! |
||
| 2404 | * |
||
| 2405 | * @param int $cal_id Id of the calendar-entry |
||
| 2406 | * @return int number of alarms deleted |
||
| 2407 | */ |
||
| 2408 | View Code Duplication | private function delete_alarms($cal_id) |
|
| 2422 | |||
| 2423 | /** |
||
| 2424 | * delete one alarms identified by its id |
||
| 2425 | * |
||
| 2426 | * @param string $id alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, it is used as the job-id too |
||
| 2427 | * @return int number of alarms deleted |
||
| 2428 | */ |
||
| 2429 | View Code Duplication | function delete_alarm($id) |
|
| 2445 | |||
| 2446 | /** |
||
| 2447 | * Delete account hook |
||
| 2448 | * |
||
| 2449 | * @param array|int $old_user integer old user or array with keys 'account_id' and 'new_owner' as the deleteaccount hook uses it |
||
| 2450 | * @param int $new_user =null |
||
| 2451 | */ |
||
| 2452 | function deleteaccount($old_user, $new_user=null) |
||
| 2511 | |||
| 2512 | /** |
||
| 2513 | * get stati of all recurrences of an event for a specific participant |
||
| 2514 | * |
||
| 2515 | * @param int $cal_id |
||
| 2516 | * @param int $uid =null participant uid; if == null return only the recur dates |
||
| 2517 | * @param int $start =0 if != 0: startdate of the search/list (servertime) |
||
| 2518 | * @param int $end =0 if != 0: enddate of the search/list (servertime) |
||
| 2519 | * |
||
| 2520 | * @return array recur_date => status pairs (index 0 => main status) |
||
| 2521 | */ |
||
| 2522 | function get_recurrences($cal_id, $uid=null, $start=0, $end=0) |
||
| 2562 | |||
| 2563 | /** |
||
| 2564 | * get all participants of an event |
||
| 2565 | * |
||
| 2566 | * @param int $cal_id |
||
| 2567 | * @param int $recur_date =0 gives participants of this recurrence, default 0=all |
||
| 2568 | * |
||
| 2569 | * @return array participants |
||
| 2570 | */ |
||
| 2571 | /* seems NOT to be used anywhere, NOT ported to new md5-email schema! |
||
| 2572 | function get_participants($cal_id, $recur_date=0) |
||
| 2573 | { |
||
| 2574 | $participants = array(); |
||
| 2575 | $where = array('cal_id' => $cal_id); |
||
| 2576 | if ($recur_date) |
||
| 2577 | { |
||
| 2578 | $where['cal_recur_date'] = $recur_date; |
||
| 2579 | } |
||
| 2580 | |||
| 2581 | foreach ($this->db->select($this->user_table,'DISTINCT cal_user_type,cal_user_id', $where, |
||
| 2582 | __LINE__,__FILE__,false,'','calendar') as $row) |
||
| 2583 | { |
||
| 2584 | $uid = self::combine_user($row['cal_user_type'], $row['cal_user_id']); |
||
| 2585 | $id = $row['cal_user_type'] . $row['cal_user_id']; |
||
| 2586 | $participants[$id]['type'] = $row['cal_user_type']; |
||
| 2587 | $participants[$id]['id'] = $row['cal_user_id']; |
||
| 2588 | $participants[$id]['uid'] = $uid; |
||
| 2589 | } |
||
| 2590 | return $participants; |
||
| 2591 | }*/ |
||
| 2592 | |||
| 2593 | /** |
||
| 2594 | * get all releated events |
||
| 2595 | * |
||
| 2596 | * @param int $uid UID of the series |
||
| 2597 | * |
||
| 2598 | * @return array of event exception ids for all events which share $uid |
||
| 2599 | */ |
||
| 2600 | function get_related($uid) |
||
| 2617 | |||
| 2618 | /** |
||
| 2619 | * Gets the exception days of a given recurring event caused by |
||
| 2620 | * irregular participant stati or timezone transitions |
||
| 2621 | * |
||
| 2622 | * @param array $event Recurring Event. |
||
| 2623 | * @param string tz_id=null timezone for exports (null for event's timezone) |
||
| 2624 | * @param int $start =0 if != 0: startdate of the search/list (servertime) |
||
| 2625 | * @param int $end =0 if != 0: enddate of the search/list (servertime) |
||
| 2626 | * @param string $filter ='all' string filter-name: all (not rejected), |
||
| 2627 | * accepted, unknown, tentative, rejected, delegated |
||
| 2628 | * rrule return array of remote exceptions in servertime |
||
| 2629 | * tz_rrule/tz_only, return (only by) timezone transition affected entries |
||
| 2630 | * map return array of dates with no pseudo exception |
||
| 2631 | * key remote occurrence date |
||
| 2632 | * tz_map return array of all dates with no tz pseudo exception |
||
| 2633 | * |
||
| 2634 | * @return array Array of exception days (false for non-recurring events). |
||
| 2635 | */ |
||
| 2636 | function get_recurrence_exceptions($event, $tz_id=null, $start=0, $end=0, $filter='all') |
||
| 2783 | |||
| 2784 | /** |
||
| 2785 | * Checks for status only pseudo exceptions |
||
| 2786 | * |
||
| 2787 | * @param int $cal_id event id |
||
| 2788 | * @param int $recur_date occurrence to check |
||
| 2789 | * @param string $filter status filter criteria for user |
||
| 2790 | * |
||
| 2791 | * @return boolean true, if stati don't match with defaults |
||
| 2792 | */ |
||
| 2793 | function status_pseudo_exception($cal_id, $recur_date, $filter) |
||
| 2907 | |||
| 2908 | /** |
||
| 2909 | * Check if the event is the whole day |
||
| 2910 | * |
||
| 2911 | * @param array $event event (all timestamps in servertime) |
||
| 2912 | * @return boolean true if whole day event within its timezone, false othwerwise |
||
| 2913 | */ |
||
| 2914 | function isWholeDay($event) |
||
| 2942 | |||
| 2943 | /** |
||
| 2944 | * Moves a datetime to the beginning of the day within timezone |
||
| 2945 | * |
||
| 2946 | * @param Api\DateTime $time the datetime entry |
||
| 2947 | * @param string tz_id timezone |
||
| 2948 | * |
||
| 2949 | * @return DateTime |
||
| 2950 | */ |
||
| 2951 | function &startOfDay(Api\DateTime $time, $tz_id=null) |
||
| 2967 | |||
| 2968 | /** |
||
| 2969 | * Updates the modification timestamp to force an etag, ctag and sync-token change |
||
| 2970 | * |
||
| 2971 | * @param int $id event id |
||
| 2972 | * @param int|boolean $update_master =false id of series master or true, to update series master too |
||
| 2973 | * @param int $time =null new timestamp, default current (server-)time |
||
| 2974 | * @param int $modifier =null uid of the modifier, default current user |
||
| 2975 | */ |
||
| 2976 | function updateModified($id, $update_master=false, $time=null, $modifier=null) |
||
| 2994 | } |
||
| 2995 |