Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/issue 1507 #1511

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Model/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,35 @@ public function getLocationIDs(): array {
}
}

/**
* @return string
*/
public function getLocationString(): string {
switch ( $this->getMeta(self::META_LOCATION_SELECTION_TYPE) ) {
case self::SELECTION_MANUAL_ID:
$locations = $this->getLocations();
if ( count($locations) === 1 ) {
return reset($locations)->post_title;
}
else {
return __('Multiple', 'commonsbooking');
}
case self::SELECTION_ALL_ID:
return __('All', 'commonsbooking');
case self::SELECTION_CATEGORY_ID:
return __('Multiple (by category)', 'commonsbooking');
default:
//backwards compatible for tfs that don't have a defined selection option yet
$locations = $this->getLocations();
if ( count($locations) === 1 ) {
return reset($locations)->post_title;
}
else {
return '-';
}
}
}

/**
* Get the corresponding single item for a timeframe.
* Will get corresponding item object for this timeframe.
Expand Down Expand Up @@ -453,6 +482,40 @@ public function getItemIDs(): array {
}
}

/**
* Will return the assigned item as a string.
* When one item is assigned this is the post title,
* if multiple items are assigned this will return "Multiple"
* and "All" for all. This is used to display the item assignment in the backend.
* @return string
*/
public function getItemString(): string {
$id = $this->ID;
switch ( $this->getMeta(self::META_ITEM_SELECTION_TYPE) ) {
case self::SELECTION_MANUAL_ID:
$items = $this->getItems();
if ( count($items) === 1 ) {
return reset($items)->post_title;
}
else {
return __('Multiple', 'commonsbooking');
}
case self::SELECTION_ALL_ID:
return __('All', 'commonsbooking');
case self::SELECTION_CATEGORY_ID:
return __('Multiple (by category)', 'commonsbooking');
default:
//backwards compatible for tfs that don't have a defined selection option yet
$items = $this->getItems();
if ( count($items) === 1 ) {
return reset($items)->post_title;
}
else {
return '-';
}
}
}

/**
* Returns type id
* The type of the timeframe are constants defined in @see \CommonsBooking\Wordpress\CustomPostType\Timeframe
Expand Down
10 changes: 10 additions & 0 deletions src/Wordpress/CustomPostType/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ public function setCustomColumnsData( $column, $post_id ) {
}


$timeframe = new \CommonsBooking\Model\Timeframe( $post_id );
if ( $value = get_post_meta( $post_id, $column, true ) ) {
switch ( $column ) {
case 'location-id':
Expand Down Expand Up @@ -1228,6 +1229,15 @@ public function setCustomColumnsData( $column, $post_id ) {
break;
}
} else {
switch ( $column ) {
case 'location-id':
echo $timeframe->getLocationString();
break;
case 'item-id':
echo $timeframe->getItemString();
break;
}

$bookingColumns = [
'post_date',
'post_status',
Expand Down
24 changes: 24 additions & 0 deletions tests/php/Model/TimeframeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,18 @@ public function testGetLocationIDs() {
$this->assertEquals([$this->locationId],$this->firstTimeframe->getLocationIDs());
}

public function testGetLocationString() {
$this->assertEquals($this->firstLocation->post_title,$this->firstTimeframe->getLocationString());
$allHoliday = new Timeframe( $this->createHolidayTimeframeForAllItemsAndLocations() );
$this->assertEquals("All",$allHoliday->getLocationString());
//update postmeta to seem like a manual selection of all locations
update_post_meta($allHoliday->ID, Timeframe::META_LOCATION_SELECTION_TYPE, Timeframe::SELECTION_MANUAL_ID);
$this->assertEquals("Multiple",$allHoliday->getLocationString());
//update postmeta to seem like category selection
update_post_meta($allHoliday->ID, Timeframe::META_LOCATION_SELECTION_TYPE, Timeframe::SELECTION_CATEGORY_ID);
$this->assertEquals("Multiple (by category)",$allHoliday->getLocationString());
}

public function testGetItem() {
$item = New Item($this->itemId);
$this->assertEquals($item,$this->firstTimeframe->getItem());
Expand All @@ -808,6 +820,18 @@ public function testGetItemID() {
$this->assertEquals($this->itemId,$this->firstTimeframe->getItemID());
}

public function testGetItemString() {
$this->assertEquals($this->firstItem->post_title,$this->firstTimeframe->getItemString());
$allHoliday = new Timeframe( $this->createHolidayTimeframeForAllItemsAndLocations() );
$this->assertEquals("All",$allHoliday->getItemString());
//update postmeta to seem like a manual selection of all items
update_post_meta($allHoliday->ID, Timeframe::META_ITEM_SELECTION_TYPE, Timeframe::SELECTION_MANUAL_ID);
$this->assertEquals("Multiple",$allHoliday->getItemString());
//update postmeta to seem like category selection
update_post_meta($allHoliday->ID, Timeframe::META_ITEM_SELECTION_TYPE, Timeframe::SELECTION_CATEGORY_ID);
$this->assertEquals("Multiple (by category)",$allHoliday->getItemString());
}

public function testGetItems() {
//for just one item
$singleItem = $this->validTF->getItems();
Expand Down
Loading