55namespace Icinga \Module \Notifications \Controllers ;
66
77use Icinga \Module \Notifications \Common \Links ;
8+ use Icinga \Module \Notifications \Model \Channel ;
89use Icinga \Module \Notifications \View \ContactRenderer ;
910use Icinga \Module \Notifications \Web \Control \SearchBar \ObjectSuggestions ;
1011use Icinga \Module \Notifications \Common \Database ;
1112use Icinga \Module \Notifications \Model \Contact ;
1213use Icinga \Module \Notifications \Web \Form \ContactForm ;
1314use Icinga \Module \Notifications \Widget \ItemList \ObjectList ;
1415use Icinga \Web \Notification ;
16+ use ipl \Html \Contract \Form ;
17+ use ipl \Html \TemplateString ;
1518use ipl \Sql \Connection ;
19+ use ipl \Sql \Expression ;
1620use ipl \Stdlib \Filter ;
1721use ipl \Web \Compat \CompatController ;
1822use ipl \Web \Compat \SearchControls ;
1923use ipl \Web \Control \LimitControl ;
2024use ipl \Web \Control \SortControl ;
2125use ipl \Web \Filter \QueryString ;
2226use ipl \Web \Layout \MinimalItemLayout ;
27+ use ipl \Web \Widget \ActionLink ;
2328use ipl \Web \Widget \ButtonLink ;
2429
2530class ContactsController extends CompatController
@@ -48,8 +53,8 @@ public function indexAction()
4853 $ sortControl = $ this ->createSortControl (
4954 $ contacts ,
5055 [
51- 'full_name ' => t ('Full Name ' ),
52- 'changed_at ' => t ('Changed At ' )
56+ 'full_name ' => $ this -> translate ('Full Name ' ),
57+ 'changed_at ' => $ this -> translate ('Changed At ' )
5358 ]
5459 );
5560
@@ -79,15 +84,32 @@ public function indexAction()
7984 $ this ->addControl ($ sortControl );
8085 $ this ->addControl ($ limitControl );
8186 $ this ->addControl ($ searchBar );
82- $ this ->addContent (
83- (new ButtonLink (t ('Add Contact ' ), Links::contactAdd (), 'plus ' ))
84- ->setBaseTarget ('_next ' )
85- ->addAttributes (['class ' => 'add-new-component ' ])
86- );
87+
88+ $ addButton = (new ButtonLink (
89+ $ this ->translate ('Add Contact ' ),
90+ Links::contactAdd (),
91+ 'plus ' ,
92+ ['class ' => 'add-new-component ' ]
93+ ))->setBaseTarget ('_next ' );
94+
95+ $ emptyStateMessage = null ;
96+ if (Channel::on ($ this ->db )->columns ([new Expression ('1 ' )])->limit (1 )->first () === null ) {
97+ $ addButton ->disable ($ this ->translate ('A channel is required to add a contact ' ));
98+
99+ $ emptyStateMessage = TemplateString::create (
100+ $ this ->translate (
101+ 'No contacts found. To add a new contact, please {{#link}}configure a Channel{{/link}} first. '
102+ ),
103+ ['link ' => (new ActionLink (null , Links::channelAdd ()))->setBaseTarget ('_next ' )]
104+ );
105+ }
106+
107+ $ this ->addContent ($ addButton );
87108
88109 $ this ->addContent (
89110 (new ObjectList ($ contacts , new ContactRenderer ()))
90111 ->setItemLayoutClass (MinimalItemLayout::class)
112+ ->setEmptyStateMessage ($ emptyStateMessage )
91113 );
92114
93115 if (! $ searchBar ->hasBeenSubmitted () && $ searchBar ->hasBeenSent ()) {
@@ -102,12 +124,12 @@ public function indexAction()
102124
103125 public function addAction (): void
104126 {
105- $ this ->addTitleTab (t ('Add Contact ' ));
127+ $ this ->addTitleTab ($ this -> translate ('Add Contact ' ));
106128
107129 $ form = (new ContactForm ($ this ->db ))
108- ->on (ContactForm:: ON_SUCCESS , function (ContactForm $ form ) {
130+ ->on (Form:: ON_SUBMIT , function (ContactForm $ form ) {
109131 $ form ->addContact ();
110- Notification::success (t ('New contact has successfully been added ' ));
132+ Notification::success ($ this -> translate ('New contact has successfully been added ' ));
111133 $ this ->redirectNow (Links::contacts ());
112134 })->handleRequest ($ this ->getServerRequest ());
113135
0 commit comments