Advertisement
Guest User

tiki trunk (r28400) lib/wiki-plugins/wikiplugin_tracker.php

a guest
Aug 13th, 2010
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 50.95 KB | None | 0 0
  1. <?php
  2. // (c) Copyright 2002-2010 by authors of the Tiki Wiki/CMS/Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id: wikiplugin_tracker.php 28400 2010-08-09 13:21:49Z jonnybradley $
  7.  
  8. // Includes a tracker field
  9. // Usage:
  10. // {TRACKER()}{TRACKER}
  11.  
  12. function wikiplugin_tracker_info()
  13. {
  14.     return array(
  15.         'name' => tra('Tracker'),
  16.         'documentation' => 'PluginTracker',
  17.         'description' => tra("Displays an input form for tracker submit"),
  18.         'prefs' => array( 'feature_trackers', 'wikiplugin_tracker' ),
  19.         'body' => tra('Confirmation message after posting form'),
  20.         'icon' => 'pics/icons/database.png',
  21.         'params' => array(
  22.             'trackerId' => array(
  23.                 'required' => true,
  24.                 'name' => tra('Tracker ID'),
  25.                 'description' => tra('Tracker ID'),
  26.                 'filter' => 'digits'
  27.             ),
  28.             'fields' => array(
  29.                 'required' => true,
  30.                 'name' => tra('Fields'),
  31.                 'description' => tra('Colon-separated list of field IDs to be displayed. Example: 2:4:5'),
  32.             ),
  33.             'action' => array(
  34.                 'required' => false,
  35.                 'name' => tra('Action'),
  36.                 'description' => tra('Label on the submit button'),
  37.                 'separator' => ':',
  38.             ),
  39.             'showtitle' => array(
  40.                 'required' => false,
  41.                 'name' => tra('Show Title'),
  42.                 'description' => 'y|n' . ' ' . tra('Default=n'),
  43.                 'filter' => 'alpha'
  44.             ),
  45.             'showdesc' => array(
  46.                 'required' => false,
  47.                 'name' => tra('Show Description'),
  48.                 'description' => 'y|n' . ' ' . tra('Default=n'),
  49.                 'filter' => 'alpha'
  50.             ),
  51.             'showmandatory' => array(
  52.                 'required' => false,
  53.                 'name' => tra('Show Mandatory'),
  54.                 'description' => 'y|n' . ' ' . tra('Default=y'),
  55.                 'filter' => 'alpha'
  56.             ),
  57.             'showstatus' => array(
  58.                 'required' => false,
  59.                 'name' => tra('Show Status'),
  60.                 'description' => 'y|n' . ' ' . tra('Default=n'),
  61.                 'filter' => 'alpha'
  62.             ),
  63.             'embedded' => array(
  64.                 'required' => false,
  65.                 'name' => tra('Embedded'),
  66.                 'description' => 'y|n' . ' ' . tra('Default=n'),
  67.                 'filter' => 'alpha'
  68.             ),
  69.             'email' => array(
  70.                 'required' => false,
  71.                 'name' => tra('Email'),
  72.                 'description' => tra('from').'|'.tra('to').'|'.tra('template'),
  73.             ),
  74.             'url' => array(
  75.                 'required' => false,
  76.                 'name' => tra('URL'),
  77.                 'description' => tra('URL used for the field links'),
  78.                 'filter' => 'url',
  79.                 'separator' => ':',
  80.             ),
  81.             'target' => array(
  82.                 'required' => false,
  83.                 'name' => '_blank|_self|_parent|_top',
  84.                 'description' => tra('Url target'),
  85.             ),
  86.             'values' => array(
  87.                 'required' => false,
  88.                 'name' => tra('Values'),
  89.                 'description' => tra('Colon-separated list of values.').' '.tra('Note that plugin arguments can be enclosed with double quotes "; this allows them to contain , or :'),
  90.             ),
  91.             'overwrite' => array(
  92.                 'required' => false,
  93.                 'name' => tra('Overwrite the fields with values'),
  94.                 'description' => 'y|n '.tra('Overwrite the existing field values existing or not with the param values'),
  95.                 'filter' => 'alpha',
  96.                 'default' => 'n'
  97.             ),
  98.             'sort' => array(
  99.                 'required' => false,
  100.                 'name' => tra('Sort'),
  101.                 'description' => 'y|n',
  102.                 'filter' => 'alpha'
  103.             ),
  104.             'preview' => array(
  105.                 'required' => false,
  106.                 'name' => tra('Preview'),
  107.                 'description' => tra('Label for the preview button. Default:').' "'. tra('Preview') . '"',
  108.             ),
  109.             'reset' => array(
  110.                 'required' => false,
  111.                 'name' => tra('Reset'),
  112.                 'description' => tra('Label for the reset button, to return all fields to their default values.'),
  113.             ),
  114.             'view' => array(
  115.                 'required' => false,
  116.                 'name' => tra('View'),
  117.                 'description' => tra('user|page'),
  118.             ),
  119.             'status' => array(
  120.                 'required' => false,
  121.                 'name' => tra('Status'),
  122.                 'description' => tra('Status of the item used in combination with:').' view=user',
  123.             ),
  124.             'itemId' =>array(
  125.                 'required' => false,
  126.                 'name' => tra('itemId'),
  127.                 'description' => tra('itemId if you want to edit an item'),
  128.                 'filter' => 'digits'
  129.             ),
  130.             'ignoreRequestItemId' => array(
  131.                 'required' => false,
  132.                 'name' => tra('Do not filter on the param itemId if in the url'),
  133.                 'description' => 'y|n',
  134.                 'filter' => 'alpha',
  135.                 'default' => 'n',
  136.             ),
  137.             'tpl' => array(
  138.                 'required' => false,
  139.                 'name' => tra('Template File'),
  140.                 'description' => tra('Name of the template used to display the tracker items.'),
  141.             ),
  142.             'wiki' => array(
  143.                 'required' => false,
  144.                 'name' => tra('Wiki'),
  145.                 'description' => tra('Name of the wiki page containing the template to display the tracker items.'),
  146.                 'filter' => 'pagename'
  147.             ),
  148.             'newstatus' => array(
  149.                 'required' => false,
  150.                 'name' => tra('New Status'),
  151.                 'description' => 'o|p|c'. ' '.tra('Default status applied to newly created items.'),
  152.                 'filter' => 'alpha'
  153.             ),
  154.             'colwidth' => array(
  155.                 'required' => false,
  156.                 'name' => tra('Width of first column '),
  157.                 'description' => '## or ##% '. ' '.tra('Specify the width in pixels or percentage of the first column in the tracker form.'),
  158.             ),
  159.             'autosavefields' => array(
  160.                 'required' => false,
  161.                 'name' => tra('Autosave fields'),
  162.                 'description' => tra('Colon-separated list of field IDs to be automatically filled with values'),
  163.                 'filter' => 'digits',
  164.                 'separator' => ':'
  165.             ),
  166.             'autosavevalues' => array(
  167.                 'required' => false,
  168.                 'name' => tra('Autosavevalue'),
  169.                 'description' => tra('Colon-separated values corresponding to autosavefields'),
  170.                 'filter' => 'text',
  171.                 'separator' => ':'
  172.             ),
  173.             'registration' => array(
  174.                 'required' => false,
  175.                 'name' => tra('Add registration fields'),
  176.                 'description' => tra('y|n Add registration fields such as Username and Password'),
  177.                 'filter' => 'alpha'
  178.             ),
  179.             'outputtowiki' => array(
  180.                 'required' => false,
  181.                 'name' => tra('Output to wiki using fieldId'),
  182.                 'description' => tra('Output result to a new wiki page with the name taken from the input for the specified fieldId'),
  183.                 'filter' => 'digits'
  184.             ),
  185.             'discarditem' => array(
  186.                 'required' => false,
  187.                 'name' => tra('Discard tracker item after wiki output'),
  188.                 'description' => tra('y|n Used when results are output to a wiki page to discard the tracker item itself once the wiki page is created'),
  189.                 'filter' => 'alpha'
  190.             ),
  191.             'outputwiki' => array(
  192.                 'required' => false,
  193.                 'name' => tra('Wiki template for wiki output'),
  194.                 'description' => tra('Name of the wiki page containing the template to format the output to wiki page'),
  195.                 'filter' => 'pagename'
  196.             ),
  197.         ),
  198.     );
  199. }
  200.  
  201. function wikiplugin_tracker_name($fieldId, $name, $field_errors)
  202. {
  203.     foreach($field_errors['err_mandatory'] as $f) {
  204.         if ($fieldId == $f['fieldId'])
  205.             return '<span class="highlight">'.$name.'</span>';
  206.     }
  207.     foreach($field_errors['err_value'] as $f) {
  208.         if ($fieldId == $f['fieldId'])
  209.             return '<span class="highlight">'.$name.'</span>';
  210.     }
  211.     return $name;
  212. }
  213.  
  214. function wikiplugin_tracker($data, $params)
  215. {
  216.     global $tikilib, $userlib, $dbTiki, $user, $group, $page, $tiki_p_admin_trackers, $smarty, $prefs, $trklib, $tiki_p_view, $captchalib;
  217.     static $iTRACKER = 0;
  218.     ++$iTRACKER;
  219.     include_once('lib/trackers/trackerlib.php');
  220.     $default = array('overwrite' => 'n', 'embedded' => 'n', 'showtitle' => 'n', 'showdesc' => 'n', 'sort' => 'n', 'showmandatory'=>'y', 'status' => '', 'registration' => 'n');
  221.     $params = array_merge($default, $params);
  222.    
  223.     //var_dump($_REQUEST);
  224.     extract ($params, EXTR_SKIP);
  225.  
  226.     if ($prefs['feature_trackers'] != 'y') {
  227.         return $smarty->fetch("wiki-plugins/error_tracker.tpl");
  228.     }
  229.     if (empty($trackerId) || !($tracker = $trklib->get_tracker($trackerId))) {
  230.         return $smarty->fetch("wiki-plugins/error_tracker.tpl");
  231.     }
  232.     if ($t = $trklib->get_tracker_options($trackerId)) {
  233.         $tracker = array_merge($tracker, $t);
  234.     }
  235.     if (empty($trackerId) && !empty($view) && $view == 'user' && $prefs['userTracker'] == 'y') { // the user tracker item
  236.         $utid = $userlib->get_tracker_usergroup($user);
  237.         if (!empty($utid) && !empty($utid['usersTrackerId'])) {
  238.             $itemId = $trklib->get_item_id($utid['usersTrackerId'], $utid['usersFieldId'], $user);
  239.             $trackerId = $utid['usersTrackerId'];
  240.             $usertracker = true;
  241.         }
  242.     } elseif (!empty($trackerId) && !empty($view) && $view == 'user') {// the user item of a tracker
  243.         $itemId = $trklib->get_user_item($trackerId, $tracker, null, null, strlen($status) == 1 ? $status : '');
  244.         $usertracker = true;
  245.     } elseif (!empty($trackerId) && !empty($view) && $view == 'page' && !empty($_REQUEST['page']) && (($f = $trklib->get_field_id_from_type($trackerId, 'k', '1%')) || ($f = $trklib->get_field_id_from_type($trackerId, 'k', '%,1%')) || ($f =  $trklib->get_field_id_from_type($trackerId, 'k')))) {// the page item
  246.         $itemId = $trklib->get_item_id($trackerId, $f, $_REQUEST['page']);
  247.     } elseif (!empty($trackerId) && !empty($_REQUEST['view_user'])) {
  248.         $itemId = $trklib->get_user_item($trackerId, $tracker, $_REQUEST['view_user']);
  249.     } elseif (!empty($_REQUEST['itemId']) && (empty($ignoreRequestItemId) || $ignoreRequestItemId != 'y')) {
  250.         $itemId = $_REQUEST['itemId'];
  251.         $item = $trklib->get_tracker_item($itemId);
  252.         $trackerId = $item['trackerId'];
  253.     } elseif (!empty($view) && $view == 'group') {
  254.         $gtid = $userlib->get_grouptrackerid($group);
  255.         if(isset($gtid['groupTrackerId'])) {
  256.             $trackerId = $gtid['groupTrackerId'];
  257.             $itemId = $trklib->get_item_id($trackerId, $gtid['groupFieldId'], $group);
  258.             $grouptracker = true;
  259.         }
  260.     }
  261.     if (!isset($trackerId)) {
  262.         return $smarty->fetch("wiki-plugins/error_tracker.tpl");
  263.     }
  264.  
  265.     if (!isset($action)) {
  266.         $action = array('Save');
  267.     }
  268.     if (!is_array($action)) {
  269.         $action = array( $action );
  270.     }
  271.     if (isset($preview)) {
  272.         if (empty($preview)) {
  273.             $preview = 'Preview';
  274.         }
  275.     } else {
  276.         unset($_REQUEST['tr_preview']);
  277.     }
  278.     if (isset($reset)) {
  279.         if (empty($reset)) {
  280.             $reset = 'reset';
  281.         }
  282.     } else {
  283.         unset($_REQUEST['tr_reset']);
  284.     }
  285.     $smarty->assign('showmandatory',  empty($wiki) && empty($tpl)? 'n': $showmandatory);
  286.     if (!empty($wiki)) $wiki = trim($wiki);
  287.  
  288.     if (isset($values)) {
  289.         if (!is_array($values)) {
  290.             $values = $tikilib->quotesplit(':', $values);
  291.             foreach ($values as $i=>$v) {
  292.                 $values[$i] = preg_replace('/^"(.*)"$/', '$1', $v);
  293.             }
  294.         }
  295.     }
  296.     if (isset($_REQUEST['values'])) {
  297.         if (is_array($_REQUEST['values'])) {
  298.             foreach ($_REQUEST['values'] as $i=>$k) {
  299.                 $_REQUEST['values'][$i] = urldecode($k);
  300.             }
  301.         } else {
  302.             $_REQUEST['values'] = urldecode($_REQUEST['values']);
  303.         }
  304.     }
  305.  
  306.     $perms = $tikilib->get_perm_object($trackerId, 'tracker', $tracker, false);
  307.    
  308.     if (empty($_SERVER['SCRIPT_NAME']) || strpos($_SERVER['SCRIPT_NAME'], 'tiki-register.php') === false) {
  309.         if (!empty($itemId) && $tracker['writerCanModify'] == 'y' && isset($usertracker) && $usertracker) { // user tracker he can modify
  310.         } elseif (!empty($itemId) && $tracker['writerCanModify'] == 'y' && $user && (($itemUser = $trklib->get_item_creator($trackerId, $itemId)) == $user || ($tracker['userCanTakeOwnership'] == 'y' && empty($itemUser)))) {
  311.         } elseif (!empty($itemId) && isset($grouptracker) && $grouptracker) {
  312.         } else {
  313.             if ($perms['tiki_p_create_tracker_items'] == 'n' && empty($itemId)) {
  314.                 return '<b>'.tra("You do not have permission to insert an item").'</b>';
  315.             } elseif (!empty($itemId)) {
  316.                 $item_info = $trklib->get_tracker_item($itemId);
  317.                 if (!(($perms['tiki_p_modify_tracker_items'] == 'y' and $item_info['status'] != 'p' and $item_info['status'] != 'c') || ($perms['tiki_p_modify_tracker_items_pending'] == 'y' and $item_info['status'] == 'p') ||  ($perms['tiki_p_modify_tracker_items_closed'] == 'y' and $item_info['status'] == 'c'))) {
  318.                     if ($tracker['writerGroupCanModify'] == 'y' && in_array($trklib->get_item_group_creator($trackerId, $itemId), $tikilib->get_user_groups($user))) {
  319.                         global $group;
  320.                         $smarty->assign_by_ref('ours', $group);
  321.                     } else
  322.                         return '<b>'.tra("You do not have permission to modify an item").'</b>';
  323.                 }
  324.             }
  325.         }
  326.     }
  327.     if (!empty($itemId)) {
  328.         global $logslib; include_once('lib/logs/logslib.php');
  329.         $logslib->add_action('Viewed', $itemId, 'trackeritem', $_SERVER['REQUEST_URI']);
  330.     }
  331.  
  332.     if (isset($_REQUEST['removeattach']) && $tracker['useAttachments'] == 'y') {
  333.         $owner = $trklib->get_item_attachment_owner($_REQUEST['removeattach']);
  334.         if ($perms['tiki_p_admin_trackers'] == 'y' || ($user && $user == $owner)) {
  335.             $trklib->remove_item_attachment($_REQUEST["removeattach"]);
  336.             unset($_REQUEST['removeattach']);
  337.         }
  338.     }
  339.     if (isset($_REQUEST['removeImage']) && !empty($_REQUEST['trackerId']) && !empty($_REQUEST['itemId']) && !empty($_REQUEST['fieldId']) && !empty($_REQUEST['fieldName'])) {
  340.         $img_field = array('data' => array());
  341.         $img_field['data'][] = array('fieldId' => $_REQUEST['fieldId'], 'type' => 'i', 'name' => $_REQUEST['fieldName'], 'value' => 'blank');
  342.         $trklib->replace_item($_REQUEST['trackerId'], $_REQUEST['itemId'], $img_field);
  343.     }
  344.     $back = '';
  345.     $js = '';
  346.  
  347.     $thisIsThePlugin = isset($_REQUEST['iTRACKER']) && $_REQUEST['iTRACKER'] == $iTRACKER;
  348.  
  349.     if (!isset($_REQUEST["ok"]) || $_REQUEST["ok"]  == "n" || !$thisIsThePlugin || isset($_REQUEST['tr_preview'])) {
  350.         $field_errors = array('err_mandatory'=>array(), 'err_value'=>array());
  351.    
  352.             global $notificationlib; include_once('lib/notifications/notificationlib.php');
  353.             $tracker = $trklib->get_tracker($trackerId);
  354.             $tracker = array_merge($tracker, $trklib->get_tracker_options($trackerId));
  355.             if ((!empty($tracker['start']) && $tikilib->now < $tracker['start']) || (!empty($tracker['end']) && $tikilib->now > $tracker['end']))
  356.                 return;
  357.             $outf = array();
  358.             if (!empty($fields)  || !empty($wiki) || !empty($tpl)) {
  359.                 if ($registration == 'y' && $prefs["user_register_prettytracker"] == 'y' && !empty($prefs["user_register_prettytracker_tpl"])) {
  360.                     $smarty->assign('register_login', $smarty->fetch('register-login.tpl'));
  361.                     $smarty->assign('register_email', $smarty->fetch('register-email.tpl'));
  362.                     $smarty->assign('register_pass', $smarty->fetch('register-pass.tpl'));
  363.                     $smarty->assign('register_pass2', $smarty->fetch('register-pass2.tpl'));
  364.                     $smarty->assign('register_passcode', $smarty->fetch('register-passcode.tpl'));
  365.                     $smarty->assign('register_groupchoice', $smarty->fetch('register-groupchoice.tpl'));
  366.                 }
  367.                 if (!empty($fields)) {
  368.                     $outf = preg_split('/ *: */', $fields);$fields;
  369.                 } elseif (!empty($wiki)) {
  370.                     $outf = $trklib->get_pretty_fieldIds($wiki, 'wiki');
  371.                 } else {
  372.                     $outf = $trklib->get_pretty_fieldIds($tpl, 'tpl');
  373.                 }
  374.                 if (!empty($autosavefields)) {
  375.                     $outf = array_merge($outf, $autosavefields);
  376.                 }
  377.             $outf = array_merge($outf, $trklib->get_field_id_from_type($trackerId, array('q', 'k', 'u', 'g', 'I', 'C', 'n', 'j', 'f'), '', false));
  378.                 //q=auto-increment, k=page selector, u=user selector, g=group selector, I=Ip selector, C=computed, n=numeric, j=calendar, f=date
  379.             }
  380.             $flds = $trklib->list_tracker_fields($trackerId, 0, -1, 'position_asc', '', true, '', $outf);
  381.             $bad = array();
  382.             $embeddedId = false;
  383.             $onemandatory = false;
  384.             $full_fields = array();
  385.             $mainfield = '';
  386.  
  387.             if ($thisIsThePlugin) {
  388.                 /* ------------------------------------- Recup all values from REQUEST -------------- */
  389.                 foreach ($flds['data'] as $fl) {
  390.                     // First convert track_xx to array (need to be up here before setting autosave fields)
  391.                     $incomingTrackName = 'track_' . $fl["fieldId"];
  392.                     if (isset($_REQUEST[$incomingTrackName])) {
  393.                         $_REQUEST['track'][$fl["fieldId"]] = $_REQUEST[$incomingTrackName];
  394.                     } else if (isset($_FILES[$incomingTrackName])) {    // also for uploaded files
  395.                         foreach ($_FILES[$incomingTrackName] as $lbl => $val) {
  396.                             $_FILES['track'][$lbl][$fl["fieldId"]] = $val;
  397.                         }
  398.                     }
  399.                 }
  400.                 if (!empty($_REQUEST['autosavefields'])) {
  401.                     $autosavefields = explode(':', $_REQUEST['autosavefields']);
  402.                     $autosavevalues = explode(':', $_REQUEST['autosavevalues']);
  403.                     if (isset($params['autosavefields'])) {
  404.                         $autosavefields = array_merge($autosavefields, $params['autosavefields']);
  405.                         $autosavevalues = array_merge($autosavevalues, $params['autosavevalues']);
  406.                     }
  407.                 }
  408.                 if (!empty($autosavefields)) {
  409.                     foreach ($autosavefields as $i=>$f) {
  410.                         if (!$ff = $trklib->get_field($f, $flds['data'])) {
  411.                             continue;
  412.                         }
  413.                         if (!$trklib->fieldId_is_editable($ff, $item_info)) {
  414.                             continue;
  415.                         }
  416.                         if (preg_match('/categories\(([0-9]+)\)/', $autosavevalues[$i], $matches)) {
  417.                             global $categlib; include_once('lib/categories/categlib.php');
  418.                             $categs = $categlib->list_categs($matches[1]);
  419.                             $_REQUEST["ins_cat_$f"][] = $categs[0]['categId'];
  420.                         } elseif (preg_match('/preference\((.*)\)/', $autosavevalues[$i], $matches)) {
  421.                             $_REQUEST["$ins_id_$f"] = $prefs[$matches[1]];
  422.                         } elseif ($ff['type'] == 'e') {
  423.                             $_REQUEST["ins_cat_$f"][] = $autosavevalues[$i];
  424.                         } else {
  425.                             $_REQUEST['track'][$f] = $autosavevalues[$i];
  426.                         }
  427.                     }
  428.                 }
  429.                 $cpt = 0;
  430.                 if (isset($fields)) {
  431.                     $fields_plugin = preg_split('/:/', $fields);
  432.                 }
  433.                 foreach ($flds['data'] as $fl) {
  434.                     // store value to display it later if form
  435.                     // isn't fully filled.
  436.                     if ($flds['data'][$cpt]['type'] == 's' && $flds['data'][$cpt]['name'] == 'Rating') {
  437.                         if (isset($_REQUEST['track'][$fl['fieldId']])) {
  438.                             $newItemRate = $_REQUEST['track'][$fl['fieldId']];
  439.                             $newItemRateField = $fl['fieldId'];
  440.                         } else {
  441.                             $newItemRate = NULL;
  442.                         }
  443.                    
  444.                     } elseif ($flds["data"][$cpt]["type"] == 'c') {
  445.                         if (!isset($_REQUEST['track'][$fl['fieldId']])) {
  446.                             $_REQUEST['track'][$fl['fieldId']] = 'n';
  447.                         }  
  448.                     } elseif (($flds['data'][$cpt]['type'] == 'u' || $flds['data'][$cpt]['type'] == 'g' || $flds['data'][$cpt]['type'] == 'I' || $flds['data'][$cpt]['type'] == 'k') && // user/group/ip
  449.                               ($flds['data'][$cpt]['options_array'][0] == '1' || $flds['data'][$cpt]['options_array'][0] == '2') && // create or modif
  450.                               ($tiki_p_admin_trackers != 'y' || (!isset($_REQUEST['track'][$fl['fieldId']]) && in_array($fl['fieldId'], $fields_plugin)))) {                        // but admins can override
  451.                        
  452.                         if (empty($itemId) && ($flds['data'][$cpt]['options_array'][0] == '1' || $flds['data'][$cpt]['options_array'][0] == '2')) {
  453.                             if ($flds['data'][$cpt]['type'] == 'u') {
  454.                                 $_REQUEST['track'][$fl['fieldId']] = empty($user)?(empty($_REQUEST['name'])? '':$_REQUEST['name']):$user;
  455.                             } elseif ($flds['data'][$cpt]['type'] == 'g') {
  456.                                 $_REQUEST['track'][$fl['fieldId']] = $group;
  457.                             } elseif ($flds['data'][$cpt]['type'] == 'I') {
  458.                                 $_REQUEST['track'][$fl['fieldId']] = $tikilib->get_ip_address();
  459.                             } elseif ($flds['data'][$cpt]['type'] == 'k') {
  460.                                 $_REQUEST['track'][$fl['fieldId']] = isset($_REQUEST['page'])?$_REQUEST['page']: '';
  461.                             }
  462.                         } elseif (!empty($itemId) && $flds['data'][$cpt]['options_array'][0] == '2') {
  463.                             if ($flds['data'][$cpt]['type'] == 'u')
  464.                                 $_REQUEST['track'][$fl['fieldId']] = $user;
  465.                             elseif ($flds['data'][$cpt]['type'] == 'g')
  466.                                 $_REQUEST['track'][$fl['fieldId']] = $group;
  467.                             elseif ($flds['data'][$cpt]['type'] == 'I')
  468.                                 $_REQUEST['track'][$fl['fieldId']] = $tikilib->get_ip_address();
  469.                         }
  470.                     } elseif (($flds['data'][$cpt]['type'] == 'C' || $flds['data'][$cpt]['type'] == 'e') && empty($_REQUEST['track'][$fl['fieldId']])) {
  471.                         $_REQUEST['track'][$fl['fieldId']] = '';
  472.                     } elseif ($flds['data'][$cpt]['type'] == 'f') {
  473.                         $ins_id = 'track_'.$fl['fieldId'];
  474.                         if (isset($_REQUEST[$ins_id.'Day']) || isset($_REQUEST[$ins_id.'Hour'])) {
  475.                             $_REQUEST['track'][$fl['fieldId']] = $trklib->build_date($_REQUEST, $flds['data'][$cpt], $ins_id);
  476.                         } else {
  477.                             $_REQUEST['track'][$fl['fieldId']] = $tikilib->now;
  478.                         }
  479.                     } elseif ($f['type'] == 'N' && !empty($itemId)) {
  480.                         if (empty($itemUser)) {
  481.                             $itemUser = $this->get_item_creator($trackerId, $itemId);
  482.                         }
  483.                         $flds['data'][$i]['value'] = $trklib->in_group_value($flds['data'][$i], $itemUser);
  484.                     }
  485.                     if (isset($_REQUEST['ins_cat_'.$fl['fieldId']])) { // to remember if error
  486.                         $_REQUEST['track'][$fl['fieldId']] = $_REQUEST['ins_cat_'.$fl['fieldId']];
  487.                     }
  488.  
  489.                     if(isset($_REQUEST['track'][$fl['fieldId']])) {
  490.                         $flds['data'][$cpt]['value'] = $_REQUEST['track'][$fl['fieldId']];
  491.                     } else {
  492.                         $flds['data'][$cpt]['value'] = '';
  493.                         if (empty($itemId)) {
  494.                             if ($fl['type'] == 'c') {
  495.                                 $_REQUEST['track'][$fl['fieldId']] = 'n';
  496.                             } elseif ($fl['type'] == 'R' && $fl['isMandatory'] == 'y') {
  497.                                 // if none radio is selected, there will be no value and no error if mandatory
  498.                                 $_REQUEST['track'][$fl['fieldId']] = '';
  499.                             }
  500.                         }
  501.                     }
  502.                     if (!empty($_REQUEST['other_track'][$fl['fieldId']])) {
  503.                         $flds['data'][$cpt]['value'] = $_REQUEST['other_track'][$fl['fieldId']];
  504.                     }
  505.                     if ($flds['data'][$cpt]['isMultilingual'] == 'y') {
  506.                         foreach ($prefs['available_languages'] as $num=>$tmplang) {
  507.                             if (isset($_REQUEST['track'][$fl['fieldId']][$tmplang])) {
  508.                                 $fl['lingualvalue'][$num]['value'] = $_REQUEST['track'][$fl['fieldId']][$tmplang];
  509.                                 $fl['lingualvalue'][$num]['lang'] = $tmplang;
  510.                             }
  511.                         }
  512.                     }
  513.                     $full_fields[$fl['fieldId']] = $fl;
  514.                    
  515.                     if ($embedded == 'y' and $fl['name'] == 'page') {
  516.                         $embeddedId = $fl['fieldId'];
  517.                     }
  518.                     if ($fl['isMain'] == 'y')
  519.                         $mainfield = $flds['data'][$cpt]['value'];
  520.                     $cpt++;
  521.                 } /*foreach */
  522.  
  523.                 if (isset($_REQUEST['track'])) {
  524.                     foreach ($_REQUEST['track'] as $fld=>$val) {
  525.                         //$ins_fields["data"][] = array('fieldId' => $fld, 'value' => $val, 'type' => 1);
  526.                         if (!empty($_REQUEST['other_track'][$fld])) {
  527.                             $val = $_REQUEST['other_track'][$fld];
  528.                         }
  529.                         $ins_fields["data"][] = array_merge(array('value' => $val), $full_fields[$fld]);
  530.                     }
  531.                 }
  532.  
  533.                 if (isset($_FILES['track'])) {// image or attachment fields
  534.                     foreach ($_FILES['track'] as $label=>$w) {
  535.                         foreach ($w as $fld=>$val) {
  536.                             if ($label == 'tmp_name' && is_uploaded_file($val)) {
  537.                                 $fp = fopen( $val, 'rb' );
  538.                                 $data = '';
  539.                                 while (!feof($fp)) {
  540.                                     $data .= fread($fp, 8192 * 16);
  541.                                 }
  542.                                 fclose ($fp);
  543.                                 if (!empty($itemId) && $fl['type'] == 'A') {
  544.                                     $files[$fld]['old_value'] = $trklib->get_item_value($trackerId, $itemId, $fld);
  545.                                 }
  546.                                 $files[$fld]['value'] = $data;
  547.                             } else {
  548.                                 $files[$fld]['file_'.$label] = $val;
  549.                             }
  550.                         }
  551.                     }
  552.                     foreach ($files as $fld=>$file) {
  553.                         $ins_fields['data'][] = array_merge($file, $full_fields[$fld]);
  554.                     }
  555.                 }
  556.  
  557.                 if ($embedded == 'y' && isset($_REQUEST['page'])) {
  558.                     $ins_fields["data"][] = array('fieldId' => $embeddedId, 'value' => $_REQUEST['page']);
  559.                 }
  560.                 $ins_categs = array();
  561.                 $categorized_fields = array();
  562.                 while (list($postVar, $postVal) = each($_REQUEST)) {
  563.                     if(preg_match("/^ins_cat_([0-9]+)/", $postVar, $m)) {
  564.                         foreach ($postVal as $v) {
  565.                             $ins_categs[] = $v;
  566.                             $categorized_fields[] = $m[1];
  567.                         }
  568.                     }
  569.                 }
  570.                 /* ------------------------------------- End recup all values from REQUEST -------------- */
  571.  
  572.                 /* ------------------------------------- Check field values for each type and presence of mandatory ones ------------------- */
  573.                 $field_errors = $trklib->check_field_values($ins_fields, $categorized_fields, $trackerId, empty($itemId)?'':$itemId);
  574.  
  575.                 if (empty($user) && $prefs['feature_antibot'] == 'y' && $registration != 'y') {
  576.                     // in_tracker session var checking is for tiki-register.php
  577.                     if (!$captchalib->validate()) {
  578.                         $field_errors['err_antibot'] = 'y';
  579.                     }
  580.                 }
  581.  
  582.                 // check valid page name for wiki output if requested
  583.                 if (isset($outputtowiki) && !empty($outputwiki)) {
  584.                     $newpagename = '';
  585.                     foreach ($ins_fields["data"] as $fl) {
  586.                         if ($fl["fieldId"] == $outputtowiki) {
  587.                             $newpagename = $fl["value"];
  588.                         }
  589.                         if ($fl["type"] == 'F') {
  590.                             $newpagefreetags = $fl["value"];
  591.                         }
  592.                         $newpagefields[] = $fl["fieldId"];
  593.                     }
  594.                     if ($newpagename) {
  595.                         if ($tikilib->page_exists($newpagename)) {
  596.                             $field_errors['err_outputwiki'] = tra('The page to output the results to already exists. Try another name.');
  597.                         }
  598.                         $page_badchars_display = ":/?#[]@!$&'()*+,;=<>";
  599.                         $page_badchars = "/[:\/?#\[\]@!$&'()*+,;=<>]/";
  600.                         $matches = preg_match($page_badchars, $newpagename);
  601.                         if ($matches) {
  602.                             $field_errors['err_outputwiki'] = tra("The page to output the results to contains the following prohibited characters: $page_badchars_display. Try another name.");
  603.                         }
  604.                     } else {
  605.                         unset($outputtowiki);
  606.                     }
  607.                 }
  608.                 if( count($field_errors['err_mandatory']) == 0  && count($field_errors['err_value']) == 0 && empty($field_errors['err_antibot']) && empty($field_errors['err_outputwiki']) && !isset($_REQUEST['tr_preview'])) {
  609.                     /* ------------------------------------- save the item ---------------------------------- */
  610.                     if (!isset($itemId) && $tracker['oneUserItem'] == 'y') {
  611.                         $itemId = $trklib->get_user_item($trackerId, $tracker);
  612.                     }
  613.                     if (isset($_REQUEST['status'])) {
  614.                         $status = $_REQUEST['status'];
  615.                     } elseif (isset($newstatus) && ($newstatus == 'o' || $newstatus == 'c'|| $newstatus == 'p')) {
  616.                         $status = $newstatus;
  617.                     } elseif (empty($itemId) && isset($tracker['newItemStatus'])) {
  618.                         $status = $tracker['newItemStatus'];
  619.                     } else {
  620.                         $status = '';
  621.                     }
  622.                     $rid = $trklib->replace_item($trackerId, $itemId, $ins_fields, $status, $ins_categs);
  623.                     $trklib->categorized_item($trackerId, $rid, $mainfield, $ins_categs);
  624.                     if (isset($newItemRate)) {
  625.                         $trklib->replace_rating($trackerId, $rid, $newItemRateField, $user, $newItemRate);
  626.                     }
  627.                     // now for wiki output if desired
  628.                     if (isset($outputtowiki) && !empty($outputwiki)) {
  629.                         // note that values will be raw - that is the limit of the capability of this feature for now
  630.                         $newpageinfo = $tikilib->get_page_info($outputwiki);
  631.                         $wikioutput = $newpageinfo["data"];
  632.                         $newpagefields = $trklib->get_pretty_fieldIds($outputwiki, 'wiki');
  633.                         foreach($newpagefields as $lf) {
  634.                             $wikioutput = str_replace('{$f_' . $lf . '}', $trklib->get_item_value($trackerId, $rid, $lf), $wikioutput);
  635.                         }
  636.                         $tikilib->create_page($newpagename, 0, $wikioutput, $tikilib->now, '', $user, $tikilib->get_ip_address());
  637.                         $cat_desc = '';
  638.                         $cat_type = 'wiki page';
  639.                         $cat_name = $newpagename;
  640.                         $cat_objid = $newpagename;
  641.                         $cat_href = "tiki-index.php?page=".urlencode($newpagename);
  642.                         if (count($ins_categs)) {
  643.                             $_REQUEST['cat_categories'] = $ins_categs;
  644.                             $_REQUEST['cat_categorize'] = 'on';
  645.                             include_once("categorize.php");
  646.                         }
  647.                         if (isset($newpagefreetags) && $newpagefreetags) {
  648.                             $_REQUEST['freetag_string'] = $newpagefreetags;
  649.                             include_once("freetag_apply.php");
  650.                         }
  651.                         if ($discarditem == 'y') {
  652.                             $trklib->remove_tracker_item($rid);
  653.                         }
  654.                         if (empty($url)) {
  655.                             global $wikilib;
  656.                             $url[0] = $wikilib->sefurl($newpagename);
  657.                         }
  658.                     }
  659.                     // end wiki output
  660.                     if (!empty($email)) {
  661.                         $emailOptions = preg_split("#\|#", $email);
  662.                         if (is_numeric($emailOptions[0])) {
  663.                             $emailOptions[0] = $trklib->get_item_value($trackerId, $rid, $emailOptions[0]);
  664.                         }
  665.                         if (empty($emailOptions[0])) { // from
  666.                             $emailOptions[0] = $prefs['sender_email'];
  667.                         }
  668.                         if (empty($emailOptions[1])) { // to
  669.                             $emailOptions[1][0] = $prefs['sender_email'];
  670.                         } else {
  671.                             $emailOptions[1] = preg_split('/ *, */', $emailOptions[1]);
  672.                             foreach ($emailOptions[1] as $key=>$email) {
  673.                                 if (is_numeric($email))
  674.                                     $emailOptions[1][$key] = $trklib->get_item_value($trackerId, $rid, $email);
  675.                             }
  676.                         }
  677.                         include_once('lib/webmail/tikimaillib.php');
  678.                         $mail = new TikiMail();
  679.                         $mail->setHeader('From', $emailOptions[0]);
  680.                        
  681.                         if (!empty($emailOptions[2])) { //tpl
  682.                             $emailOptions[2] = preg_split('/ *, */', $emailOptions[2]);
  683.                             foreach ($emailOptions[2] as $ieo=>$eo) {
  684.                                 if (!preg_match('/\.tpl$/', $eo))
  685.                                     $emailOptions[2][$ieo] = $eo.'.tpl';
  686.                                 $tplSubject[$ieo] = str_replace('.tpl', '_subject.tpl', $emailOptions[2][$ieo]);
  687.                             }
  688.                         } else {
  689.                             $emailOptions[2] = array('tracker_changed_notification.tpl');
  690.                         }
  691.                         if (empty($tplSubject)) {
  692.                             $tplSubject = array('tracker_changed_notification_subject.tpl');
  693.                         }
  694.                         $itpl = 0;
  695.                         foreach ($emailOptions[1] as $ieo=>$ueo) {
  696.                             @$mail_data = $smarty->fetch('mail/'.$tplSubject[$itpl]);
  697.                             if (empty($mail_data))
  698.                                 $mail_data = tra('Tracker was modified at '). $_SERVER["SERVER_NAME"];
  699.                             $mail->setSubject($mail_data);
  700.                             $mail_data = $smarty->fetch('mail/'.$emailOptions[2][$itpl]);
  701.                             $mail->setText($mail_data);
  702.                             $mail->buildMessage(array('text_encoding' => '8bit'));
  703.                             $mail->send($ueo);
  704.                             if (isset($tplSubject[$itpl+1]))
  705.                                 ++$itpl;
  706.                         }
  707.                     }
  708.                     if (empty($url)) {
  709.                         if (!empty($page)) {
  710.                             $url = "tiki-index.php?page=".urlencode($page);
  711.                             if (!empty($itemId)) {
  712.                                 $url .= "&itemId=".$itemId;
  713.                             }
  714.                             $url .= "&ok=y&iTRACKER=$iTRACKER";
  715.                             $url .= "#wikiplugin_tracker$iTRACKER";
  716.                             header("Location: $url");
  717.                             die;
  718.                         } else {
  719.                             return '';
  720.                         }
  721.                     } else {
  722.                         $key = 0;
  723.                         foreach ($action as $key=>$act) {
  724.                             if (!empty($_REQUEST["action$key"])) {
  725.                                 break;
  726.                             }
  727.                         }
  728.                         if (strstr($url[$key], 'itemId')) {
  729.                             $url[$key] = str_replace('itemId', 'itemId='.$rid, $url[$key]);
  730.                         }
  731.                         header('Location: '.$url[$key]);
  732.                         die;
  733.                     }
  734.                     /* ------------------------------------- end save the item ---------------------------------- */
  735.                 } elseif (isset($_REQUEST['trackit']) and $_REQUEST['trackit'] == $trackerId) {
  736.                     $smarty->assign('wikiplugin_tracker', $trackerId);//used in vote plugin
  737.                 }
  738.  
  739.             } else if ((empty($itemId) || $overwrite == 'y') && !empty($values) || (!empty($_REQUEST['values']) and empty($_REQUEST['prefills']))) { // assign default values for each filedId specify
  740.                 if (empty($values)) { // url with values[]=x&values[] witouth the list of fields
  741.                     $values = $_REQUEST['values'];
  742.                 }
  743.                 if (!is_array($values)) {
  744.                     $values = array($values);
  745.                 }
  746.                 if (isset($fields)) {
  747.                     $fl = preg_split('/:/', $fields);
  748.                     for ($j = 0, $count_fl = count($fl); $j < $count_fl; $j++) {
  749.                         for ($i = 0, $count_flds = count($flds['data']); $i < $count_flds; $i++) {
  750.                             if ($flds['data'][$i]['fieldId'] == $fl[$j]) {
  751.                                 $flds['data'][$i]['value'] = $values[$j];
  752.                             }  
  753.                         }
  754.                     }
  755.                 } else { // values contains all the fields value in the default order
  756.                     $i = 0;
  757.                     foreach ($values as $value) {
  758.                         $flds['data'][$i++]['value'] = $value;
  759.                     }
  760.                 }
  761.            
  762.             } elseif (!empty($itemId)) {
  763.                 if (isset($fields)) {
  764.                     $fl = preg_split('/:/', $fields);
  765.                     $filter = '';
  766.                     foreach ($flds['data'] as $f) {
  767.                         if (in_array($f['fieldId'], $fl))
  768.                             $filter[] = $f;
  769.                     }
  770.                 } else {
  771.                     $filter = &$flds['data'];
  772.                 }
  773.                 if (!empty($filter)) {
  774.                     foreach ($filter as $f) {
  775.                         $filter2[$f['fieldId']] = $f;
  776.                     }
  777.                     $flds['data'] = $trklib->get_item_fields($trackerId, $itemId, $filter2, $itemUser);
  778.                 }
  779.                 // todo: apply the values for fields with no values
  780.             } else {
  781.                 if (isset($_REQUEST['values']) && isset($_REQUEST['prefills'])) { //url:prefields=1:2&values[]=x&values[]=y
  782.                     if (!is_array($_REQUEST['values']))
  783.                         $_REQUEST['values'] = array($_REQUEST['values']);
  784.                     $fl = preg_split('/:/', $_REQUEST['prefills']);
  785.                 } else {
  786.                     unset($fl);
  787.                 }
  788.                 for ($i = 0, $count_flds2 = count($flds['data']); $i < $count_flds2; $i++) {
  789.                     if (isset($fl) && ($j = array_search($flds['data'][$i]['fieldId'], $fl)) !== false) {
  790.                         $flds['data'][$i]['value'] = $_REQUEST['values'][$j];
  791.                     } else {
  792.                         $flds['data'][$i]['value'] = ''; // initialize fields with blank values
  793.                     }
  794.                 }
  795.             }
  796.  
  797.             $optional = array();
  798.             $outf = array();
  799.             if (isset($fields) && !empty($fields)) {
  800.                 $fl = preg_split('/:/', $fields);
  801.                 if ($sort == 'y')
  802.                     $flds = $trklib->sort_fields($flds, $fl);      
  803.                 foreach ($fl as $l) {
  804.                     if (substr($l, 0, 1) == '-') {
  805.                         $l = substr($l, 1);
  806.                         $optional[] = $l;
  807.                     }
  808.                     $ok = false;
  809.                     foreach ($flds['data'] as $f) {
  810.                         if ($f['fieldId'] == $l) {
  811.                             $ok = true;
  812.                             break;
  813.                         }
  814.                     }
  815.                     if (!$ok) {
  816.                         $back .= tra('Incorrect fieldId:').' '.$l;
  817.                     }
  818.                     $outf[] = $l;
  819.                 }
  820.             } elseif (empty($fields) && !empty($wiki)) {
  821.                 $wiki_info = $tikilib->get_page_info($wiki);
  822.                 preg_match_all('/\$f_([0-9]+)/', $wiki_info['data'], $matches);
  823.                 $outf = $matches[1];
  824.             } elseif (empty($fields) && !empty($tpl)) {
  825.                 $f = $smarty->get_filename($tpl);
  826.                 if (!empty($f)) {
  827.                     $f = file_get_contents($f);
  828.                     preg_match_all('/\$f_([0-9]+)/', $f, $matches);
  829.                     $outf = $matches[1];
  830.                 }
  831.             } elseif (empty($fields) && empty($wiki)) {
  832.                 foreach ($flds['data'] as $f) {
  833.                     if ($f['isMandatory'] == 'y')
  834.                         $optional[] = $f['fieldId'];
  835.                     $outf[] = $f['fieldId'];
  836.                 }
  837.             }
  838.  
  839.             // Display warnings when needed
  840.            
  841.             if(count($field_errors['err_mandatory']) > 0) {
  842.                 $smarty->assign_by_ref('err_mandatory', $field_errors['err_mandatory']);
  843.             }
  844.             if(count($field_errors['err_value']) > 0) {
  845.                 $smarty->assign_by_ref('err_value', $field_errors['err_value']);
  846.             }
  847.             if(count($field_errors['err_mandatory']) > 0 || count($field_errors['err_value']) > 0) {
  848.                 $back .= $smarty->fetch('tracker_error.tpl');
  849.                 $_REQUEST['error'] = 'y';
  850.             }
  851.             if (isset($field_errors['err_antibot'])) {
  852.                 $back.= '<div class="simplebox highlight"><img src="pics/icons/exclamation.png" alt=" '.tra('Error').'" style="vertical-align:middle" /> ';
  853.                 $back .= $captchalib->getErrors();
  854.                 $back.= '</div><br />';
  855.                 $_REQUEST['error'] = 'y';
  856.             }
  857.             if (isset($field_errors['err_outputwiki'])) {
  858.                 $back.= '<div class="simplebox highlight"><img src="pics/icons/exclamation.png" alt=" '.tra('Error').'" style="vertical-align:middle" /> ';
  859.                 $back .= $field_errors['err_outputwiki'];
  860.                 $back.= '</div><br />';
  861.                 $_REQUEST['error'] = 'y';
  862.             }
  863.             if (count($field_errors['err_mandatory']) > 0 || count($field_errors['err_value']) > 0 || isset($field_errors['err_antibot']) || isset($field_errors['err_outputwiki'])) {
  864.                 $smarty->assign('input_err', 'y');
  865.             }
  866.             if (!empty($page))
  867.                 $back .= '~np~';
  868.             $smarty->assign_by_ref('tiki_p_admin_trackers', $perms['tiki_p_admin_trackers']);
  869.             $smarty->assign('trackerEditFormId', $iTRACKER);
  870.             if ($prefs['feature_jquery'] == 'y' && $prefs['feature_jquery_validation'] == 'y') {
  871.                 global $validatorslib;
  872.                 include_once('lib/validatorslib.php');
  873.                 $customvalidation = '';
  874.                 $customvalidation_m = '';
  875.                 if ($registration == 'y') {
  876.                     // email validation
  877.                     $customvalidation .= 'email: { ';
  878.                     $customvalidation .= 'required: true, ';
  879.                     $customvalidation .= 'email: true }, ';
  880.                     $customvalidation_m .= 'email: { email: "'. tra("Invalid email") .  '"}, ';
  881.                     // password validation
  882.                     $customvalidation .= 'pass: { ';
  883.                     $customvalidation .= 'required: true, ';
  884.                     $customvalidation .= 'remote: { ';
  885.                     $customvalidation .= 'url: "validate-ajax.php", ';
  886.                     $customvalidation .= 'type: "post", ';
  887.                     $customvalidation .= 'data: { ';
  888.                     $customvalidation .= 'validator: "password", ';
  889.                     $customvalidation .= 'input: function() { ';
  890.                     $customvalidation .= 'return $("#pass1").val(); ';
  891.                     $customvalidation .= '} } } ';
  892.                     $customvalidation .= '}, ';
  893.                     // password repeat validation
  894.                     $customvalidation .= 'passAgain: { equalTo: "#pass1" }, ';
  895.                     $customvalidation_m .= 'passAgain: { equalTo: "'. tra("Passwords do not match") .  '"}, ';
  896.                     // username validation
  897.                     $customvalidation .= 'name: { ';
  898.                     $customvalidation .= 'required: true, ';
  899.                     $customvalidation .= 'remote: { ';
  900.                     $customvalidation .= 'url: "validate-ajax.php", ';
  901.                     $customvalidation .= 'type: "post", ';
  902.                     $customvalidation .= 'data: { ';
  903.                     $customvalidation .= 'validator: "username", ';
  904.                     $customvalidation .= 'input: function() { ';
  905.                     $customvalidation .= 'return $("#name").val(); ';
  906.                     $customvalidation .= '} } } ';
  907.                     $customvalidation .= '}, ';
  908.                 }
  909.                 $validationjs = $validatorslib->generateTrackerValidateJS( $flds['data'], "track_", $customvalidation, $customvalidation_m );
  910.  
  911.                 $smarty->assign('validationjs', $validationjs);
  912.                 $back .= $smarty->fetch('tracker_validator.tpl');
  913.             }
  914.             $back .= '<form name="editItemForm' . $iTRACKER . '" id="editItemForm' . $iTRACKER . '" enctype="multipart/form-data" method="post"'.(isset($target)?' target="'.$target.'"':'').' action="'. $_SERVER['REQUEST_URI'] .'"><input type="hidden" name="trackit" value="'.$trackerId.'" />';
  915.             $back .= '<input type="hidden" name="iTRACKER" value="'.$iTRACKER.'" />';
  916.             $back .= '<input type="hidden" name="refresh" value="1" />';
  917.             if (isset($_REQUEST['page']))
  918.                 $back.= '<input type="hidden" name="page" value="'.$_REQUEST["page"].'" />';
  919.              // for registration
  920.             if (isset($_REQUEST['name']))
  921.                 $back.= '<input type="hidden" name="name" value="'.$_REQUEST["name"].'" />';
  922.             if (isset($_REQUEST['pass'])) {
  923.                 $back.= '<input type="hidden" name="pass" value="'.$_REQUEST["pass"].'" />';
  924.                 $back.= '<input type="hidden" name="passAgain" value="'.$_REQUEST["pass"].'" />';
  925.             }
  926.             if (isset($_REQUEST['email']))
  927.                 $back.= '<input type="hidden" name="email" value="'.$_REQUEST["email"].'" />';
  928.             if (isset($_REQUEST['antibotcode']))
  929.                 $back.= '<input type="hidden" name="antibotcode" value="'.$_REQUEST["antibotcode"].'" />';
  930.             if (isset($_REQUEST['chosenGroup'])) // for registration
  931.                 $back.= '<input type="hidden" name="chosenGroup" value="'.$_REQUEST["chosenGroup"].'" />';
  932.             if (isset($_REQUEST['register']))
  933.                 $back.= '<input type="hidden" name="register" value="'.$_REQUEST["register"].'" />';
  934.             if ($showtitle == 'y') {
  935.                 $back.= '<div class="titlebar">'.$tracker["name"].'</div>';
  936.             }
  937.             if ($showdesc == 'y' && $tracker['description']) {
  938.  
  939.                 if ($tracker['descriptionIsParsed'] == 'y') {
  940.                     $back .= '<div class="wikitext">'.$tikilib->parse_data($tracker['description']).'</div><br />';
  941.                     } else {
  942.                     $back.= '<div class="wikitext">'.$tracker["description"].'</div><br />';
  943.                     }
  944.             }
  945.             if (isset($_REQUEST['tr_preview'])) { // use for the computed and join fields
  946.                 $assocValues = array();
  947.                 $assocNumerics = array();
  948.                 foreach ($flds['data'] as $f) {
  949.                     if (empty($f['value']) && ($f['type'] == 'u' || $f['type'] == 'g' || $f['type'] == 'I') && ($f['options_array'][0] == '1' || $f['options_array'][0] == '2')) { //need to fill the selector fields for the join
  950.                         $f['value'] = ($f['type'] == 'I')? $tikilib->get_ip_address(): (($f['type'] == 'g')? $group: $user);
  951.                     }
  952.                     $assocValues[$f['fieldId']] = $f['value'];
  953.                     $assocNumerics[$f['fieldId']] = preg_replace('/[^0-9\.\+]/', '', $f['value']); // get rid off the $ and such unit
  954.                 }
  955.             }
  956.  
  957.             if (!empty($itemId)) {
  958.                 $item = array('itemId'=>$itemId, 'trackerId'=>$trackerId);
  959.             }
  960.             foreach ($flds['data'] as $i=>$f) { // collect additional infos
  961.                 if (in_array($f['fieldId'], $outf)) {
  962.                     $flds['data'][$i]['ins_id'] = ($f['type'] == 'e')?'ins_cat_'.$f['fieldId']: 'track_'.$f['fieldId'];
  963.                     if (($f['isHidden'] == 'c' || $f['isHidden'] == 'p') && !empty($itemId) && !isset($item['creator'])) {
  964.                         $item['creator'] = $trklib->get_item_creator($trackerId, $itemId);
  965.                     }
  966.                     if ($f['type'] == 's' && ($f['name'] == 'Rating' || $f['name'] == tra('Rating')) && $perms['tiki_p_tracker_vote_ratings'] == 'y' && isset($item)) {
  967.                         $item['my_rate'] = $tikilib->get_user_vote("tracker$trackerId.$itemId", $user);
  968.                     }
  969.                     if ($f['isMultilingual'] == 'y') {
  970.                         $multi_languages = $prefs['available_languages'];
  971.                         foreach ($multi_languages as $num=>$tmplang){
  972.                             $flds['data'][$i]['lingualvalue'][$num]['lang'] = $tmplang;
  973.                         }
  974.                     }
  975.                     if ($f['type'] == 'r') {
  976.                         if (!isset($f['options_array'][3])) {
  977.                         $flds['data'][$i]['list'] = array_unique($trklib->get_all_items($f['options_array'][0], $f['options_array'][1], isset($f['options_array'][4])?$f['options_array'][4]:'poc'));
  978.                         }
  979.                         else {
  980.                         $flds['data'][$i]['list'] = $trklib->get_all_items($f['options_array'][0], $f['options_array'][1], isset($f['options_array'][4])?$f['options_array'][4]:'poc');
  981.                         }
  982.                         if (isset($f['options_array'][3])) {
  983.                             $flds['data'][$i]['listdisplay'] = array_unique($trklib->concat_all_items_from_fieldslist($f['options_array'][0], $f['options_array'][3], isset($f['options_array'][4])?$f['options_array'][4]:'poc'));
  984.                         }
  985.                     } elseif ($f['type'] == 'y') {
  986.                         $flds['data'][$i]['flags'] = $tikilib->get_flags(true, true, isset($f['options_array'][1])&&$f['options_array'][1]==1?false:true);
  987.                     } elseif ($f['type'] == 'u') {
  988.                         if ($perms['tiki_p_admin_trackers'] == 'y' || ($f['options_array'][0] != 1 && $f['options_array'][0] != 2))
  989.                             $flds['data'][$i]['list'] = $userlib->list_all_users();
  990.                         elseif ($f['options_array'][0] == 1)
  991.                             $flds['data'][$i]['value'] = $user;
  992.                     } elseif ($f['type'] == 'g') {
  993.                         if ($perms['tiki_p_admin_trackers'] == 'y' || ($f['options_array'][0] != 1 && $f['options_array'][0] != 2)) {
  994.                             $flds['data'][$i]['list'] = $userlib->list_all_groups();
  995.                         } elseif ($f['options_array'][0] == 1) {
  996.                             global $group;
  997.                             $flds['data'][$i]['value'] = $group;
  998.                         }
  999.                     } elseif ($f['type'] == 'k') {
  1000.                         if ($f['options_array'][0] == 1) {
  1001.                             if (isset($page)) {
  1002.                                 $flds['data'][$i]['value'] = $page;
  1003.                             }
  1004.                         }
  1005.                     } elseif ($f['type'] == 'd') { // attempting to pre-load default value for drop down if empty
  1006. echo("f['options_array'][0]: ");
  1007. echo($f['options_array'][0]);
  1008.                         if ($f['options_array'][0] == 0) {
  1009. echo("<br/>f['value']: ");
  1010. echo($f['value']);                         
  1011. echo("<br/>empty(f['value']): ");
  1012. echo(empty($f['value']));                          
  1013.  
  1014.                             if (empty($f['value'])) {
  1015. echo("<br/>flds['data'][i]: ");
  1016. echo($flds['data'][$i]);
  1017.  
  1018.                                 $flds['data'][$i]['value'] = $trklib->set_default_dropdown_option($flds['data'][$i]);
  1019. echo("<br/>flds['data'][i]['value']: ");
  1020. echo($flds['data'][$i]['value']);
  1021. //                              $ins_fields['data'][$i]['value'] = $this->set_default_dropdown_option($ins_fields['data'][$i]);
  1022.                             }
  1023.                         }
  1024.                     } elseif ($f['type'] == 'e') {
  1025.                         global $categlib; include_once('lib/categories/categlib.php');
  1026.                         $flds['data'][$i]['list'] = $categlib->get_viewable_child_categories($f["options_array"][0]);
  1027.                     } elseif ($f['type'] == 'A') {
  1028.                         if (!empty($f['value'])) {
  1029.                             $flds['data'][$i]['info'] = $trklib->get_item_attachment($f['value']);
  1030.                         }
  1031.                     } elseif ($f['type'] == 'a') {
  1032.                         if ($f['options_array'][0] == 1 && empty($toolbars)) {
  1033.                             // all in the smarty object now
  1034.                         }
  1035.                     } elseif ($f['type'] == 'l' && isset($itemId)) {
  1036.                         $opts[1] = preg_split('/:/', $f['options_array'][1]);
  1037.                         $finalFields = explode('|', $f['options_array'][3]);
  1038.                         $flds['data'][$i]['value'] = $trklib->get_join_values($trackerId, $itemId, array_merge(array($f['options_array'][2]), array($f['options_array'][1]), array($finalFields[0])), $f['options_array'][0], $finalFields, ' ', empty($f['options_array'][5])?'':$f['options_array'][5]);
  1039.                     } elseif ($f['type'] == 'w') {
  1040.                         $trklib->prepare_dynamic_items_list($f, $flds['data']);
  1041.                         if ($flds['data'][$i]['type'] == 'r') {
  1042.                             // we prentended it was an item link
  1043.                             $bindingValue = $trklib->get_item_value($trackerId, $itemId, $f['options_array'][2]);
  1044.                             $bindingItemLinkField = $trklib->get_tracker_field($f['options_array'][2]);
  1045.                             $bindingValueIndex = $trklib->get_item_id($bindingItemLinkField['options_array'][0], $bindingItemLinkField['options_array'][3], $bindingValue);
  1046.                             $flds['data'][$i]['list'] = $trklib->get_filtered_item_values($f['options_array'][1], $bindingValueIndex, $f['options_array'][3]);
  1047.                         }
  1048.                     } elseif ($f['type'] == 'f' && empty($itemId) && empty($f['options_array'][3])) {
  1049.                         $flds['data'][$i]['value'] = $tikilib->now;
  1050.                     } elseif ($f['type'] == 'F') {
  1051.                         global $freetaglib;
  1052.                         if (!is_object($freetaglib)) {
  1053.                             include_once('lib/freetag/freetaglib.php');
  1054.                         }
  1055.                         $flds['data'][$i]["freetags"] = $freetaglib->_parse_tag($f['value']);
  1056.                         $flds['data'][$i]["tag_suggestion"] = $freetaglib->get_tag_suggestion($flds['data'][$i]["freetags"],$prefs['freetags_browse_amount_tags_suggestion']);
  1057.                     }
  1058.  
  1059.                     if (!empty($f['value'])) {
  1060.                         // if it has an original or a suggested value, then set it as the default value
  1061.                         $flds['data'][$i]['defaultvalue'] = $f['value'];
  1062.                     }
  1063.                 }
  1064.             }
  1065.             if (!empty($showstatus) && $showstatus == 'y') {
  1066.                 $status_types = $trklib->status_types();
  1067.                 $smarty->assign_by_ref('status_types', $status_types);
  1068.                 $smarty->assign('form_status', 'status');
  1069.                 $smarty->assign_by_ref('tracker', $tracker);
  1070.                 if (!empty($item_info)) {
  1071.                     $smarty->assign_by_ref('item', $item_info);
  1072.                 }
  1073.                 $status_input = $smarty->fetch('tracker_status_input.tpl');
  1074.             }
  1075.  
  1076.             if ($registration == "y") {
  1077.                 $back .= '<input type="hidden" name="register" value="Register" />';
  1078.             }
  1079.            
  1080.             // Loop on tracker fields and display form
  1081.             if (empty($tpl) && empty($wiki)) {
  1082.                 $back.= '<table class="wikiplugin_tracker">';
  1083.                 if (!empty($showstatus) && $showstatus == 'y') {
  1084.                     $back .= '<tr><td>'.tra('Status').'</td><td>'.$status_input.'</td></tr>';
  1085.                 }
  1086.                 if ($registration == 'y') {
  1087.                     $back .= $smarty->fetch('register-form.tpl');
  1088.                 }
  1089.             } else {
  1090.                 $back .= '<div class="wikiplugin_tracker">';
  1091.                 if (!empty($showstatus) && $showstatus == 'y') {
  1092.                     $smarty->assign_by_ref('f_status_input', $status_input);
  1093.                 }
  1094.             }
  1095.             $backLength0 = strlen($back);
  1096.             foreach ($flds['data'] as $f) {
  1097.                 if ($f['type'] == 'u' && $f['options_array'][0] == '1' && !in_array($f['fieldId'], $outf)) {
  1098.                     $back.= '<input type="hidden" name="authorfieldid" value="'.$f['fieldId'].'" />';
  1099.                 } elseif ($f['type'] == 'I' and $f['options_array'][0] == '1') {
  1100.                     $back.= '<input type="hidden" name="authoripid" value="'.$f['fieldId'].'" />';
  1101.                 } elseif ($f['type'] == 'g' and $f['options_array'][0] == '1') {
  1102.                     $back.= '<input type="hidden" name="authorgroupfieldid" value="'.$f['fieldId'].'" />';
  1103.                 } elseif ($f['type'] == 'q') {
  1104.                     $back .= '<input type="hidden" name="track['.$f['fieldId'].']" />';
  1105.                 } elseif (in_array($f['fieldId'], $outf)) {
  1106.                     if ($showmandatory == 'y' and $f['isMandatory'] == 'y') {
  1107.                         $onemandatory = true;
  1108.                     }
  1109.                     if ($f['type'] == 'A') {
  1110.                         $smarty->assign_by_ref('tiki_p_attach_trackers', $perms['tiki_p_attach_trackers']);
  1111.                     }
  1112.                     if (!empty($tpl) || !empty($wiki)) {
  1113.                         $smarty->assign_by_ref('field_value', $f);
  1114.                         if (isset($item)) {
  1115.                             $smarty->assign_by_ref('item', $item);
  1116.                         }
  1117.                         $smarty->assign('f_'.$f['fieldId'], $smarty->fetch('tracker_item_field_input.tpl'));
  1118.                     } else {
  1119.                         if (in_array($f['fieldId'], $optional)) {
  1120.                             $f['name'] = "<i>".$f['name']."</i>";
  1121.                         }
  1122.                         if ($f['type'] != 'h') {
  1123.                             $back.= "<tr><td";
  1124.                             if (!empty($colwidth)){
  1125.                                 $back .= " width='".$colwidth."'";
  1126.                             }
  1127.                             $back .= ">".wikiplugin_tracker_name($f['fieldId'], tra($f['name']), $field_errors);
  1128.                             if ($showmandatory == 'y' and $f['isMandatory'] == 'y') {
  1129.                                 $back.= "&nbsp;<strong class='mandatory_star'>*</strong>&nbsp;";
  1130.                             }
  1131.                             $back.= "</td><td>";
  1132.                         } else {
  1133.                             $back .= "<tr><th colspan='2'>".wikiplugin_tracker_name($f['fieldId'], tra($f['name']), $field_errors);
  1134.                         }
  1135.                         $smarty->assign_by_ref('field_value', $f);
  1136.                         if (isset($item)) {
  1137.                             $smarty->assign_by_ref('item', $item);
  1138.                         }
  1139.                         $back .= $smarty->fetch('tracker_item_field_input.tpl');
  1140.                     }
  1141.  
  1142.                     if (!empty($f['description']) && $f['type'] != 'h' && $f['type'] != 'S') {
  1143.                         $back .= '<br />';
  1144.                         if ($f['descriptionIsParsed'] == 'y') {
  1145.                             $back .= $tikilib->parse_data($f['description']);
  1146.                         } else {
  1147.                             $back .= '<span class="trackerplugindesc">'.$f['description'].'</span>';
  1148.                         }
  1149.                     }
  1150.                     if (empty($tpl) && empty($wiki)) {
  1151.                     if ($f['type'] != 'h'){
  1152.                         $back.= "</td></tr>";
  1153.                         } else {
  1154.                         $back.= "</th></tr>";                      
  1155.                         }
  1156.                     }
  1157.                     if (!empty($f['http_request']) && !empty($itemId)) {
  1158.                         $js .= 'selectValues("trackerIdList=' . $f['http_request'][0]
  1159.                                     . '&fieldlist=' . $f['http_request'][3]
  1160.                                     . '&filterfield=' . $f['http_request'][1]
  1161.                                     . '&status=' . $f['http_request'][4]
  1162.                                     . '&mandatory=' . $f['http_request'][6]
  1163.                                     . '&filtervalue=' . $f['http_request'][7]
  1164.                                     . '&selected=' . $f['http_request'][8]
  1165.                                     . '","' . $f['http_request'][5]
  1166.                                     .'");'
  1167.                                     ;
  1168.                     }
  1169.                 }
  1170.             }
  1171.             if (!empty($tpl)) {
  1172.                 $smarty->security = true;
  1173.                 $back .= $smarty->fetch($tpl);
  1174.             } elseif (!empty($wiki)) {
  1175.                 $smarty->security = true;
  1176.                 $back .= $smarty->fetch('wiki:'.$wiki);
  1177.             }
  1178.             if ($prefs['feature_antibot'] == 'y' && empty($user) && $registration != 'y') {
  1179.                 // in_tracker session var checking is for tiki-register.php
  1180.                 $back .= $smarty->fetch('antibot.tpl');
  1181.             }
  1182.             if (empty($tpl) && empty($wiki)) {
  1183.                 $back.= "</table>";
  1184.             } else {
  1185.                 $back .= '</div>';
  1186.             }
  1187.             $back .= '<div class="input_submit_container">';
  1188.            
  1189.             if (!empty($reset)) {
  1190.                 $back .= "<input class='button submit preview' type='reset' name='tr_reset' value='".tra($reset)."' />";
  1191.             }
  1192.             if (!empty($preview)) {
  1193.                 $back .= "<input class='button submit preview' type='submit' name='tr_preview' value='".tra($preview)."' />";
  1194.             }
  1195.             foreach ($action as $key=>$act) {
  1196.                 $back .= "  <input class='button submit' type='submit' name='action$key' value='".tra($act)."' />";
  1197.             }
  1198.             $back .= '</div>';
  1199.             if ($showmandatory == 'y' and $onemandatory) {
  1200.                 $back.= "<em class='mandatory_note'>".tra("Fields marked with a * are mandatory.")."</em>";
  1201.             }
  1202.             $back.= '</form>';
  1203.             if (!empty($js)) {
  1204.                 $back .= '<script type="text/javascript">'.$js.'</script>';
  1205.             }
  1206.             if (!empty($page))
  1207.                 $back .= '~/np~';
  1208.             $smarty->assign_by_ref('tiki_p_admin_trackers', $tiki_p_admin_trackers);
  1209.         return $back;
  1210.     }
  1211.     else {
  1212.         if (isset($_REQUEST['trackit']) and $_REQUEST['trackit'] == $trackerId)
  1213.             $smarty->assign('wikiplugin_tracker', $trackerId);//used in vote plugin
  1214.         $id = ' id="wikiplugin_tracker'.$iTRACKER.'"';
  1215.         if ($showtitle == 'y') {
  1216.             $back.= '<div class="titlebar"'.$id.'>'.$tracker["name"].'</div>';
  1217.             $id = '';
  1218.         }
  1219.         if ($showdesc == 'y') {
  1220.             $back.= '<div class="wikitext"'.$id.'>'.$tracker["description"].'</div><br />';
  1221.             $id = '';
  1222.         }
  1223.         $back.= "<div$id>".$data.'</div>';
  1224.         return $back;
  1225.     }
  1226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement