Monday, February 20, 2017

BuddyPress remove default "----" dashs 1st value from drop-down list-boxes

BuddyPress Default Drop-Down ListBox Fix

I found the having a default value as "----" as the 1st value from drop-down list-boxes in BuddyPress 2.8.0 very annoying. If this is a required field, then "----" is not a valid value? 

Red X indicates default value

Download, back-up  and edit this file class-bp-xprofile-field-type-selectbox.php located in 

Change the one line in this file to this; 

$html    = ''; // 

Here's the code block and highlighted line is how to change it. Not, next update to BuddyPress will blow this away so back it up.

  * Output the edit field options HTML for this field type.
  * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
  * These are stored separately in the database, and their templating is handled separately.
  * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
  * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
  * Must be used inside the {@link bp_profile_fields()} template loop.
  * @since 2.0.0
  * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
 public function edit_field_options_html( array $args = array() ) {
  $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) );

  $options = $this->field_obj->get_children();
  $html    = ''; //     '<option value="">' . /* translators: no option picked in select box */ esc_html__( '----', 'buddypress' ) . '</option>';

  if ( empty( $original_option_values ) && !empty( $_POST['field_' . $this->field_obj->id] ) ) {
   $original_option_values = sanitize_text_field(  $_POST['field_' . $this->field_obj->id] );

  $option_values = ( $original_option_values ) ? (array) $original_option_values : array();
  for ( $k = 0, $count = count( $options ); $k < $count; ++$k ) {
   $selected = '';

   // Check for updated posted values, but errors preventing them from
   // being saved first time.
   foreach( $option_values as $i => $option_value ) {
    if ( isset( $_POST['field_' . $this->field_obj->id] ) && $_POST['field_' . $this->field_obj->id] != $option_value ) {
     if ( ! empty( $_POST['field_' . $this->field_obj->id] ) ) {
      $option_values[$i] = sanitize_text_field( $_POST['field_' . $this->field_obj->id] );

   // Run the allowed option name through the before_save filter, so
   // we'll be sure to get a match.
   $allowed_options = xprofile_sanitize_data_value_before_save( $options[$k]->name, false, false );

   // First, check to see whether the user-entered value matches.
   if ( in_array( $allowed_options, $option_values ) ) {
    $selected = ' selected="selected"';

   // Then, if the user has not provided a value, check for defaults.
   if ( ! is_array( $original_option_values ) && empty( $option_values ) && $options[$k]->is_default_option ) {
    $selected = ' selected="selected"';

    * Filters the HTML output for options in a select input.
    * @since 1.1.0
    * @param string $value    Option tag for current value being rendered.
    * @param object $value    Current option being rendered for.
    * @param int    $id       ID of the field object being rendered.
    * @param string $selected Current selected value.
    * @param string $k        Current index in the foreach loop.
   $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_html( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k], $this->field_obj->id, $selected, $k );

  echo $html;

Tuesday, February 14, 2017

Copy WPF DataGrid selected rows to clipboard using CopyingRowClipboardContent

Searchedhigh and low for this code, that properly use the e.ClipboardRowContent to copy C# .NET 4.0+ WPF DataGrid  selected rows to clipboard.

For my needs, I'm re-pasting back into the e.ClipboardRowContent the row I would like. The cell.Item is all the information I need for each selected row.

Hint: I was getting duplicates without doing an e.ClipboardRowContent.Clear(); after using the e.ClipboardRowContent . I was clearing before and using DataGrid.SelectedItems to build the rows. 

private void yourDataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
 var dataGridClipboardCellContent = new List<DataGridClipboardCellContent>(); 
 string prevCell = "";
 string curCell = ""; 
 foreach (DataGridClipboardCellContent cell in e.ClipboardRowContent)
  //Gives you access item.Item or item.Content here
        //if you are using your struct (data type) you can recast it here curItem = (yourdatatype)item.Item;  
  curItem = cell.Item.ToString(); 
  if (curCell != prevCell)
   dataGridClipboardCellContent.Add(new DataGridClipboardCellContent(item, item.Column, curCell)); 

  prevCell = curCell;  

 //Re-paste back into e.ClipboardRowContent, additionally if you have modified/formatted rows to your liking


This a more general case

private void dataPaths_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
   IList<DataGridCellInfo> cells = (sender as DataGrid).SelectedCells;

   foreach (DataGridCellInfo cell in cells)
       e.ClipboardRowContent.Add(new DataGridClipboardCellContent(e.Item, cell.Column, null));

   //or write out to a log
   foreach (var row in e.ClipboardRowContent)

Saturday, February 11, 2017

Consolidate or Merge multiple files in multiple locations into 1 (one) file location

This post will explore consolidate or merging multiple files in multiple locations into 1 (one) directory and consolidating repeat files into 1 file using Richcopy, a free easy-to-use utility from Microsoft.

Basically Richcopy GUI tool, exposes some advanced functionality found in Robocopy.  Robocopy is short for robust copy, and is a built-in Windows utility that provides robust file copy, such as copying files without permissions.  Read my post for more info.

Firstly you need to get a copy of RichCopy  as described in this article in TechNet Magazine Utility Spotlight RichCopy, by J. Hoffman

RichCopy download is called
HoffmanUtilitySpotlight2009_04.exe (5,896 KB) after the authors name.  This exe extracts install files, choose to a temporary directory and run the installer setup.exe.

Image 1: RichCopy GUI

A bried background on Robocopy

Robocopy enables the more serious file replication tasks that can really simplify your job. The biggest benefit I think you'll find is the ability to create full mirror duplicates of two file structures (including all sub-directories and files, if you choose) without copying any unnecessary files. Only the files that are new or have been updated in the source location will be copied. Robocopy also allows you to preserve all of the associated file information, including date and time stamps, security access control lists (ACLs) and more.

Robocopy is built into Windows 7, 
Windows 8+, Windows 10+, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2016+ 

In this case, we're discussing the simplest of tasks: copying files. Except copying files is not always that simple. 
  • What if you're copying thousands of files across a slow connection? 
  • What happens if your network hiccups and interrupts the copy? 
  • What if you want to make sure that you preserve particular file attributes, such as a Last Modified date, but not other attributes, like security descriptors? 
  • What if you want to filter the files you're copying from source to destination based on filename or extension?
  • Copy files without permissions and file information
  • Copy files longer than 260 characters 

Consolidate Repeated Files

RichCopy version 4.0 supports specifying multiple source directories. Default behavior is to create directories with same name as source, and make a copy. When this option is selected, RichCopy copy all sources files and directories into specified destination directory without creating directories with same name as source. 

Also, if you have File A in Directory 1 and File A in Directory 2, File A with the latest date will be copied into Destination directory. This behaviour can be altered using Copy always (do not compare source and destination) options, see image 2.

Never have repeat file again, especially when syncing to the cloud.

Consolidate Files Using RichCopy

Step 1 Choose multiple source directories and destination (consolidated) directory. 

NOTE : When this completes, Copy complete only mentions the last path of Source Path. 

Image 1: RichCopy GUI

Step 2 Choose Option button and select "Consolidate multiple sources"

Image 2: RichCopy Options interface

Step 3 Wait until "Copy Complete" appears in Description as in Image 1. Copy complete only mentions the last path of Source Path. This behaviour can be altered using Copy always (do not compare source and destination) options.

Step 4 Done.

Consolidate Multiple Source Results

Here's proof of the the process when using "Consolidate multiple sources" option; 
Our two source directories are c:\temp and c:\backup, and contain the same files NameLengths.txt and PathLengths.txt

Image 3 : Temp source Directory 

Take note of NameLength.txt time-stamp in Backup directory (Image 4) is newer than temp directory (Image 3 above).

Image 4 : Backup source directory

The resultant consolidate directory contains only 1 copy of the all the repeated files. Duplicate files are resolved using the most recent time-stamp. This behaviour can be altered using Copy always (do not compare source and destination) options.

time-stamp in Backup directory (Image 4) is newer than temp directory (Image 3 above) and therefore end-up in the output consolidate_test directory. 

Image 5 : Consolidate directory and files

There you go, a great way to consolidate your mess of duplicate files.