SHARE
TWEET

Untitled

benjamin_mcf Dec 5th, 2011 12 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Administrative page callbacks for the avatar_selection module.
  6.  */
  7.  
  8.  
  9. /**
  10.  * Scan the directory for new files.
  11.  *
  12.  * @param $name
  13.  *   The name of the avatar(s).
  14.  * @param $access
  15.  *   The permission - the value determines which user roles have rights to see
  16.  *   the avatar.
  17.  * @param $og
  18.  *   Organic group (optional).
  19.  * @param $weight
  20.  *   Avatar weight to assign.
  21.  * @return
  22.  *   Number of images found.
  23.  */
  24. function _avatar_selection_scan_images($name, $access, /*$og = array(), */$weight = 0) {
  25.   $avatars = array();
  26.   $add_count = 0;
  27.   $delete_count = 0;
  28.  
  29.   $dir = file_build_uri('avatar_selection');
  30.   file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  31.   $mask = '/.*\.(gif|GIF|Gif|jpg|JPG|Jpg|jpeg|JPEG|Jpeg|png|PNG|Png)/';
  32.   $listings = file_scan_directory($dir, $mask);
  33.  
  34.   $result = db_query("SELECT avatar FROM {avatar_selection} avs");
  35.   foreach ($result as $avatar) {
  36.     $avatars[$avatar->avatar] = $avatar->avatar;
  37.   }
  38.  
  39.   // Search for new files.  Remove matching records from avatars array.
  40.   foreach ($listings as $listing) {
  41.     $filename = str_replace("$dir/", '', $listing->filename);
  42.     if (in_array($filename, $avatars)) {
  43.       unset($avatars[$filename]);
  44.     }
  45.     else {
  46.       _avatar_selection_save_avatar_info(0, $filename, (empty($name) ?  $filename : $name), $access, /*$og, */$weight);
  47.       $add_count++;
  48.     }
  49.   }
  50.  
  51.   // Remove records from database where we have an avatar entry but no
  52.   // corresponding file.
  53.   foreach ($avatars as $avatar) {
  54.     avatar_selection_image_delete($avatar);
  55.     $delete_count++;
  56.   }
  57.  
  58.   $count['add'] = $add_count;
  59.   $count['delete'] = $delete_count;
  60.  
  61.   return $count;
  62. }
  63.  
  64.  
  65. /**
  66.  * Select which form will be shown to the user, according to the permissions.
  67.  *
  68.  * @param $op
  69.  *   Default NULL; the action the user wants to do after the function checks
  70.  &   the permission.
  71.  * @return
  72.  *   Return the structure of the form.
  73.  */
  74. function avatar_selection_settings_page($op = NULL) {
  75.  
  76.   switch ($op) {
  77.     case 'edit':
  78.       $output = drupal_get_form('avatar_selection_config_form');
  79.       break;
  80.     case 'upload':
  81.       $output = drupal_get_form('avatar_selection_upload_form');
  82.       break;
  83.     case 'list':
  84.       $output = drupal_get_form('avatar_selection_edit_form');
  85.       break;
  86.     default:
  87.       $form[] = array(
  88.         '#type' => 'fieldset',
  89.         '#title' => t('Add another'),
  90.       );
  91.       $output = drupal_get_form('avatar_selection_config_form');
  92.       break;
  93.   }
  94.   return $output;
  95. }
  96.  
  97.  
  98. /**
  99.  * Create the form structure for configuring the avatar module settings; seen
  100.  * in the 'Configure' tab under the Avatar Selection administration page.
  101.  *
  102.  * @return
  103.  *   Return the structure of the form.
  104.  */
  105. function avatar_selection_config_form($form) {
  106.   if (!variable_get('user_pictures', 0)) {
  107.     drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  108.   }
  109.  
  110.   // To store how many avatars per page are displayed.
  111.   $form['update']['avatar_per_page'] = array(
  112.     '#type' => 'textfield',
  113.     '#title' => t('How many avatars per page'),
  114.     '#description' => t('The number of avatars to show per page.'),
  115.     '#default_value' => variable_get('avatar_selection_avatar_per_page', 30),
  116.     '#size' => 3,
  117.   );
  118.  
  119.   $form['update']['disable_user_upload'] = array(
  120.     '#type' => 'checkbox',
  121.     '#title' => t('Disable users uploading pictures to profile'),
  122.     '#description' => t('Allow users to pick their avatar from the selection but prevent them from uploading new avatars when editing their account.'),
  123.     '#default_value' => variable_get('avatar_selection_disable_user_upload', FALSE),
  124.   );
  125.   $form['update']['force_set_image_reg'] = array(
  126.     '#type' => 'checkbox',
  127.     '#title' => t('Force users to select an avatar image on user registration.'),
  128.     '#description' => t('This only applies on the user registration screen.'),
  129.     '#default_value' => variable_get('avatar_selection_force_user_avatar_reg', FALSE),
  130.   );
  131.   $form['update']['force_set_image'] = array(
  132.     '#type' => 'checkbox',
  133.     '#title' => t('Force users to select an avatar image when editing their account'),
  134.     '#description' => t('This only applies when the user is editing their account details and image uploads are disabled.'),
  135.     '#default_value' => variable_get('avatar_selection_force_user_avatar', FALSE),
  136.   );
  137.   $form['update']['set_random_default'] = array(
  138.     '#type' => 'checkbox',
  139.     '#title' => t('Enable random default avatar image.'),
  140.     '#description' => t("Automatically set a random avatar image to be used when the user doesn't set one for their account."),
  141.     '#default_value' => variable_get('avatar_selection_set_random_default', FALSE),
  142.   );
  143.   $form['update']['distinctive_avatars'] = array(
  144.     '#type' => 'checkbox',
  145.     '#title' => t('Enable unique avatars.'),
  146.     '#description' => t("Only allow users to pick an avatar that isn't already in use by another user.  If there are no available avatars left, the default avatar image will be used."),
  147.     '#default_value' => variable_get('avatar_selection_distinctive_avatars', FALSE),
  148.   );
  149.  
  150.   if (module_exists('imagecache')) {
  151.     // Load imagecache presets
  152.     $presets = array();
  153.     $presets[] = '';
  154.     foreach (imagecache_presets() as $preset) {
  155.       $presets[$preset['presetname']] = check_plain($preset['presetname']);
  156.     }
  157.  
  158.     $form['update']['imagecache_preset'] = array(
  159.       '#type' => 'select',
  160.       '#title' => t('Imagecache preset'),
  161.       '#default_value' => variable_get('avatar_selection_imagecache_preset', FALSE),
  162.       '#options' => $presets,
  163.       '#description' => t('Choose an imagecache preset to format the avatars in the selection list with.  The <a href="@url">ImageCache Profiles</a> module can be used to format the avatars in other locations.', array('@url' => url('http://drupal.org/project/imagecache_profiles'))),
  164.     );
  165.   }
  166.  
  167.   $subjids = array();  
  168.   if (!empty($form_state['values']['subject'])) {
  169.     $subjids = array_filter($form_state['values']['subject']);
  170.   }
  171.   $gradids = array();
  172.   if (!empty($form_state['values']['grade'])) {
  173.     $gradids = array_filter($form_state['values']['grade']);
  174.   }
  175.   $strandids = array();
  176.   if (!empty($form_state['values']['strand'])) {
  177.     $strandids = array_filter($form_state['values']['strand']);
  178.   }
  179.  
  180.  
  181.   $form['subjectblock'] = array(
  182.         '#type'     => 'fieldset',
  183.         '#title'     => 'Subjects',
  184.   );  
  185.   $form['subjectblock']['subject'] = array(
  186.         '#type'     => 'checkboxes',
  187.         '#options' => grab_subjects(),
  188.         '#prefix' => '<div style="background:#FF7F55;">',
  189.         '#suffix' => '</div>',
  190.         '#ajax' => array(
  191.             'callback' => 'make_topic_chex_callback',
  192.             'progress' => array('type' => 'none'),
  193.         ),
  194.   );
  195.   $form['gradeblock'] = array(
  196.         '#type'     => 'fieldset',
  197.         '#title'     => 'Grades',
  198.   );
  199.   $form['gradeblock']['grade'] = array(
  200.         '#type'     => 'checkboxes',
  201.         '#options' => grab_grades(),
  202.         '#prefix' => '<div style="background:#557FFF;">',
  203.         '#suffix' => '</div>',
  204.         '#ajax' => array(
  205.             'callback' => 'make_topic_chex_callback',
  206.             'progress' => array('type' => 'none'),
  207.         ),
  208.   );
  209.   $form['blanx'] = array(
  210.         '#type'     => 'markup',
  211.         '#prefix' => '<div>',
  212.         '#suffix' => '</div>',
  213.   );  
  214.   $form['strandwrap'] = array(
  215.         '#type'     => 'markup',
  216.         '#prefix' => '<div id="pow">',
  217.         '#suffix' => '</div>',
  218.   );  
  219.   if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  220.       $form['strandwrap']['strandblock'] = array(
  221.             '#type'     => 'fieldset',
  222.             '#title'     => 'Strands',
  223.       );
  224.   }
  225.   if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  226.       $form['strandwrap']['strandblock']['strand'] = array(
  227.             '#type'     => 'checkboxes',
  228.             '#options' => grab_strands($subjids, $gradids),
  229.             '#prefix' => '<div style="background:#FFFF2A;">',
  230.             '#suffix' => '</div>',
  231.             '#ajax' => array(
  232.                 'callback' => 'make_topic_chex_callback',
  233.                 'progress' => array('type' => 'none'),
  234.             ),
  235.       );
  236.   }
  237.   $form['topwrap'] = array(
  238.         '#type'     => 'markup',
  239.         '#prefix' => '<div id="gunge">',
  240.         '#suffix' => '</div>',
  241.   );  
  242.   if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  243.       $form['topwrap']['topblock'] = array(
  244.                 '#type'   => 'fieldset',
  245.                 '#title'  => 'Topics',
  246.       );
  247.   }
  248.   if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  249.       $form['topwrap']['topblock']['topics'] = array(
  250.             '#type'     => 'checkboxes',
  251.             '#options' => grab_topics($subjids, $gradids, $strandids),
  252.             '#prefix' => '<div style="background:#FFAA00;">',
  253.             '#suffix' => '</div>',
  254.       );
  255.   }
  256.  
  257.   $form['update']['submit'] = array(
  258.     '#type' => 'submit',
  259.     '#value' => t('Update'),
  260.   );
  261.  
  262.   return $form;
  263. }
  264.  
  265. /**
  266.  * Create the form structure for uploading an avatar.
  267.  *
  268.  * @return
  269.  *   Return the structure of the form.
  270.  */
  271. function avatar_selection_upload_form($form) {
  272.   if (!variable_get('user_pictures', 0)) {
  273.     drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  274.   }
  275.  
  276.   $form['#attributes']['enctype'] = 'multipart/form-data';
  277.  
  278.   $dir = file_build_uri('avatar_selection');
  279.   file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  280.   $form['bulk_upload'] = array(
  281.     '#type' => 'fieldset',
  282.     '#title' => t('Bulk Upload / Delete'),
  283.     '#description' => t("To upload a large number of avatars, first copy the images manually to the %dir folder, using ftp for example.  To make these new avatar images available, check the 'Scan for new avatars' option. All new images will then be added to the list.  By removing files from this directory and checking the box, you can also perform a bulk delete.", array('%dir' => $dir)),
  284.     '#collapsed' => TRUE,
  285.     '#collapsible' => TRUE,
  286.   );
  287.   $form['bulk_upload']['scan_avatars'] = array(
  288.     '#type' => 'checkbox',
  289.     '#title' => t('Scan avatars'),
  290.     '#description' => t('All new avatar images found will be added to the list of available avatars with the name, weight and permissions defined below.  Scanning for new avatars may be slow depending on the number of files.  All avatar entries which no longer have a corresponding file will be be removed.'),
  291.     '#default_value' => 0,
  292.   );
  293.  
  294.  
  295.   $form['picture_upload'] = array(
  296.     '#type' => 'file',
  297.     '#title' => t('Upload image'),
  298.     '#size' => 48,
  299.     '#description' => t('A new avatar image.  Maximum dimensions are %dimensions and the maximum size is %size kB.  Images must have one of the following extensions (case sensitive): png, jpg, jpeg, gif, PNG, JPG, JPEG, GIF.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', 30))) . ' ' .  variable_get('user_picture_guidelines', ''),
  300.   );
  301.  
  302.   $form['avatar_name'] = array(
  303.     '#type' => 'textfield',
  304.     '#title' => t('Name'),
  305.     '#description' => t("Image name or title which will be displayed when hovering over the image.  It's also used in conjunction with the weight setting for sorting the avatars."),
  306.     '#size' => 48,
  307.   );
  308.   $form['avatar_weight'] = array(
  309.     '#type' => 'weight',
  310.     '#title' => t('Weight'),
  311.     '#delta' => 100,
  312.     '#description' => t('Avatars with a lower weight appear before higher weighted avatars in lists.'),
  313.   );
  314.   $form['permissions'] = array(
  315.     '#type' => 'fieldset',
  316.     '#title' => t('Permissions'),
  317.     '#weight' => 2,
  318.   );
  319.   $form['permissions']['access'] = array(
  320.     '#type' => 'checkboxes',
  321.     '#title' => t('User Roles'),
  322.     '#options' => avatar_selection_handler_filter_role(),
  323.     '#description' => t('Only the checked roles will be able to see this avatar icon; if no roles are checked, access will not be restricted.'),
  324.   );
  325.   //if (module_exists('og')) {
  326.   //  $form['permissions']['og_access'] = array(
  327.   //    '#type' => 'checkboxes',
  328.   //    '#title' => t('Organic Groups'),
  329.   //    '#options' => og_all_groups_options(),
  330.   //    '#description' => t('Only users in the checked organic groups will be able to see this avatar icon; if no groups are checked, access will not be restricted.'),
  331.   //  );
  332.   //}
  333.  
  334.   $form['upload'] = array(
  335.     '#type' => 'submit',
  336.     '#value' => t('Upload'),
  337.     '#weight' => 10,
  338.   );
  339.  
  340.   return $form;
  341. }
  342.  
  343. /**
  344.  * @todo Please document this function.
  345.  * @see http://drupal.org/node/1354
  346.  */
  347. function avatar_selection_roles_page($op = NULL) {
  348.   $output = '';
  349.  
  350.   // Display the form where appropriate.
  351.   if (isset($op) && ($op == 'role' || $op == 'og')) {
  352.     $output = array();
  353.     $output['avatar_selection_edit_form'] = drupal_get_form('avatar_selection_edit_form');
  354.   }
  355.  
  356.   // Display the number of avatars per role / group.
  357.   else {
  358.     $avs_access = array();
  359.     $og_access = array();
  360.     $avs_access[0] = 0;
  361.     $og_access[0] = 0;
  362.  
  363.     // Get the list of access roles and initialise the count to 0.
  364.     $roles = avatar_selection_handler_filter_role();
  365.     foreach ($roles as $rid => $role_name) {
  366.       $avs_access[$rid] = 0;
  367.     }
  368.  
  369.     // Get the list of organic groups and initialise the count to 0.
  370.     //if (module_exists('og')) {
  371.     //  $ogroups = og_all_groups_options();
  372.     //  foreach ($ogroups as $ogid => $ogroup_name) {
  373.     //    $og_access[$ogid] = 0;
  374.     //  }
  375.     //}
  376.  
  377.     // Get the total number of avatars available on the system.
  378.     $total_count = 0;
  379.     $result = db_query("SELECT count(*) AS count FROM {avatar_selection} avs");
  380.     foreach ($result as $avatar) {
  381.       $total_count = $avatar->count;
  382.     }
  383.     $output .= '<p>' . t('There is a total of %count avatars configured.', array('%count' => $total_count)) . '</p>';
  384.  
  385.  
  386.     // Get the count of avatars per role.
  387.     $result = db_query("SELECT avsr.rid, count(*) AS count FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid GROUP BY avsr.rid");
  388.     foreach ($result as $avatar) {
  389.       if (empty($avatar->rid)) {
  390.         $avs_access[0] += $avatar->count;
  391.       }
  392.       else {
  393.         $avs_access[$avatar->rid] += $avatar->count;
  394.       }
  395.     }
  396.  
  397.     // Get the count of avatars per organic group.
  398.     $result = db_query("SELECT avso.ogid, count(*) AS count FROM {avatar_selection} avs LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid GROUP BY avso.ogid");
  399.     foreach ($result as $avatar) {
  400.       if (empty($avatar->ogid)) {
  401.         $og_access[0] += $avatar->count;
  402.       }
  403.       else {
  404.         $og_access[$avatar->ogid] += $avatar->count;
  405.       }
  406.     }
  407.  
  408.     // Format the user roles table.
  409.     $avs_rows = array();
  410.     $header = array(t('User Role'), t('Number of Avatars'));
  411.     $edit = l(t('edit'), 'admin/config/people/avatar_selection/edit/role/0');
  412.     $avs_rows[] = array(t('Available to all roles'), $avs_access[0], $edit);
  413.     foreach ($roles as $rid => $role_name) {
  414.       $edit = l(t('edit'), 'admin/config/people/avatar_selection/edit/role/' . $rid);
  415.       $avs_rows[] = array($role_name, $avs_access[$rid], $edit);
  416.     }
  417.     $output .= theme('table', array('header' => $header, 'rows' => $avs_rows));
  418.  
  419.  
  420.     // Format the organic groups table.
  421.     /*if (module_exists('og')) {
  422.       $og_rows = array();
  423.       $header = array(t('Organic Group'), t('Number of Avatars'));
  424.       $edit = l(t('edit'), 'admin/config/people/avatar_selection/edit/og/0');
  425.       $og_rows[] = array(t('Available to all groups'), $og_access[0], $edit);
  426.       foreach ($ogroups as $ogid => $ogroup_name) {
  427.         $edit = l(t('edit'), 'admin/config/people/avatar_selection/edit/og/' . $ogid);
  428.         $og_rows[] = array($ogroup_name, $og_access[$ogid], $edit);
  429.       }
  430.       $output .= theme('table', array('header' => $header, 'rows' => $og_rows));
  431.     }*/
  432.   }
  433.  
  434.   return $output;
  435. }
  436.  
  437.  
  438. /**
  439.  * Create the form structure for listing the avatars and managing them in the
  440.  * 'Manage Avatars' tab under the Avatar Selection administration page.
  441.  *
  442.  * @param $form_state
  443.  *   General variable, used to control the processing of the form.
  444.  * @return
  445.  *   Return the structure of the form.
  446.  */
  447. function avatar_selection_edit_form($form, $form_state) {
  448.   // We need the pager global variables.
  449.   global $_GET;
  450.  
  451.   $form = array();
  452.  
  453.   if (!variable_get('user_pictures', 0)) {
  454.     drupal_set_message(t('User Pictures option is disabled.  You will need to enable this option before you can use the Avatar Selection module.  You may configure this setting on the <a href="@url">User settings</a> page.', array('@url' => url('admin/user/settings'))));
  455.   }
  456.  
  457.   drupal_add_css(drupal_get_path('module', 'avatar_selection') . '/avatar_selection.css');
  458.  
  459.   $set_type = arg(5);
  460.   $set_id = arg(6);
  461.  
  462.   // We find out the current page number.
  463.   $page = 0;
  464.   if (isset($_GET['page']) && is_numeric($_GET['page'])) {
  465.     $page = $_GET['page'];
  466.   }
  467.  
  468.   $avatars_per_page = variable_get('avatar_selection_avatar_per_page', 30);
  469.   $selects = _avatar_selection_image_list('', $set_type, $set_id, $page * $avatars_per_page, $avatars_per_page);
  470.   if (!count($selects['avatars'])) {
  471.     drupal_set_message(t('There are no avatars configured.'));
  472.   }
  473.  
  474.   else {
  475.  
  476.     if (!isset($form_state['values'])) {
  477.       $step = 'list';
  478.     }
  479.     else {
  480.       $step = 'edit';
  481.     }
  482.     $form['step'] = array(
  483.       '#type' => 'value',
  484.       '#value' => $step,
  485.     );
  486.  
  487.  
  488.     if ($step == 'list') {
  489.  
  490.       if ($set_type == 'role') {
  491.         $sets = avatar_selection_handler_filter_role();
  492.       }
  493.       elseif ($set_type == 'og') {
  494.         $sets = og_all_groups_options();
  495.       }
  496.       $sets[0] = t('All roles');
  497.       drupal_set_title(t('Manage Avatars - %name', array('%name' => $sets[$set_id])), PASS_THROUGH);
  498.       drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js');
  499.       $js_file = drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection.js';
  500.  
  501.       $form['select_avatar'] = array(
  502.         '#type' => 'radios',
  503.         '#title' => t('Select an avatar to edit'),
  504.         '#options' => $selects['avatars'],
  505.         '#required' => TRUE,
  506.         '#attributes' => array('class' => array('user_avatar_select')),
  507.         '#suffix' => theme('avatar_selection_pager', array('form_id' => 'form#avatar-selection-edit-form', 'class' => 'div.user_avatar_select', 'total' => $selects['total'], 'limit', $avatars_per_page, 'js_file', '/' . $js_file)),
  508.       );
  509.  
  510.       $form['search'] = array(
  511.         '#type' => 'submit',
  512.         '#value' => t('Edit'),
  513.         '#submit' => array('avatar_selection_edit_list_form_submit'),
  514.       );
  515.  
  516.       drupal_add_js($js_file);
  517.  
  518.     }
  519.     elseif ($step == 'edit') {
  520.       drupal_set_title(t('Manage Avatars'));
  521.       $form_state['#redirect'] = array('admin/config/people/avatar_selection/edit');
  522.       $roles = avatar_selection_handler_filter_role();
  523.       $aid = 0;
  524.       $avs_access = $og_access = array();
  525.       $weight = 0;
  526.       $name = '';
  527.  
  528.       $result = db_query("SELECT avs.aid, avatar, name, weight, rid, ogid FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE avatar = :avatar ORDER BY weight, name, avatar", array(':avatar' => $form_state['values']['select_avatar']));
  529.       foreach ($result as $avatar) {
  530.         $aid = $avatar->aid;
  531.         $name = $avatar->name;
  532.         $weight = $avatar->weight;
  533.         if ($avatar->rid) {
  534.           array_push($avs_access, $avatar->rid);
  535.         }
  536.         if ($avatar->ogid) {
  537.           array_push($og_access, $avatar->ogid);
  538.         }
  539.       }
  540.  
  541.       $image_path = file_build_uri('avatar_selection');
  542.       file_prepare_directory($image_path, FILE_CREATE_DIRECTORY);
  543.       $selected_avatar = $form_state['values']['select_avatar'];
  544.       $image = theme('image', array('path' => $image_path . '/' . $selected_avatar));
  545.       $form['avatar_image'] = array('#value' => $image);
  546.       $form['aid'] = array(
  547.         '#type' => 'value',
  548.         '#value' => $aid,
  549.       );
  550.  
  551.       $form['select_avatar'] = array(
  552.         '#type' => 'value',
  553.         '#value' => $form_state['values']['select_avatar'],
  554.       );
  555.  
  556.       $form['avatar_name'] = array(
  557.         '#type' => 'textfield',
  558.         '#title' => t('Name'),
  559.         '#description' => t("Image name or title which will be displayed when hovering over the image.  It's also used in conjunction with the weight setting for sorting the avatars."),
  560.         '#size' => 48,
  561.         '#default_value' => $name,
  562.       );
  563.       $form['avatar_weight'] = array(
  564.         '#type' => 'weight',
  565.         '#title' => t('Weight'),
  566.         '#delta' => 100,
  567.         '#description' => t('Avatars with a lower weight appear before higher weighted avatars in lists.'),
  568.         '#default_value' => $weight,
  569.       );
  570.       $form['permissions'] = array(
  571.         '#type' => 'fieldset',
  572.         '#title' => t('Permissions'),
  573.         '#weight' => 1,
  574.       );
  575.       $form['permissions']['access'] = array(
  576.         '#type' => 'checkboxes',
  577.         '#title' => t('User Roles'),
  578.         '#default_value' => $avs_access,
  579.         '#options' => $roles,
  580.         '#description' => t('Only the checked roles will be able to see this avatar icon; if no roles are checked, access will not be restricted.'),
  581.       );
  582.  
  583.       /*if (module_exists('og')) {
  584.         $form['permissions']['og_access'] = array(
  585.           '#type' => 'checkboxes',
  586.           '#title' => t('Organic Groups'),
  587.           '#default_value' => $og_access,
  588.           '#options' => og_all_groups_options(),
  589.           '#description' => t('Only users in the checked organic groups will be able to see this avatar icon; if no groups are checked, access will not be restricted.'),
  590.         );
  591.       }*/
  592.  
  593.       $form['update'] = array(
  594.         '#type' => 'submit',
  595.         '#value' => t('Update'),
  596.         '#weight' => 9,
  597.         '#submit' => array('avatar_selection_edit_update_form_submit'),
  598.       );
  599.       $form['delete'] = array(
  600.         '#type' => 'submit',
  601.         '#value' => t('Delete'),
  602.         '#weight' => 10,
  603.         '#submit' => array('avatar_selection_edit_delete_form_submit'),
  604.       );
  605.     }
  606.  
  607.   }
  608.  
  609.   return $form;
  610. }
  611.  
  612.  
  613. /**
  614.  * Validate the submission.
  615.  *
  616.  * Ensure the number of avatars page setting is numeric.
  617.  *
  618.  * @param $form
  619.  *   General variable used in drupal, defining the structure & the fields of a
  620.  *   form.
  621.  * @param &$form_state
  622.  *   General reference, used to control the processing of the form.
  623.  */
  624. function avatar_selection_config_form_validate($form, &$form_state) {
  625.   $error = FALSE;
  626.  
  627.   if ($form_state['values']['op'] == t('Update')) {
  628.     if (!is_numeric($form_state['values']['avatar_per_page'])) {
  629.       form_set_error('avatar_per_page', t('Must be a number.'));
  630.       $error = TRUE;
  631.     }
  632.   }
  633. }
  634.  
  635.  
  636.  
  637. /**
  638.  * Validate the submission.
  639.  *
  640.  * Check if Delete has been chosen AND a checkbox has been selected.
  641.  *
  642.  * @param $form
  643.  *   General variable used in drupal, defining the structure & the fields of a
  644.  *   form.
  645.  * @param &$form_state
  646.  *   General reference, used to control the processing of the form.
  647.  */
  648. function avatar_selection_delete_form_validate($form, &$form_state) {
  649.   if ($form_state['values']['op'] == t('Delete')) {
  650.     if (count(array_filter($form_state['values']['images'])) == 0) {
  651.       form_set_error('images', t('Please select images to delete.'));
  652.     }
  653.   }
  654. }
  655.  
  656. /**
  657.  * Submit the settings form in the Avatar Selection administration page.
  658.  *
  659.  * @param $form
  660.  *   General variable used in drupal, defining the structure & the fields of a
  661.  *   form.
  662.  * @param &$form_state
  663.  *   General reference, used to control the processing of the form.
  664.  */
  665. function avatar_selection_config_form_submit($form, &$form_state) {
  666.   $op = $form_state['values']['op'];
  667.  
  668.   if ($op == t('Update')) {
  669.     // Save system variables.
  670.     variable_set('avatar_selection_disable_user_upload', $form_state['values']['disable_user_upload']);
  671.     variable_set('avatar_selection_force_user_avatar_reg', $form_state['values']['force_set_image_reg']);
  672.     variable_set('avatar_selection_force_user_avatar', $form_state['values']['force_set_image']);
  673.     variable_set('avatar_selection_avatar_per_page', $form_state['values']['avatar_per_page']);
  674.     variable_set('avatar_selection_set_random_default', $form_state['values']['set_random_default']);
  675.     variable_set('avatar_selection_distinctive_avatars', $form_state['values']['distinctive_avatars']);
  676.     if (module_exists('imagecache')) {
  677.       variable_set('avatar_selection_imagecache_preset', $form_state['values']['imagecache_preset']);
  678.     }
  679.     drupal_set_message(t('Configuration has been updated.'));
  680.   }
  681. }
  682.  
  683. /**
  684.  * Submit the image upload form in the Avatar Selection administration page.
  685.  *
  686.  * @param $form
  687.  *   General variable used in drupal, defining the structure & the fields of a
  688.  *   form.
  689.  * @param &$form_state
  690.  *   General reference, used to control the processing of the form.
  691.  */
  692. function avatar_selection_upload_form_submit($form, &$form_state) {
  693.   $op = $form_state['values']['op'];
  694.  
  695.   if ($op == t('Upload')) {
  696.     // Get access settings.
  697.     $access = array_keys(array_filter($form_state['values']['access']));
  698.     $name = $form_state['values']['avatar_name'];
  699.     $weight = $form_state['values']['avatar_weight'];
  700.  
  701. //    $og = array();
  702. //    if (module_exists('og')) {
  703. //      $og = array_keys(array_filter($form_state['values']['og_access']));
  704. //    }
  705.  
  706.     // Scan for new files.
  707.     //if ($form_state['values']['scan_avatars'] == 1) {
  708.     //  $count = _avatar_selection_scan_images($name, $access, /*$og, */$weight);
  709.     //  drupal_set_message(t('Scan complete: %added new avatars found. %deleted avatars removed.', array('%added' => $count['add'], '%deleted' => $count['delete'])));
  710.     //}
  711.  
  712.     // Save uploaded files.
  713.     $dir = file_build_uri('avatar_selection');
  714.     $is_writable = file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  715.  
  716.     if ($is_writable) {
  717.       // If required, validate the uploaded picture.
  718.       $validators = array(
  719.         'file_validate_is_image' => array(),
  720.         'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
  721.         'file_validate_size' => array(variable_get('user_picture_file_size', 30) * 1024),
  722.       );
  723.  
  724.       if ($file = file_save_upload('picture_upload', $validators, $dir, FILE_EXISTS_RENAME)) {
  725.  
  726.         if (image_get_info($file->uri)) {
  727.           $file = file_save($file);
  728.         }
  729.         else {
  730.           file_delete($file->uri);
  731.           drupal_set_message(t('Uploaded file does not appear to be a valid image file. Please try again.'));
  732.         }
  733.       }
  734.       global $user;
  735.       $info = image_get_info($file->uri);
  736.       $picture_directory =  file_default_scheme() . '://' . 'avatar_selection';
  737.  
  738.       // Prepare the pictures directory.
  739.       file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
  740.       $destination = file_stream_wrapper_uri_normalize($picture_directory . '/avatar-' . $user->uid . '-' . REQUEST_TIME . '.' . $info['extension']);
  741.  
  742.      
  743.      
  744.       if ($file = file_move($file, $destination, FILE_EXISTS_RENAME)) {
  745.         $file->status = FILE_STATUS_PERMANENT;
  746.         $file = file_save($file);
  747.         _avatar_selection_save_avatar_info(0, $file->filename, $name, $access, /*$og, */$weight);
  748.         drupal_set_message(t('New image saved.'));
  749.       }
  750.     }
  751.     else {
  752.       form_set_error('picture_upload', t('Directory not writable: !dir', array('!dir' => $dir)));
  753.     }
  754.   }
  755. }
  756.  
  757. /**
  758.  * Submit the image list form in the Avatar Selection - 'Manage Avatars' page.
  759.  *
  760.  * Function called when the Edit button is pressed.
  761.  * Practically it sets the $form_state['rebuild'] value to true, which will
  762.  * determine the rebuilding of the page.
  763.  *
  764.  * @param $form
  765.  *   General variable used in drupal, defining the structure & the fields of a
  766.  *   form.
  767.  * @param &$form_state
  768.  *   General reference, used to control the processing of the form.
  769.  */
  770. function avatar_selection_edit_list_form_submit($form, &$form_state) {
  771.   $form_state['rebuild'] = TRUE;
  772. }
  773.  
  774. /**
  775.  * Submit the image list  form in the Avatar Selection - 'Manage Avatars' page.
  776.  *
  777.  * Function called when the Update button is pressed.
  778.  *
  779.  * @param $form
  780.  *   General variable used in drupal, defining the structure & the fields of a
  781.  *   form.
  782.  * @param &$form_state
  783.  *   General reference, used to control the processing of the form.
  784.  */
  785. function avatar_selection_edit_update_form_submit($form, &$form_state) {
  786.   /*$og = array();*/
  787.   $access = array_keys(array_filter($form_state['values']['access']));
  788.   /*if (module_exists('og')) {
  789.     $og = array_keys(array_filter($form_state['values']['og_access']));
  790.   }*/
  791.   $file = $form_state['values']['select_avatar'];
  792.   $name = $form_state['values']['avatar_name'];
  793.   $weight = $form_state['values']['avatar_weight'];
  794.   $aid = $form_state['values']['aid'];
  795.   _avatar_selection_save_avatar_info($aid, $file, $name, $access,/* $og, */$weight);
  796.   drupal_set_message(t('Image updated.'));
  797. }
  798.  
  799. /**
  800.  * Submit the image list form in the Avatar Selection - 'Manage Avatars' page.
  801.  *
  802.  * Function called when the Delete button is pressed, and deletes the selected
  803.  * avatar(s).
  804.  *
  805.  * @param $form
  806.  *   General variable used in drupal, defining the structure & the fields of a
  807.  *   form.
  808.  * @param &$form_state
  809.  *   General reference, used to control the processing of the form.
  810.  */
  811. function avatar_selection_edit_delete_form_submit($form, &$form_state) {
  812.   $image = $form_state['values']['select_avatar'];
  813.   $deleted = avatar_selection_image_delete($image);
  814.   if ($deleted) {
  815.     drupal_set_message(t('Image deleted.'));
  816.   }
  817. }
  818.  
  819. /**
  820.  * Delete the specified avatar image.
  821.  *
  822.  * @param $image
  823.  *   Path to the image to be deleted.
  824.  */
  825. function avatar_selection_image_delete($image) {
  826.   $dir = file_build_uri('avatar_selection');
  827.   file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  828.   if (file_check_location($dir . '/' . $image, $dir)) {
  829.     $aid = db_query("SELECT aid FROM {avatar_selection} WHERE avatar = :avatar", array(':avatar' => $image))->fetchField();
  830.     if ($aid) {
  831.       $result = db_delete('avatar_selection')
  832.         ->condition('aid', $aid)
  833.         ->execute();
  834.       $result = db_delete('avatar_selection_roles')
  835.         ->condition('aid', $aid)
  836.         ->execute();
  837.       $result = db_delete('avatar_selection_og')
  838.         ->condition('aid', $aid)
  839.         ->execute();
  840.       file_delete($dir . '/' . $image);
  841.       if (!image_get_info($dir . '/' . $image)) {
  842.         return 1;
  843.       }
  844.     }
  845.   }
  846.   return 0;
  847. }
  848.  
  849. /**
  850.  * Create the SQL queries in order to save the avatar path and data into the
  851.  * database, and perform them according to the SQL server type.
  852.  *
  853.  * @param $aid
  854.  *   The avatar identifier.
  855.  * @param $image
  856.  *   The avatar image.
  857.  * @param $name
  858.  *   The avatar name, used as the image alternative text on the forms.
  859.  * @param $access
  860.  *   Array of roles - the value determines which user roles have rights to see
  861.  *   the avatar.
  862.  * @param $og
  863.  *   Array of organic groups (optional).
  864.  * @param $weight
  865.  *   The weight of the avatar.  Lower weighted avatars appear before higher
  866.  *   weighted avatars in the list.
  867.  */
  868. function _avatar_selection_save_avatar_info($aid, $image, $name, $access, /*$og = array(), */$weight = 0) {
  869.  
  870.   // Add or update avatar_selection table.
  871.   if ($aid) {
  872.     $result = db_update('avatar_selection')
  873.       ->fields(array(
  874.           'name' => $name,
  875.           'weight' => $weight,
  876.         ))
  877.       ->condition('aid', $aid)
  878.       ->condition('avatar', $image)
  879.       ->execute();
  880.     $result = db_delete('avatar_selection_roles')
  881.       ->condition('aid', $aid)
  882.       ->execute();
  883.   }
  884.   else {
  885.     $aid = db_insert('avatar_selection')
  886.         ->fields(array(
  887.           'avatar' => $image,
  888.           'name' => $name,
  889.           'weight' => $weight,
  890.         ))
  891.         ->execute();
  892.   }
  893.  
  894.   // Add access settings.
  895.   if (is_array($access) && count($access)) {
  896.     foreach ($access as $rid) {
  897.       $id = db_insert('avatar_selection_roles')
  898.         ->fields(array(
  899.           'aid' => $aid,
  900.           'rid' => $rid,
  901.         ))
  902.         ->execute();
  903.     }
  904.   }
  905.   /*
  906.   if (is_array($og) && count($og)) {
  907.     foreach ($og as $ogid) {
  908.       $id = db_insert('avatar_selection_og')
  909.         ->fields(array(
  910.           'aid' => $aid,
  911.           'ogid' => $ogid,
  912.         ))
  913.         ->execute();
  914.     }
  915.   }
  916.   */
  917. }
  918.  
  919. /**
  920.  * Return a list of the existing roles.
  921.  *
  922.  * @return
  923.  *   Return the role id(s).
  924.  */
  925. function avatar_selection_handler_filter_role() {
  926.   $rids = array();
  927.   $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  928.   foreach ($result as $obj) {
  929.     $rids[$obj->rid] = $obj->name;
  930.   }
  931.   return $rids;
  932. }
  933.  
  934.  
  935.  
  936.  
  937.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top