Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P926
ExampleCustomCategoryField.php
Active
Public
Actions
Authored by
epriestley
on Sep 16 2013, 10:25 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Flag For Later
Award Token
Tags
None
Referenced Files
F58422: ExampleCustomCategoryField.php
Feb 24 2016, 1:31 AM
2016-02-24 01:31:54 (UTC+0)
Subscribers
None
<?php
final
class
ExampleCustomCategoryField
extends
ManiphestCustomField
{
private
$value
;
public
function
getFieldKey
()
{
return
'example:category'
;
}
public
function
getFieldName
()
{
return
pht
(
'Category'
);
}
private
function
getCategories
()
{
return
array
(
'fruit'
=>
array
(
'name'
=>
'Fruit'
,
'subcategories'
=>
array
(
'apple'
=>
'Apple'
,
'banana'
=>
'Banana'
,
),
),
'vegetables'
=>
array
(
'name'
=>
'Vegetables'
,
'subcategories'
=>
array
(
'carrots'
=>
'Carrot'
,
'celery'
=>
'Celery'
,
),
),
'other'
=>
array
(
'name'
=>
'Other'
,
'subcategories'
=>
array
(
'misc'
=>
'Miscellaneous'
,
'etc'
=>
'Et Cetera'
,
),
),
);
}
public
function
shouldAppearInEditView
()
{
return
true
;
}
public
function
shouldAppearInApplicationSearch
()
{
return
true
;
}
public
function
buildFieldIndexes
()
{
$indexes
=
array
();
// Create two indexes for this field: the subcategory, and the category
// the subcategory is a member of. This allows you to search for either
// "Vegetables" or "Carrots" to find things in the "Carrots" category.
$value
=
$this
->
value
;
if
(
$value
)
{
$indexes
[]
=
$this
->
newStringIndex
(
$value
);
foreach
(
$this
->
getCategories
()
as
$key
=>
$categories
)
{
foreach
(
$categories
[
'subcategories'
]
as
$subkey
=>
$subname
)
{
if
(
$subkey
==
$value
)
{
$indexes
[]
=
$this
->
newStringIndex
(
$key
);
break
2
;
}
}
}
}
return
$indexes
;
}
public
function
readApplicationSearchValueFromRequest
(
PhabricatorApplicationSearchEngine
$engine
,
AphrontRequest
$request
)
{
return
$request
->
getArr
(
$this
->
getFieldKey
());
}
public
function
applyApplicationSearchConstraintToQuery
(
PhabricatorApplicationSearchEngine
$engine
,
PhabricatorCursorPagedPolicyAwareQuery
$query
,
$value
)
{
if
(
$value
)
{
$query
->
withApplicationSearchContainsConstraint
(
$this
->
newStringIndex
(
null
),
$value
);
}
}
public
function
appendToApplicationSearchForm
(
PhabricatorApplicationSearchEngine
$engine
,
AphrontFormView
$form
,
$value
,
array
$handles
)
{
if
(!
is_array
(
$value
))
{
$value
=
array
();
}
$value
=
array_fuse
(
$value
);
$control
=
id
(
new
AphrontFormCheckboxControl
())
->
setLabel
(
$this
->
getFieldName
())
->
setName
(
$this
->
getFieldKey
());
foreach
(
$this
->
getCategories
()
as
$key
=>
$category
)
{
$control
->
addCheckbox
(
$this
->
getFieldKey
().
'[]'
,
$key
,
phutil_tag
(
'strong'
,
array
(),
$category
[
'name'
]),
isset
(
$value
[
$key
]));
foreach
(
$category
[
'subcategories'
]
as
$subkey
=>
$subname
)
{
$control
->
addCheckbox
(
$this
->
getFieldKey
().
'[]'
,
$subkey
,
"
\x
C2
\x
A0
\x
C2
\x
A0"
.
$subname
,
isset
(
$value
[
$subkey
]));
}
}
$form
->
appendChild
(
$control
);
}
public
function
renderEditControl
()
{
$options
=
array
();
foreach
(
$this
->
getCategories
()
as
$category
)
{
$options
[
$category
[
'name'
]]
=
$category
[
'subcategories'
];
}
return
id
(
new
AphrontFormSelectControl
())
->
setName
(
$this
->
getFieldKey
())
->
setLabel
(
$this
->
getFieldName
())
->
setValue
(
$this
->
value
)
->
setOptions
(
$options
);
}
public
function
shouldUseStorage
()
{
return
true
;
}
public
function
setValueFromStorage
(
$value
)
{
$this
->
value
=
$value
;
return
$this
;
}
public
function
getValueForStorage
()
{
return
$this
->
value
;
}
public
function
readValueFromRequest
(
AphrontRequest
$request
)
{
$this
->
value
=
$request
->
getStr
(
$this
->
getFieldKey
());
return
$this
;
}
public
function
shouldAppearInPropertyView
()
{
return
true
;
}
public
function
renderTransactionDescription
(
ManiphestTransaction
$transaction
,
$target
)
{
$name
=
$this
->
getCategoryName
(
$transaction
->
getNewValue
());
return
'categorized this task as "'
.
$name
.
'"'
;
}
public
function
renderPropertyViewValue
()
{
return
$this
->
getCategoryName
(
$this
->
value
);
}
private
function
getCategoryName
(
$value
)
{
foreach
(
$this
->
getCategories
()
as
$category
)
{
foreach
(
$category
[
'subcategories'
]
as
$key
=>
$name
)
{
if
(
$key
==
$value
)
{
return
$name
;
}
}
}
return
null
;
}
}
Event Timeline
epriestley
edited the content of this paste.
(Show Details)
Sep 16 2013, 10:25 PM
2013-09-16 22:25:24 (UTC+0)
epriestley
changed the title of this paste from untitled to
ExampleCustomCategoryField.php
.
epriestley
updated the paste's language from
autodetect
to
autodetect
.
Log In to Comment