streams/Code/Module/Admin/Profs.php

201 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Module\Admin;
2022-02-16 04:08:28 +00:00
use Code\Lib\Channel;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2022-01-25 01:26:12 +00:00
2021-12-02 23:02:31 +00:00
class Profs
{
2021-12-02 23:02:31 +00:00
public function post()
{
2021-12-02 23:02:31 +00:00
if (array_key_exists('basic', $_REQUEST)) {
$arr = explode(',', $_REQUEST['basic']);
array_walk($arr, 'array_trim');
$narr = [];
if (count($arr)) {
foreach ($arr as $a) {
if (strlen($a)) {
$narr[] = $a;
}
}
}
2021-12-03 03:01:39 +00:00
if (!$narr) {
2021-12-02 23:02:31 +00:00
del_config('system', 'profile_fields_basic');
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
set_config('system', 'profile_fields_basic', $narr);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if (array_key_exists('advanced', $_REQUEST)) {
$arr = explode(',', $_REQUEST['advanced']);
array_walk($arr, 'array_trim');
$narr = [];
if (count($arr)) {
foreach ($arr as $a) {
if (strlen($a)) {
$narr[] = $a;
}
}
}
2021-12-03 03:01:39 +00:00
if (!$narr) {
2021-12-02 23:02:31 +00:00
del_config('system', 'profile_fields_advanced');
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
set_config('system', 'profile_fields_advanced', $narr);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
goaway(z_root() . '/admin/profs');
}
if (array_key_exists('field_name', $_REQUEST)) {
if ($_REQUEST['id']) {
2021-12-03 03:01:39 +00:00
$r = q(
"update profdef set field_name = '%s', field_type = '%s', field_desc = '%s' field_help = '%s', field_inputs = '%s' where id = %d",
2021-12-02 23:02:31 +00:00
dbesc($_REQUEST['field_name']),
dbesc($_REQUEST['field_type']),
dbesc($_REQUEST['field_desc']),
dbesc($_REQUEST['field_help']),
dbesc($_REQUEST['field_inputs']),
intval($_REQUEST['id'])
);
} else {
2021-12-03 03:01:39 +00:00
$r = q(
"insert into profdef ( field_name, field_type, field_desc, field_help, field_inputs ) values ( '%s' , '%s', '%s', '%s', '%s' )",
2021-12-02 23:02:31 +00:00
dbesc($_REQUEST['field_name']),
dbesc($_REQUEST['field_type']),
dbesc($_REQUEST['field_desc']),
dbesc($_REQUEST['field_help']),
dbesc($_REQUEST['field_inputs'])
);
}
}
// add to chosen array basic or advanced
goaway(z_root() . '/admin/profs');
}
public function get()
{
if ((argc() > 3) && argv(2) == 'drop' && intval(argv(3))) {
2021-12-03 03:01:39 +00:00
$r = q(
"delete from profdef where id = %d",
2021-12-02 23:02:31 +00:00
intval(argv(3))
);
// remove from allowed fields
goaway(z_root() . '/admin/profs');
}
if ((argc() > 2) && argv(2) === 'new') {
2022-02-12 20:43:29 +00:00
return replace_macros(Theme::get_template('profdef_edit.tpl'), array(
2021-12-02 23:02:31 +00:00
'$header' => t('New Profile Field'),
'$field_name' => array('field_name', t('Field nickname'), $_REQUEST['field_name'], t('System name of field')),
'$field_type' => array('field_type', t('Input type'), (($_REQUEST['field_type']) ? $_REQUEST['field_type'] : 'text'), ''),
'$field_desc' => array('field_desc', t('Field Name'), $_REQUEST['field_desc'], t('Label on profile pages')),
'$field_help' => array('field_help', t('Help text'), $_REQUEST['field_help'], t('Additional info (optional)')),
'$submit' => t('Save')
));
}
if ((argc() > 2) && intval(argv(2))) {
2021-12-03 03:01:39 +00:00
$r = q(
"select * from profdef where id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval(argv(2))
);
if (!$r) {
notice(t('Field definition not found') . EOL);
goaway(z_root() . '/admin/profs');
}
2022-02-12 20:43:29 +00:00
return replace_macros(Theme::get_template('profdef_edit.tpl'), array(
2021-12-02 23:02:31 +00:00
'$id' => intval($r[0]['id']),
'$header' => t('Edit Profile Field'),
'$field_name' => array('field_name', t('Field nickname'), $r[0]['field_name'], t('System name of field')),
'$field_type' => array('field_type', t('Input type'), $r[0]['field_type'], ''),
'$field_desc' => array('field_desc', t('Field Name'), $r[0]['field_desc'], t('Label on profile pages')),
'$field_help' => array('field_help', t('Help text'), $r[0]['field_help'], t('Additional info (optional)')),
'$submit' => t('Save')
));
}
$basic = '';
$barr = [];
2022-01-25 01:26:12 +00:00
$fields = Channel::get_profile_fields_basic();
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
if (!$fields) {
2022-01-25 01:26:12 +00:00
$fields = Channel::get_profile_fields_basic(1);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if ($fields) {
foreach ($fields as $k => $v) {
2021-12-03 03:01:39 +00:00
if ($basic) {
2021-12-02 23:02:31 +00:00
$basic .= ', ';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$basic .= trim($k);
$barr[] = trim($k);
}
}
$advanced = '';
2022-01-25 01:26:12 +00:00
$fields = Channel::get_profile_fields_advanced();
2021-12-03 03:01:39 +00:00
if (!$fields) {
2022-01-25 01:26:12 +00:00
$fields = Channel::get_profile_fields_advanced(1);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if ($fields) {
foreach ($fields as $k => $v) {
2021-12-03 03:01:39 +00:00
if (in_array(trim($k), $barr)) {
2021-12-02 23:02:31 +00:00
continue;
2021-12-03 03:01:39 +00:00
}
if ($advanced) {
2021-12-02 23:02:31 +00:00
$advanced .= ', ';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$advanced .= trim($k);
}
}
$all = '';
2022-01-25 01:26:12 +00:00
$fields = Channel::get_profile_fields_advanced(1);
2021-12-02 23:02:31 +00:00
if ($fields) {
foreach ($fields as $k => $v) {
2021-12-03 03:01:39 +00:00
if ($all) {
2021-12-02 23:02:31 +00:00
$all .= ', ';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$all .= trim($k);
}
}
$r = q("select * from profdef where true");
if ($r) {
foreach ($r as $rr) {
2021-12-03 03:01:39 +00:00
if ($all) {
2021-12-02 23:02:31 +00:00
$all .= ', ';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$all .= $rr['field_name'];
}
}
2022-02-12 20:43:29 +00:00
$o = replace_macros(Theme::get_template('admin_profiles.tpl'), array(
2021-12-02 23:02:31 +00:00
'$title' => t('Profile Fields'),
'$basic' => array('basic', t('Basic Profile Fields'), $basic, ''),
'$advanced' => array('advanced', t('Advanced Profile Fields'), $advanced, t('(In addition to basic fields)')),
'$all' => $all,
'$all_desc' => t('All available fields'),
'$cust_field_desc' => t('Custom Fields'),
'$cust_fields' => $r,
'$edit' => t('Edit'),
'$drop' => t('Delete'),
'$new' => t('Create Custom Field'),
'$submit' => t('Submit')
));
return $o;
}
2021-12-03 03:01:39 +00:00
}