Esempio n. 1
0
 /**
  * Read in data posted by an add/edit form
  *
  * @param  boolean	Whether to insert unknown workflows into the database. For adding this should be true, otherwise false (the default)
  * @return array		(workflow_id, workflow_name, array(approval point IDs=>names), default)
  */
 function read_in_data($insert_if_needed = false)
 {
     require_lang('workflows');
     // Grab the given name. We allow spaces, letters and numbers.
     $name = implode(' ', array_map('strip_tags', explode(' ', trim(post_param('name')))));
     // Look for an existing workflow with this name
     $workflows = get_all_workflows();
     if (in_array($name, $workflows)) {
         // Found one, use it
         $workflow_id = current(array_keys($workflows, $name));
     } elseif ($insert_if_needed) {
         // Couldn't find any. Let's make one, with a dummy approval point.
         // HACKHACK We should use a normalised table with an ID column
         $workflow_id = insert_lang($name, 3);
         $GLOBALS['SITE_DB']->query_insert('workflow_requirements', array('workflow_name' => $workflow_id, 'is_default' => 0, 'the_position' => 0, 'workflow_approval_name' => 0));
     } else {
         warn_exit(do_lang('_MISSING_RESOURCE', $name));
     }
     // Grab all of the requested points
     $points = array_map('trim', explode("\n", trim(post_param('points'))));
     // Discard whitespace
     $temp_points = array();
     foreach ($points as $p) {
         if (strlen(trim($p)) > 0) {
             $temp_points[] = trim($p);
         }
     }
     $points = $temp_points;
     unset($temp_points);
     // Clean them up a bit. We'll allow spaces, but no other punctuation.
     $clean_points = array();
     foreach ($points as $p) {
         $clean_points[] = implode(' ', array_map('strip_tags', explode(' ', $p)));
     }
     $points = $clean_points;
     unset($clean_points);
     // Find any points which are already defined
     $all_points = get_all_approval_points();
     $point_ids = array();
     foreach ($points as $p) {
         if (in_array($p, array_values($all_points))) {
             // This already exists. Use the existing version.
             $point_id = current(array_keys($all_points, $p));
             $point_ids[$point_id] = $p;
         } else {
             // This doesn't exist yet. Define it now.
             // HACKHACK We should use a normalised table with an ID field
             $point_id = insert_lang($p, 3);
             $point_ids[$point_id] = $p;
         }
         // Make sure that there are groups allowed to approve this point
         $this_key = NULL;
         foreach (array_keys($_POST) as $post_key) {
             if (strpos($post_key, 'code_') === 0) {
                 $this_key = intval(str_replace('code_', '', $post_key));
             } elseif (strpos($post_key, 'redef_') === 0) {
                 $this_key = intval(str_replace('redef_', '', $post_key));
             }
         }
         if (is_null($this_key)) {
             // If we can't find any then it may be that the browser didn't send
             // us the ticks, since the user didn't change them (notably Chrome
             // does this).
             // If this is the case then we leave existing points alone...
             $has_defaults = $GLOBALS['SITE_DB']->query_select('workflow_requirements', array('*'), array('workflow_approval_name' => $point_id));
             if (count($has_defaults) > 0) {
                 continue;
             } else {
                 // ... but this is an error is there are no existing permissions
                 // Clean up any dummies we made
                 $GLOBALS['SITE_DB']->query_delete('workflow_requirements', array('workflow_approval_name' => 0));
                 warn_exit(do_lang('WORKFLOW_POINT_MUST_HAVE_GROUP', $p));
             }
         }
         // Clear previous permissions for this approval point
         $GLOBALS['SITE_DB']->query_delete('workflow_permissions', array('workflow_approval_name' => $point_id));
         // Insert the new permissions for this approval point
         if (array_key_exists('groups_' . strval($this_key), $_POST)) {
             foreach ($_POST['groups_' . strval($this_key)] as $k => $v) {
                 $GLOBALS['SITE_DB']->query_insert('workflow_permissions', array('usergroup' => intval($k), 'workflow_approval_name' => $point_id, 'validated' => 1));
             }
         }
         if (array_key_exists('redef_groups_' . strval($this_key), $_POST)) {
             foreach ($_POST['redef_groups_' . strval($this_key)] as $k => $v) {
                 $GLOBALS['SITE_DB']->query_insert('workflow_permissions', array('usergroup' => intval($k), 'workflow_approval_name' => $point_id, 'validated' => 1));
             }
         }
     }
     // Now replace the workflow requirements. Add the new points (existing
     // instances will be replaced, so we don't need to worry about duplicates)
     $point_names = array_flip($point_ids);
     $points_we_want = array();
     foreach ($points as $p) {
         add_requirement_to_workflow($point_names[$p], $workflow_id);
         $points_we_want[] = $point_names[$p];
     }
     // Now we remove those points which are not wanted. We have to do this
     // after the insertions, since we need to keep at least one approval point
     // for the workflow in order for it to exist.
     $GLOBALS['SITE_DB']->query('DELETE FROM ' . get_table_prefix() . 'workflow_requirements WHERE workflow_name=' . strval($workflow_id) . ' AND workflow_approval_name NOT IN (' . implode(',', $points_we_want) . ')');
     return array($workflow_id, $name, $point_ids, post_param_integer('is_default', 0) == 1);
 }
Esempio n. 2
0
/**
 * Returns a form field to choose the desired workflow (if there is more than
 * one in the system).
 *
 * @param  boolean		Whether to include an option for inheriting
 * @param  boolean		Whether to include an option for leaving it alone
 * @return tempcode		The UI for choosing a workflow (if needed)
 */
function workflow_choose_ui($include_inherit = false, $include_current = false)
{
    // Grab the necessary code
    require_code('workflows');
    require_lang('workflows');
    // Find out which workflows are available
    $all_workflows = get_all_workflows();
    // Only give an option to select a workflow if there is more
    // than one available
    if (count($all_workflows) > 1) {
        // Grab the default workflow
        $def = get_default_workflow();
        $workflows = new ocp_tempcode();
        // If we've been asked to show a "current" option then add that
        if ($include_current) {
            $workflows->attach(form_input_list_entry('wf_-2', true, do_lang('KEEP_WORKFLOW'), false, false));
        }
        // If we've been asked to show an "inherit" option then add that
        if ($include_inherit) {
            $workflows->attach(form_input_list_entry('wf_-1', !$include_current, do_lang('INHERIT_WORKFLOW'), false, false));
        }
        // Get all of the workflows we have
        foreach ($all_workflows as $id => $workflow) {
            $workflows->attach(form_input_list_entry('wf_' . strval($id), !$include_inherit && !$include_current && $id == $def, $workflow, false, false));
        }
        // Return a list entry to choose from
        $help = $include_inherit ? do_lang('INHERIT_WORKFLOW_HELP') : '';
        $help .= $include_current ? do_lang('CURRENT_WORKFLOW_HELP') : '';
        return form_input_list(do_lang_tempcode('USE_WORKFLOW'), do_lang_tempcode('USE_WORKFLOW_DESCRIPTION', $help), 'workflow', $workflows, NULL, false);
    } elseif (count($all_workflows) == 1) {
        return form_input_hidden('workflow', 'wf_' . current(array_keys($all_workflows)));
    } else {
        return new ocp_tempcode();
    }
}