Example #1
0
File: App.php Project: Hildy/cerb5
 function saveWatcherPanelAction()
 {
     $translate = DevblocksPlatform::getTranslationService();
     @($id = DevblocksPlatform::importGPC($_REQUEST['id'], 'integer', 0));
     @($active_worker = CerberusApplication::getActiveWorker());
     //	    if(!$active_worker->is_superuser)
     //	    	return;
     /*****************************/
     @($name = DevblocksPlatform::importGPC($_POST['name'], 'string', ''));
     @($is_disabled = DevblocksPlatform::importGPC($_POST['is_disabled'], 'integer', 0));
     @($worker_id = DevblocksPlatform::importGPC($_POST['worker_id'], 'integer', 0));
     @($rules = DevblocksPlatform::importGPC($_POST['rules'], 'array', array()));
     @($do = DevblocksPlatform::importGPC($_POST['do'], 'array', array()));
     if (empty($name)) {
         $name = $translate->_('Watcher Filter');
     }
     $criterion = array();
     $actions = array();
     // Custom fields
     $custom_fields = DAO_CustomField::getAll();
     // Criteria
     if (is_array($rules)) {
         foreach ($rules as $rule) {
             $rule = DevblocksPlatform::strAlphaNumDash($rule);
             @($value = DevblocksPlatform::importGPC($_POST['value_' . $rule], 'string', ''));
             // [JAS]: Allow empty $value (null/blank checking)
             $criteria = array('value' => $value);
             // Any special rule handling
             switch ($rule) {
                 case 'dayofweek':
                     // days
                     $days = DevblocksPlatform::importGPC($_REQUEST['value_dayofweek'], 'array', array());
                     if (in_array(0, $days)) {
                         $criteria['sun'] = 'Sunday';
                     }
                     if (in_array(1, $days)) {
                         $criteria['mon'] = 'Monday';
                     }
                     if (in_array(2, $days)) {
                         $criteria['tue'] = 'Tuesday';
                     }
                     if (in_array(3, $days)) {
                         $criteria['wed'] = 'Wednesday';
                     }
                     if (in_array(4, $days)) {
                         $criteria['thu'] = 'Thursday';
                     }
                     if (in_array(5, $days)) {
                         $criteria['fri'] = 'Friday';
                     }
                     if (in_array(6, $days)) {
                         $criteria['sat'] = 'Saturday';
                     }
                     unset($criteria['value']);
                     break;
                 case 'timeofday':
                     $from = DevblocksPlatform::importGPC($_REQUEST['timeofday_from'], 'string', '');
                     $to = DevblocksPlatform::importGPC($_REQUEST['timeofday_to'], 'string', '');
                     $criteria['from'] = $from;
                     $criteria['to'] = $to;
                     unset($criteria['value']);
                     break;
                 case 'event':
                     @($events = DevblocksPlatform::importGPC($_REQUEST['value_event'], 'array', array()));
                     if (is_array($events)) {
                         foreach ($events as $event) {
                             $criteria[$event] = true;
                         }
                     }
                     unset($criteria['value']);
                     break;
                 case 'groups':
                     @($groups = DevblocksPlatform::importGPC($_REQUEST['value_groups'], 'array', array()));
                     if (is_array($groups) && !empty($groups)) {
                         $criteria['groups'] = array();
                         foreach ($groups as $group_id) {
                             @($all = DevblocksPlatform::importGPC($_REQUEST['value_group' . $group_id . '_all'], 'integer', 0));
                             // Did we only want to watch specific buckets in this group?
                             $bucket_ids = array();
                             if (!$all) {
                                 @($bucket_ids = DevblocksPlatform::importGPC($_REQUEST['value_group' . $group_id . '_buckets'], 'array', array()));
                             }
                             // Add to criteria (key=group id, val=array of bucket ids)
                             $criteria['groups'][$group_id] = $bucket_ids;
                         }
                     }
                     unset($criteria['value']);
                     break;
                 case 'next_worker_id':
                     break;
                 case 'subject':
                     break;
                 case 'from':
                     break;
                     //				case 'tocc':
                     //					break;
                 //				case 'tocc':
                 //					break;
                 case 'header1':
                 case 'header2':
                 case 'header3':
                 case 'header4':
                 case 'header5':
                     if (null != @($header = DevblocksPlatform::importGPC($_POST[$rule], 'string', null))) {
                         $criteria['header'] = strtolower($header);
                     }
                     break;
                 case 'body':
                     break;
                 default:
                     // ignore invalids // [TODO] Very redundant
                     // Custom fields
                     if ("cf_" == substr($rule, 0, 3)) {
                         $field_id = intval(substr($rule, 3));
                         if (!isset($custom_fields[$field_id])) {
                             continue;
                         }
                         // [TODO] Operators
                         switch ($custom_fields[$field_id]->type) {
                             case 'S':
                                 // string
                             // string
                             case 'T':
                                 // clob
                             // clob
                             case 'U':
                                 // URL
                                 @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', 'regexp'));
                                 $criteria['oper'] = $oper;
                                 break;
                             case 'D':
                                 // dropdown
                             // dropdown
                             case 'M':
                                 // multi-dropdown
                             // multi-dropdown
                             case 'X':
                                 // multi-checkbox
                             // multi-checkbox
                             case 'W':
                                 // worker
                                 @($in_array = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id], 'array', array()));
                                 $out_array = array();
                                 // Hash key on the option for quick lookup later
                                 if (is_array($in_array)) {
                                     foreach ($in_array as $k => $v) {
                                         $out_array[$v] = $v;
                                     }
                                 }
                                 $criteria['value'] = $out_array;
                                 break;
                             case 'E':
                                 // date
                                 @($from = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_from'], 'string', '0'));
                                 @($to = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_to'], 'string', 'now'));
                                 $criteria['from'] = $from;
                                 $criteria['to'] = $to;
                                 unset($criteria['value']);
                                 break;
                             case 'N':
                                 // number
                                 @($oper = DevblocksPlatform::importGPC($_REQUEST['value_cf_' . $field_id . '_oper'], 'string', '='));
                                 $criteria['oper'] = $oper;
                                 $criteria['value'] = intval($value);
                                 break;
                             case 'C':
                                 // checkbox
                                 $criteria['value'] = intval($value);
                                 break;
                         }
                     } else {
                         continue;
                     }
                     break;
             }
             $criterion[$rule] = $criteria;
         }
     }
     // Actions
     if (is_array($do)) {
         foreach ($do as $act) {
             $action = array();
             switch ($act) {
                 // Forward a copy to...
                 case 'email':
                     @($emails = DevblocksPlatform::importGPC($_REQUEST['do_email'], 'array', array()));
                     if (!empty($emails)) {
                         $action = array('to' => $emails);
                     }
                     break;
                     // Watcher notification
                 // Watcher notification
                 case 'notify':
                     //@$emails = DevblocksPlatform::importGPC($_REQUEST['do_email'],'array',array());
                     //if(!empty($emails)) {
                     $action = array();
                     //}
                     break;
             }
             $actions[$act] = $action;
         }
     }
     $fields = array(DAO_WatcherMailFilter::NAME => $name, DAO_WatcherMailFilter::IS_DISABLED => $is_disabled, DAO_WatcherMailFilter::WORKER_ID => $worker_id, DAO_WatcherMailFilter::CRITERIA_SER => serialize($criterion), DAO_WatcherMailFilter::ACTIONS_SER => serialize($actions));
     // Create
     if (empty($id)) {
         $fields[DAO_WatcherMailFilter::POS] = 0;
         $id = DAO_WatcherMailFilter::create($fields);
         // Update
     } else {
         DAO_WatcherMailFilter::update($id, $fields);
     }
     exit;
     //DevblocksPlatform::redirect(new DevblocksHttpResponse(array('preferences','watchers')));
 }