/** * Perform a recursive search in a "multidimensional array" * @param scalar $needle The element to search for * @param array $haystack The array to search in * @param bool $strict True to perform a strict (===) check * @return false|array Returns an array of path keys, or false if the needle couldn't be found in the haystack */ function array_search_recursive($needle, $haystack, $strict = false) { foreach ($haystack as $haystackkey => $haystackelement) { if ($strict && $needle === $haystackelement) { return (array) $haystackkey; } if (!$strict && $needle == $haystackelement) { return (array) $haystackkey; } if (is_array($haystackelement) && ($found = \array_search_recursive($needle, $haystackelement, $strict)) !== false) { $keylist = \array_merge((array) $haystackkey, $found); return $keylist; } } return false; }
function array_search_recursive($Needle, $Haystack, $NeedleKey = "", $Strict = false, $Path = array()) { if (!is_array($Haystack)) { return false; } foreach ($Haystack as $Key => $Val) { if (is_array($Val) && ($SubPath = array_search_recursive($Needle, $Val, $NeedleKey, $Strict, $Path))) { $Path = array_merge($Path, array($Key), $SubPath); return $Path; } elseif (!$Strict && $Val == $Needle && $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key) || $Strict && $Val === $Needle && $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key)) { $Path[] = $Key; return $Path; } } return false; }
function array_search_recursive($needle, $haystack, $strict = false, $path = array()) { if (!is_array($haystack)) { return false; } foreach ($haystack as $key => $val) { if (is_array($val) && ($subPath = array_search_recursive($needle, $val, $strict, $path))) { $path = array_merge($path, array($key), $subPath); return $path; } elseif (!$strict && $val == $needle || $strict && $val === $needle) { $path[] = $key; return $path; } } return false; }
function array_search_recursive($needle, $haystack) { $path = array(); foreach ($haystack as $id => $val) { if ($val === $needle) { $path[] = $id; } else { if (is_array($val)) { $found = array_search_recursive($needle, $val); if (count($found) > 0) { $path[$id] = $found; } } } } return array_keys($path); }
/** Search in array and return the parents keys */ function array_search_recursive($needle, $haystack, $firstKey = false) { $keys = array(); foreach ($haystack as $key => $value) { if (is_array($value)) { $keys[] = $key; $newkeys = array_search_recursive($needle, $value); if ($newkeys) { $keys = array_merge($keys, $newkeys); return $firstKey ? $keys[0] : $keys; } $keys = array(); } else { if ($value == $needle) { $keys[] = $key; return $firstKey ? $keys[0] : $keys; } } } return false; }
/** * @param $record * @param $debug * determine completeness of all survey-containing events */ function set_survey_completion($record, $debug) { if (isset($record)) { global $Proj, $project_id; $today = date("Y-m-d"); $fields = array(); $arms = get_arms(array_keys($Proj->eventsForms)); $baseline_event_id = $Proj->firstEventId; $trt = Treatment::getTrtInfo($record); /** * use the selected duration ($trt['arm']) to set timings */ $tx_first_event = array_search_recursive($trt['arm'], $arms) !== false ? array_search_recursive($trt['arm'], $arms) : null; $survey_event_ids = isset($tx_first_event) ? $Proj->getEventsByArmNum($arms[$tx_first_event]['arm_num']) : null; foreach ($survey_event_ids AS $survey_event_id) { $data_event_name = $Proj->getUniqueEventNames($survey_event_id); $prefix = substr($data_event_name, 0, strpos($data_event_name, '_')); /** * derive inter-event timing variables */ $start_date_value = get_single_field($record, $project_id, $baseline_event_id, $prefix . '_startdate', null); $deadline_value = get_single_field($record, $project_id, $baseline_event_id, $prefix . '_deadline', null); /** * for baseline surveys, reference date is randomization date. For all other survey events, * reference date is dm_rfxstdtc, recorded treatment start date NOT dm_rfstdtc, the * treatment start derived from the treatment med data */ $t_base_date = in_array($prefix, array('baseline', 'eot1year', 'eot3year')) ? $trt['rand_date'] : $trt['rfxstdtc']; if (isset($t_base_date) && $t_base_date != '') { $t_start_date = add_date($t_base_date, ($arms[$data_event_name]['day_offset'] - $arms[$data_event_name]['offset_min'])); update_field_compare($record, $project_id, $baseline_event_id, $t_start_date, $start_date_value, $prefix . '_startdate', $debug); $t_deadline_date = add_date($t_base_date, ($arms[$data_event_name]['day_offset'] + $arms[$data_event_name]['offset_max']) - 1); update_field_compare($record, $project_id, $baseline_event_id, $t_deadline_date, $deadline_value, $prefix . '_deadline', $debug); } } foreach ($survey_event_ids AS $survey_event_id) { $survey_event_name = $Proj->getUniqueEventNames($survey_event_id); $survey_prefix = substr($survey_event_name, 0, strpos($survey_event_name, '_')); $fields[] = $survey_prefix . '_completed'; $fields[] = $survey_prefix . '_date'; $fields[] = $survey_prefix . '_startdate'; $fields[] = $survey_prefix . '_deadline'; } $data = REDCap::getData('array', $record, $fields, $baseline_event_id); /** * switch to the scheduled arm to determine completion * this is done to avoid subjects switching arms and orphaning surveys completed * prior to duration change. This should (almost) never happen, but we still need to handle if it does */ $tx_first_event = array_search_recursive($trt['timing_arm'], $arms) !== false ? array_search_recursive($trt['timing_arm'], $arms) : null; $survey_event_ids = isset($tx_first_event) ? $Proj->getEventsByArmNum($arms[$tx_first_event]['arm_num']) : null; foreach ($survey_event_ids AS $survey_event_id) { $data_event_name = $Proj->getUniqueEventNames($survey_event_id); $prefix = substr($data_event_name, 0, strpos($data_event_name, '_')); $is_t_complete = is_t_complete($record, $survey_event_id); $t_complete = $is_t_complete ? '1' : '0'; $complete_value = get_single_field($record, $project_id, $baseline_event_id, $prefix . '_completed', null); $deadline_value = get_single_field($record, $project_id, $baseline_event_id, $prefix . '_deadline', null); $t_missed = $complete_value == '0' && $today > $deadline_value && isset($deadline_value) && $deadline_value != '' ? '1' : '0'; foreach ($data[$record] AS $data_event_id => $data_event) { foreach ($data_event AS $key => $value) { /** * derive completion variables */ switch ($key) { case $prefix . '_completed': update_field_compare($record, $project_id, $data_event_id, $t_complete, $value, $key, $debug); break; case $prefix . '_date': if ($value == '' && $is_t_complete) { update_field_compare($record, $project_id, $data_event_id, $today, $value, $key, $debug); } break; case $prefix . '_missed': update_field_compare($record, $project_id, $data_event_id, $t_missed, $value, $key, $debug); break; default: break; } } } } } }
/** Filter tweets */ public function where($type, $operator = '=', $value = null) { // If it used by this way: ´->where('retweets', 12);´ if (is_null($value)) { $value = $operator; $operator = '='; } if (!($type = array_search_recursive($type, $this->filterBy, true))) { throw new BirderException("{$type} is not a valid type", 1); } if (!in_array($operator, $this->operators)) { throw new BirderException("{$operator} is not a valid operator", 1); } if (!is_numeric($value)) { throw new BirderException("The value must be numeric, {$operator} given", 1); } $this->conditions[$type] = array('operator' => $operator, 'value' => $value); return $this; }
/** * Work for language pack. * Open language file and return needed * string. * * @param string $key * @param string $context * @return string */ function __($key, $context = false) { $Register = Register::getInstance(); $language = getLang(); if (empty($language) || !is_string($language)) { $language = 'rus'; } if (!empty($Register['translation_cache'])) { $lang = $Register['translation_cache']; } else { $lang_file = ROOT . '/sys/settings/languages/' . $language . '.php'; $tpl_lang_file = ROOT . '/template/' . getTemplateName() . '/languages/' . $language . '.php'; if (!file_exists($lang_file)) { throw new Exception('Main language file not found'); } $lang = (include $lang_file); if (file_exists($tpl_lang_file)) { $tpl_lang = (include $tpl_lang_file); $lang = array_merge($lang, $tpl_lang); } $mod_langs = array_search_recursive($language, $Register['modules_translations']); if ($mod_langs) { foreach ($mod_langs as $path) { $mod_lang = (include $path); $lang = array_merge($lang, $mod_lang); } } $Register['translation_cache'] = $lang; } if ($context && is_string($context) && is_array($lang[$context])) { if (array_key_exists($context, $lang) && array_key_exists($key, $lang[$context])) { return $lang[$context][$key]; } } if (array_key_exists($key, $lang)) { return $lang[$key]; } return $key; }
/** * do stuff */ if ($debug) { if (!is_form_locked($record, $instrument, $redcap_event_name)) { d(is_t_complete($record, $event_id), $redcap_event_name); } global $Proj, $project_id; $today = date("Y-m-d"); $fields = array(); $arms = get_arms(array_keys($Proj->eventsForms)); d($arms); $baseline_event_id = $Proj->firstEventId; $tx_duration = get_single_field($record, $project_id, $baseline_event_id, 'trt_exdur', null); $tx_duration = substr($tx_duration, strpos($tx_duration, 'P') + 1, strlen($tx_duration) - 2); $tx_first_event = array_search_recursive($tx_duration . ' Weeks', $arms) !== false ? array_search_recursive($tx_duration . ' Weeks', $arms) : null; $survey_event_ids = $Proj->getEventsByArmNum($arms[$tx_first_event]['arm_num']); d($survey_event_ids); foreach ($survey_event_ids as $survey_event_id) { $survey_event_name = $Proj->getUniqueEventNames($survey_event_id); $survey_prefix = substr($survey_event_name, 0, strpos($survey_event_name, '_')); $fields[] = $survey_prefix . '_completed'; $fields[] = $survey_prefix . '_date'; $fields[] = $survey_prefix . '_startdate'; $fields[] = $survey_prefix . '_deadline'; $fields[] = $survey_prefix . '_missed'; } d($fields); $data = REDCap::getData('array', $record, $fields, $baseline_event_id); d($data); }
function array_search_recursive($needle, $haystack, $path = array()) { foreach ($haystack as $id => $val) { $path2 = $path; $path2[] = $id; if ($val === $needle) { return $path2; } else { if (is_array($val)) { if ($ret = array_search_recursive($needle, $val, $path2)) { return $ret; } } } } return false; }
/** * Just a quick handy function to search down through multi-dimensional arrays * it returns an array within which the $needle was found * needed for license() function. */ function array_search_recursive($needle, $haystack, $key_lookin = "") { $path = NULL; if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) { $path[] = $key_lookin; } else { foreach ($haystack as $key => $val) { if (is_scalar($val) && $val === $needle && empty($key_lookin)) { $path[] = $key; break; } elseif (is_array($val) && ($path = array_search_recursive($needle, $val, $key_lookin))) { array_unshift($path, $key); break; } } } return $path; }
/** * Returns key of value, key of array which contains value or false. * * @param array $haystack * @param mixed $needle * * @return int|string|bool */ function array_search_recursive($haystack, $needle) { foreach ($haystack as $key => $value) { $current_key = $key; if ($needle === $value || is_array($value) && array_search_recursive($needle, $value) !== false) { return $current_key; } } return false; }
/** * @param $subject_id * @param $debug * determine completeness of all survey-containing events */ function events_completion($subject_id, $debug) { if (isset($subject_id)) { global $Proj, $project_id; $today = date("Y-m-d"); $fields = array(); $arms = get_arms(array_keys($Proj->eventsForms)); $baseline_event_id = $Proj->firstEventId; $enrollment_event_id = getNextEventId($baseline_event_id); $tx_duration = get_single_field($subject_id, $project_id, $enrollment_event_id, 'dm_suppdm_actarmdur', null); $tx_first_event = array_search_recursive($tx_duration . ' Weeks', $arms) !== false ? array_search_recursive($tx_duration . ' Weeks', $arms) : null; $survey_event_ids = isset($tx_first_event) ? array_merge(array($baseline_event_id), $Proj->getEventsByArmNum($arms[$tx_first_event]['arm_num'])) : array($baseline_event_id); foreach ($survey_event_ids AS $survey_event_id) { $survey_event_name = $Proj->getUniqueEventNames($survey_event_id); $survey_prefix = substr($survey_event_name, 0, strpos($survey_event_name, '_')); $fields[] = $survey_prefix . '_completed'; $fields[] = $survey_prefix . '_date'; $fields[] = $survey_prefix . '_startdate'; $fields[] = $survey_prefix . '_deadline'; } $data = REDCap::getData('array', $subject_id, $fields, $baseline_event_id); foreach ($survey_event_ids AS $survey_event_id) { $data_event_name = $Proj->getUniqueEventNames($survey_event_id); $prefix = substr($data_event_name, 0, strpos($data_event_name, '_')); $is_t_complete = is_t_complete($subject_id, $survey_event_id); $t_complete = $is_t_complete ? '1' : '0'; foreach ($data[$subject_id] AS $data_event_id => $data_event) { foreach ($data_event AS $key => $value) { /** * derive intra-event timing variables */ switch ($key) { case $prefix . '_completed': update_field_compare($subject_id, $project_id, $data_event_id, $t_complete, $value, $key, $debug); break; case $prefix . '_date': if ($value == '' && $is_t_complete) { update_field_compare($subject_id, $project_id, $data_event_id, $today, $value, $key, $debug); } break; default: break; } } } /** * derive inter-event timing variables */ $complete_value = get_single_field($subject_id, $project_id, $baseline_event_id, $prefix . '_completed', null); $start_date_value = get_single_field($subject_id, $project_id, $baseline_event_id, $prefix . '_startdate', null); $deadline_value = get_single_field($subject_id, $project_id, $baseline_event_id, $prefix . '_deadline', null); $missed_value = get_single_field($subject_id, $project_id, $baseline_event_id, $prefix . '_missed', null); $t_missed = $complete_value == '0' && $today > $deadline_value && isset($deadline_value) && $deadline_value != '' ? '1' : '0'; switch ($prefix) { case 't1': $t_base_date = get_single_field($subject_id, $project_id, $baseline_event_id, $prefix . '_date', null); $t_start_date = $t_base_date; $t_deadline_date = add_date($t_base_date, 89); break; default: $t_base_date = get_single_field($subject_id, $project_id, $enrollment_event_id, 'dm_rfstdtc', null); $t_start_date = add_date($t_base_date, ($arms[$data_event_name]['day_offset'] - $arms[$data_event_name]['offset_min'])); $t_deadline_date = add_date($t_base_date, ($arms[$data_event_name]['day_offset'] + $arms[$data_event_name]['offset_max']) - 1); break; } update_field_compare($subject_id, $project_id, $baseline_event_id, $t_complete, $complete_value, $prefix . '_completed', $debug); update_field_compare($subject_id, $project_id, $baseline_event_id, $t_missed, $missed_value, $prefix . '_missed', $debug); if (isset($t_base_date) && $t_base_date != '') { update_field_compare($subject_id, $project_id, $baseline_event_id, $t_start_date, $start_date_value, $prefix . '_startdate', $debug); update_field_compare($subject_id, $project_id, $baseline_event_id, $t_deadline_date, $deadline_value, $prefix . '_deadline', $debug); } } } }
} $config['namespaces'] = \array_keys($_POST['namespaces']); $localNamespacesRequest = \json_decode($api->request('POST', $config['wiki'] . '/w/api.php', ['form_params' => ['action' => 'query', 'meta' => 'siteinfo', 'siprop' => 'namespaces', 'format' => 'json', 'formatversion' => 2]])->getBody(), true); if (!isset($localNamespacesRequest['query']['namespaces'])) { \header('Location: configsetter.php?error=nonamespacesonwiki'); return; } $localNamespaces = $localNamespacesRequest['query']['namespaces']; if (\array_diff($config['namespaces'], \array_keys($localNamespaces))) { \header('Location: configsetter.php?error=wrongnamespace'); return; } // The bot request URL should be a diff on one of the sites handled // by the Wikimedia Foundation $botRequestArray = \parse_url($_POST['botrequesturl']); if (empty($botRequestArray['host']) || \array_search_recursive('https://' . $botRequestArray['host'], $sitematrix, true) === false) { \header('Location: configsetter.php?error=requesturl'); return; } if (!empty($botRequestArray['query'])) { \parse_str($botRequestArray['query'], $botRequestQuery); // If the query has no 'diff' or 'oldid' element, no way to get a revid if (isset($botRequestQuery['diff'])) { if ($botRequestQuery['diff'] === 'prev' && (!empty($botRequestQuery['oldid']) && \ctype_digit($botRequestQuery['oldid']))) { $config['botrequestrevid'] = $botRequestQuery['oldid']; } elseif (\ctype_digit($botRequestQuery['diff'])) { $config['botrequestrevid'] = $botRequestQuery['diff']; } else { \header('Location: configsetter.php?error=requesturl'); return; }
/** * @param $record * @param $debug */ public static function set_tx_data($record, $debug) { global $Proj, $project_id, $tx_prefixes, $dm_array, $tx_array, $endt_fields, $regimen_fields; $enable_kint = $debug && (isset($record) && $record != '') ? true : false; Kint::enabled($enable_kint); $baseline_event_id = $Proj->firstEventId; $fields = array_merge($dm_array, $tx_array, $endt_fields, array('trt_suppex_txstat')); $data = REDCap::getData('array', $record, $fields); $regimen_data = REDCap::getData('array', $record, $regimen_fields); foreach ($data AS $subject_id => $subject) { $start_stack = array(); $tx_start_date = null; $stop_date = null; $age_at_start = null; $end_values = array(); foreach ($subject AS $event_id => $event) { /** * build dm_rfstdtc array */ foreach ($tx_array AS $tx_start) { if ($event[$tx_start] != '') { $start_stack[] = $event[$tx_start]; } } /** * build entdtc array */ foreach ($endt_fields AS $endt_field) { if ($event[$endt_field] != '') { $end_values[$event_id][$endt_field] = $event[$endt_field]; } } } /** * SUBJECT LEVEL */ rsort($start_stack); $tx_start_date = get_end_of_array($start_stack); /** * dm_rfstdtc */ update_field_compare($subject_id, $project_id, $baseline_event_id, $tx_start_date, $subject[$baseline_event_id]['dm_rfstdtc'], 'dm_rfstdtc', $debug); /** * age is dependent on the dm_rfxstdtc, not the derived treatment start date used elsewhere */ $dm_rfxstdtc = $subject[$baseline_event_id]['dm_rfxstdtc']; if (isset($dm_rfxstdtc)) { /** * Age at start of treatment * age_suppvs_age */ if ($subject[$baseline_event_id]['dm_brthyr'] != '') { $birth_year = $subject[$baseline_event_id]['dm_brthyr']; } elseif ($subject[$baseline_event_id]['dm_brthdtc'] != '') { $birth_year = substr($subject[$baseline_event_id]['dm_brthdtc'], 0, 4); } else { $birth_year = ''; } if (isset($birth_year) && $birth_year != '') { $tx_start_year = substr($dm_rfxstdtc, 0, 4); $age_at_start = ($tx_start_year - $birth_year) > 0 ? $tx_start_year - $birth_year : null; } update_field_compare($subject_id, $project_id, $baseline_event_id, $age_at_start, $subject[$baseline_event_id]['age_suppvs_age'], 'age_suppvs_age', $debug); } /** * dependent on derived TX start */ if (isset($tx_start_date)) { /** * Date of last dose of HCV treatment or Treatment stop date * dis_suppfa_txendt */ $stack = array(); if (array_search_recursive('ONGOING', $end_values) === false) { foreach ($tx_prefixes AS $endt_prefix) { foreach ($end_values AS $event) { if ($event[$endt_prefix . '_exendtc'] != '' && ($event[$endt_prefix . '_suppex_extrtout'] == 'COMPLETE') || $event[$endt_prefix . '_suppex_extrtout'] == 'PREMATURELY_DISCONTINUED') { $stack[] = $event[$endt_prefix . '_exendtc']; d('PREFIX ' . $endt_prefix, $event); } } } } sort($start_stack); sort($stack); $last_date_in_start_stack = get_end_of_array($start_stack); $last_date_in_stack = get_end_of_array($stack); $stop_date = $last_date_in_stack < $last_date_in_start_stack ? null : $last_date_in_stack; d($end_values); d($start_stack); d($stack); d($last_date_in_start_stack); d($last_date_in_stack); d($stop_date); update_field_compare($subject_id, $project_id, $baseline_event_id, $stop_date, $subject[$baseline_event_id]['dis_suppfa_txendt'], 'dis_suppfa_txendt', $debug); /** * HCV Treatment duration */ if (isset($stop_date)) { $tx_start_date_obj = new DateTime($tx_start_date); $tx_stop_date_obj = new DateTime($stop_date); $tx_duration = $tx_start_date_obj->diff($tx_stop_date_obj); $dis_dsstdy = $tx_duration->format('%R%a') + 1; update_field_compare($subject_id, $project_id, $baseline_event_id, $dis_dsstdy, $subject[$baseline_event_id]['dis_dsstdy'], 'dis_dsstdy', $debug); } } /** * update treatment regimen */ $txstat = isset($tx_start_date) ? 'Y' : 'N'; $regimen = get_regimen($regimen_data[$subject_id], $subject[$baseline_event_id]['eot_dsterm'], $txstat); update_field_compare($subject_id, $project_id, $baseline_event_id, $regimen['actarm'], $subject[$baseline_event_id]['dm_actarm'], 'dm_actarm', $debug); update_field_compare($subject_id, $project_id, $baseline_event_id, $regimen['actarmcd'], $subject[$baseline_event_id]['dm_actarmcd'], 'dm_actarmcd', $debug); } }
public function confirmRequestByMail($step_id) { $wp_slug = $this->_post->post_name; $step_id = intval($step_id); $elements = $this->fetchKeyValue('wp_slug_data_elements_step_' . $step_id); $search = array_search_recursive('e-mail_address', $elements); $email = ''; if (!empty($search[0])) { $email = $elements[$search[0]]['element_value']; } if (!empty($email)) { $wp_slug_email_file = TEMPLATEPATH . '/' . $wp_slug . '_email.php'; if (file_exists($wp_slug_email_file)) { require_once $wp_slug_email_file; $to = $email; $subject = $custom_subject; $message = $custom_message . "\r\n" . get_bloginfo('wpurl') . '/step_6/?cid=' . session_id(); $headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } } }
return; } if (empty($_POST['diff'])) { echo \json_encode(['error' => 'differror']); return; } $ajaxResponse = []; $api = new \GuzzleHttp\Client(['base_uri' => 'https://www.wikidata.org/w/api.php', 'headers' => ['User-Agent' => 'RollBot v0.1 (human interface), by [[:fr:User:Alphos]]']]); if (($diff = \parse_url($_POST['diff'])) === null || empty($diff['host'])) { echo \json_encode(['error' => 'diffurlerror']); return; } $wiki = 'https://' . $diff['host']; $sitematrix = \json_decode($api->request('POST', '', ['form_params' => ['action' => 'sitematrix', 'format' => 'json']])->getBody(), true); \parse_str($diff['query'], $diffQueryStringArray); if (($sitematrixSearch = \array_search_recursive($wiki, $sitematrix, true)) === false || \end($sitematrixSearch) !== 'url') { echo \json_encode(['error' => 'wikierror']); return; } if ($diffQueryStringArray['diff'] === 'prev') { $diffQueryStringArray['diff'] = $diffQueryStringArray['oldid']; } $diffInfo = \json_decode($api->request('GET', $wiki . '/w/api.php', ['query' => ['action' => 'query', 'prop' => 'revisions', 'revids' => $diffQueryStringArray['diff'], 'rvprop' => 'ids|timestamp|user', 'format' => 'json', 'formatversion' => 2]])->getBody(), true); if (!isset($diffInfo['query']['pages']) || \count($pages = \array_values($diffInfo['query']['pages'])) !== 1 || \count($pages[0]['revisions']) !== 1) { echo \json_encode(['error' => 'noreverror']); return; } $badRevision = $pages[0]['revisions'][0]; $ajaxResponse['starttimestamp'] = $badRevision['timestamp']; $ajaxResponse['offender'] = $badRevision['user']; $prefilledNamespace = $pages[0]['ns'];